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

Deepa said:   1 decade ago
Why \n is printed?

Rahul said:   1 decade ago
Why is this \0 printed?

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

Vishnu said:   1 decade ago
Explain this again please.

1. printf("India\rBIX");

Output: BIXia.

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).


Post your comments here:

Your comments will be displayed after verification.