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 3 of 6.

Rajeev said:   1 decade ago
Think this might also depend on the OS line separator
*nix \n
Windows \r\n
(1)

Viswanath said:   1 decade ago
Depends on the OS guys. In windows, the line break is \r\n but in linux the line break is \n. So when it reads \r, it automatically understands that it has reached end of line and hence stops further input. GCC must have done the same to allow consistency between various OSes. Not sure though.

Sharmi said:   1 decade ago
Thanks sundar. Nice explanation.

Isvariyaa said:   1 decade ago
Also gets printed in Turbo C compiler.

Gyani said:   1 decade ago
And the question still remains the same. Why not ?

"\r\r\n" is stored and why not?

"\r\n" is returned?

Sakshi said:   1 decade ago
Explain this again please.

1. printf("India\rBIX");

Output: BIXia.

Deepa said:   1 decade ago
Why \n is printed?

Rahul said:   1 decade ago
Why is this \0 printed?

Vipul said:   1 decade ago
Hey then both the string have same answer I guess.

"i am a boy\r\n"

and

"i am a boy\n"

What will happen what pointer traverse '\r' ?

Vishnu said:   1 decade ago
Explain this again please.

1. printf("India\rBIX");

Output: BIXia.


Post your comments here:

Your comments will be displayed after verification.