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

APARNA said:   8 years ago
Will anyone explain it with an example?

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

Please explain.

Gaurav said:   8 years ago
"I am a boy\n\0"

"\n\0" why the two came in the picture and where is gone \r.

please, don't tell me above things again. Explain me in a different way.
(1)

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

Madhav said:   9 years ago
If we are writing.

printf ("hello\n welcome");

Output will be:

Hello.

Welcome.

But when we a are writing.

Printf ("hello\n");

Why? Hello\n is printed.

I mean this time \no is also printed.

Why? Please explain.

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

Satish said:   9 years ago
Hi,

If \r is in printf () then the the characters after it are printed first, now let the number of characters printed till now is N then the first n characters are skipped and following characters are printed upto \r.

Anis gupta said:   9 years ago
Hi dear.

I think the answer will be D, because newline return require.

For Linux \n.
For windows \r\n.
For mac os \r.

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

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.