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

Jayesh rathod said:   3 years ago
Thanks @Kamal.
(1)

Asfahan Ahmad said:   4 years ago
Assume you have a string and you need to override two characters from the beginning.

Then the code like this

Printf(Hyderabad\raa)

Output:
aaderabad
But if you do not put anything after r then;
The given string will be as it is.
(5)

Shambavi said:   4 years ago
Thank You @Aravind.
(1)

Aravind said:   5 years ago
@All.

\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.
(1)

Rajnandinee said:   5 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.

Shubham said:   5 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.

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

Tanya said:   7 years ago
Yes @Suman.

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

Suman said:   7 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)

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


Post your comments here:

Your comments will be displayed after verification.