C Programming - Input / Output - Discussion

Discussion Forum : Input / Output - General Questions (Q.No. 1)
1.
In a file contains the line "I am a boy\r\n" then on reading this line into the array str using fgets(). What will str contain?
"I am a boy\r\n\0"
"I am a boy\r\0"
"I am a boy\n\0"
"I am a boy"
Answer: Option
Explanation:

Declaration: char *fgets(char *s, int n, FILE *stream);

fgets reads characters from stream into the string s. It stops when it reads either n - 1 characters or a newline character, whichever comes first.

Therefore, the string str contain "I am a boy\n\0"

Discussion:
57 comments Page 4 of 6.

Rahul said:   2 decades ago
What about \r ? Why it is not reading? Can any one explain this please?
(1)

Jyoti said:   1 decade ago
Nikita, because In C string always ended with null (\0) character.

Neelu said:   1 decade ago
In this program only main() is sufficient or necessary int main().

Mohit said:   1 decade ago
Please explain:

printf("India\rBIX");

Output: BIXia.

Ganga said:   9 years ago
Why we can write \r. What is the use of that?

Please explain.

NISHANTH SHETTY K said:   1 decade ago
Because it is a carraige return. It does not store in str.

Aiza said:   10 years ago
Can anybody explain to me why its not be "I am a boy\r\0"?

Saraswathi sivaguru said:   10 years ago
How the \o and \n characters are printed? Please explain?

Richa said:   1 decade ago
But yet the question is here only - why /0 is printed ?

Raviranjan kumar said:   1 decade ago
Many many so thanks guys to discussing this topic.


Post your comments here:

Your comments will be displayed after verification.