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?
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:
1 decade ago
Can anybody explain to me why its not be "I am a boy\r\0"?
Sushil said:
2 decades ago
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.
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.
Ganga said:
10 years ago
Why we can write \r. What is the use of that?
Please explain.
Please explain.
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.
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.
Abhi said:
9 years ago
For me, only I am a boy is getting in a string.
Riya said:
2 decades ago
@Sundar : Thanks for the perfect explanation.
Vineet said:
2 decades 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???????
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???????
Priya said:
8 years ago
printf("india\rBIX") I am not understanding this. Please explain me.
Shubham said:
7 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.
How 0 comes in output.... "I am a boy\r\n"?
Please tell me.
Sundar said:
2 decades 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!
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!
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers