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.

Madhav said:   10 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.

Satish said:   1 decade 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.

Vineet said:   1 decade ago
Can anyone explain me...
Why \r doesn't get read..
and if your ans is that because it is a carraige return.
then why \n gets read..it is also a new line type.

and plz give me explanation that why \n is printed???????

Ganesh sable said:   1 decade ago
In this program,
\r same like \0.
So output print to \r .

And cursor automatically go new line,

That wise \n is must print in c this is rule in C "I am a boy\n\0" is output.

Nisha said:   1 decade ago
Everyone wrote whatever is already known, but the question is still unanswered. Why the hell \r didn't get stored while \n does!

Anyone. Give the to 'to the point answer' plz!

Gaurav said:   9 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)

Nayna said:   1 decade ago
Can anyone explain me...
Why \r doesn't get read..
and if your ans is that because it is a carraige return.
then why \n gets read..it is also a new line type.

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' ?

Suman said:   8 years ago
@Sundar. Please, explain again.

printf("india\rBIX") should print india since you said the control shifts to a leftmost position that is i?
(1)

Shubham said:   6 years ago
Please, anyone explain me full explanation of \r And \n What it means?

How 0 comes in output.... "I am a boy\r\n"?

Please tell me.


Post your comments here:

Your comments will be displayed after verification.