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

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

Riya said:   1 decade ago
@Sundar : Thanks for the perfect explanation.

Richa said:   1 decade ago
But yet the question is here only - why /0 is printed ?

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

Abhi said:   8 years ago
For me, only I am a boy is getting in a string.

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

Sundar said:   1 decade ago
@Nayna

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.

Example:

1. printf("India\rBIX");

Output: BIXia

2. printf("India\nBIX");

Output:

India
BIX

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!

Priya said:   7 years ago
printf("india\rBIX") I am not understanding this. Please explain me.

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.

Rajnandinee said:   6 years ago
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.


Post your comments here:

Your comments will be displayed after verification.