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

Surendra said:   1 decade ago
@kamal Dua
You are right dude.... That's why it printed \n instead of "\r\n".
Thnx.........

Yami said:   1 decade ago
Sundar : Thanks for the nice explanation.

Avinash said:   1 decade ago
Considering all your explanations about \r.
What will str contain if file contains: "india\rbix"?

Varsha said:   1 decade ago
@Sundar

Thank you.

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.

Nischal said:   1 decade ago
@Sundar

I didn't get your example.

\r is to move cursor to left most position in the current line

printf("india\rbix");

Here output must be, first it prints india then \r so again cursor is moved to left most position right?

Please help me this doubt.

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?


Post your comments here:

Your comments will be displayed after verification.