C Programming - Input / Output - Discussion


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?

[A]. "I am a boy\r\n\0"
[B]. "I am a boy\r\0"
[C]. "I am a boy\n\0"
[D]. "I am a boy"

Answer: Option C


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"

Amaresh said: (Jun 27, 2010)  
Explain in details the answer.

Rahul said: (Aug 7, 2010)  
What about \r ? Why it is not reading? Can any one explain this please?

Nishanth Shetty K said: (Feb 14, 2011)  
Because it is a carraige return. It does not store in str.

Sairamteja said: (Feb 14, 2011)  
The question is good and \r is a carriage return.

Nayna said: (Mar 2, 2011)  
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.

Sundar said: (Mar 3, 2011)  

Let us know the difference between the \r and \n.

\r - is moves the cursor to the left-most position of the CURRENT line.

\n - is moves the cursor to the left-most position of the NEXT line.


1. printf("India\rBIX");

Output: BIXia

2. printf("India\nBIX");



Note: It may not work as I said in 32-bit platform. But works fine in Turbo C (under DOS 16-bit platform).

Hope the understand the difference. Have a nice day!

Vineet said: (Apr 22, 2011)  
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???????

Riya said: (Apr 25, 2011)  
@Sundar : Thanks for the perfect explanation.

Sushil said: (May 25, 2011)  
Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or a the End-of-File is reached, whichever comes first.

A newline character makes fgets() stop reading, but it is considered a valid character and therefore it is included in the string copied to str.

A null character is automatically appended in str after the characters read to signal the end of the C string.

Varsha said: (Aug 9, 2011)  

Thank you.

Nisha said: (Aug 22, 2011)  
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!

Nischal said: (Sep 2, 2011)  

I didn't get your example.

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


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.

Nikita said: (Sep 20, 2011)  
Why in ans there is '\0'.

Jyoti said: (Oct 7, 2011)  
Nikita, because In C string always ended with null (\0) character.

Kamal Dua said: (Oct 9, 2011)  
This question is on I/O.
In C when we attempt to write a string(which is done using function fputs()).

It converts "\n" into "\r\n".

But while reading using function(which is done using function fgets()). "\r\n" is converted to "\n".

Actually this is a feature of standard library functions.

Hope you guys understand. Have a nice day!

Vivek said: (Dec 2, 2011)  
Please any one clear exlpain.

I don't understand.

Nutan said: (Jan 9, 2012)  
@kamal dua

Thanks for explain it.....

Surendra said: (Jan 27, 2012)  
@kamal Dua
You are right dude.... That's why it printed \n instead of "\r\n".

Yami said: (Feb 7, 2012)  
Sundar : Thanks for the nice explanation.

Avinash said: (May 10, 2012)  
Considering all your explanations about \r.
What will str contain if file contains: "india\rbix"?

Rajeev said: (May 27, 2012)  
Think this might also depend on the OS line separator
*nix \n
Windows \r\n

Viswanath said: (Aug 4, 2012)  
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: (Sep 2, 2012)  
Thanks sundar. Nice explanation.

Isvariyaa said: (Jan 23, 2013)  
Also gets printed in Turbo C compiler.

Gyani said: (Jul 17, 2013)  
And the question still remains the same. Why not ?

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

"\r\n" is returned?

Sakshi said: (Jul 20, 2013)  
Explain this again please.

1. printf("India\rBIX");

Output: BIXia.

Deepa said: (Aug 15, 2013)  
Why \n is printed?

Rahul said: (Aug 21, 2013)  
Why is this \0 printed?

Vipul said: (Sep 17, 2013)  
Hey then both the string have same answer I guess.

"i am a boy\r\n"


"i am a boy\n"

What will happen what pointer traverse '\r' ?

Vishnu said: (Sep 29, 2013)  
Explain this again please.

1. printf("India\rBIX");

Output: BIXia.

Mahesh said: (Oct 2, 2013)  
Why \0 is added at last?

Mohit said: (Dec 18, 2013)  
Please explain:


Output: BIXia.

Sunil said: (Jan 8, 2014)  

You can understand by the example.


It produce an output as bixiacon.

The main function of \r is ,it prints the string which are written in right side and count the number of character(here bix=3) and then the cursor move to the left and skip as many number of character which was read from the right side of \r(here 3).

Richa said: (Apr 23, 2014)  
But yet the question is here only - why /0 is printed ?

Chetan said: (May 6, 2014)  
"\r" will be stored in buffer.

If you try to print str (buffer) with printf or in a file you will not able to see the difference because both are white space. Try to print buffer with debugger like GDB or you can print character one by one.

Kalyani said: (May 21, 2014)  

\0 is a default null character that will be stored automatically at the end of the file when we use file related functions in C.

Ganesh Sable said: (Jun 29, 2014)  
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.

Raviranjan Kumar said: (Jul 5, 2014)  
Many many so thanks guys to discussing this topic.

Neelu said: (Jan 24, 2015)  
In this program only main() is sufficient or necessary int main().

Anis Gupta said: (Jul 24, 2015)  
Hi dear.

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

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

Satish said: (Sep 9, 2015)  

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.

Saraswathi Sivaguru said: (Oct 18, 2015)  
How the \o and \n characters are printed? Please explain?

Madhav said: (Oct 20, 2015)  
If we are writing.

printf ("hello\n welcome");

Output will be:



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.

Aiza said: (Nov 19, 2015)  
Can anybody explain to me why its not be "I am a boy\r\0"?

Gaurav said: (Aug 21, 2016)  
"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.

Ganga said: (Sep 19, 2016)  
Why we can write \r. What is the use of that?

Please explain.

Aparna said: (Mar 1, 2017)  
Will anyone explain it with an example?

Abhi said: (Mar 6, 2017)  
For me, only I am a boy is getting in a string.

Suman said: (Jun 22, 2017)  
@Sundar. Please, explain again.

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

Tanya said: (Jul 8, 2017)  
Yes @Suman.

But that overwrites 'ind' and changes it to 'bix' and hence the output.

Priya said: (Sep 9, 2018)  
printf("india\rBIX") I am not understanding this. Please explain me.

Shubham said: (Jul 31, 2019)  
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.

Rajnandinee said: (Oct 19, 2019)  
Hello guys!
I hope you are doing well.

1. Well there is fputs() Function in C which reads string in the format of "\r\n"

2.But at the time of reading fgets() function reads only "\n"...Not like previously... "\r\n"

3. And \0 is there just because In C string always ended with null (\0) character.

Aravind said: (Jan 29, 2020)  

\r, and \n.

while using fputs() function. it actually puts the char,string value in it.

so in this case;

\r moves the cursor to the left side of the present line. and puts the value whatever behind it.
so printf(" indiabix\rbix)
which is bixiabix. which is actually used for puts function type.

so since the string values is stored in an array. and gets() function is used it doesn't change value just to get the value.

and after the end of the string statement in an array. \0 is used just to ensure the statement is over which is actually use as white spaces.

Shambavi said: (Oct 10, 2020)  
Thank You @Aravind.

Asfahan Ahmad said: (Apr 18, 2021)  
Assume you have a string and you need to override two characters from the beginning.

Then the code like this


But if you do not put anything after r then;
The given string will be as it is.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.