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

Mahesh said:   1 decade ago
Why \0 is added at last?

Mohit said:   1 decade ago
Please explain:

printf("India\rBIX");

Output: BIXia.

Sunil said:   1 decade ago
@Mohit.

You can understand by the example.

printf("indiacon\rbix");

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:   1 decade ago
But yet the question is here only - why /0 is printed ?

Chetan said:   1 decade ago
"\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:   1 decade ago
@Richard.

\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:   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.

Raviranjan kumar said:   1 decade ago
Many many so thanks guys to discussing this topic.

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

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


Post your comments here:

Your comments will be displayed after verification.