IndiaBIX.com
Arithmetic Aptitude Data Interpretation
Logical Reasoning Verbal Reasoning Non Verbal Reasoning
General Knowledge
Sudoku Number puzzles Missing letters puzzles Logical puzzles Playing cards puzzles Clock puzzles
C Programming C# Programming Java Programming
Networking Database Questions Computer Science Basic Electronics Digital Electronics Electronic Devices Circuit Simulation Electrical Enigneering Engineering Mechanics Technical Drawing
Placement Papers Group Disucssion HR Interview Technical Interview Body Language
Aptitude Test Verbal Ability Test Verbal Reasoning Test Logical Reasoning Test C Programming Test Java Programming Test Data Interpretation Test General Knowledge Test
Data Structures Operating Systems Networking DATABASE Database Basics SQL Server Basics SQL Server Advanced SQL Server 2008 JAVA Core Java Java Basics Advanced Java UNIX Unix File Management Unix Memory Management Unix Process Managemnt C Interview Questions The C Language Basics .NET Interview Questions .NET Framework ADO.NET ASP.NET Software Testing

C Programming - Input / Output - Discussion

@ : Home > C Programming > Input / Output > General Questions - Discussion

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?

[A]. "I am a boy\r\n\0"[B]. "I am a boy\r\0"
[C]. "I am a boy\n\0"[D]. "I am a boy"

Answer: Option B

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"


Amaresh said: (Sun, Jun 27, 2010 05:48:31 AM)    
 
Explain in details the answer.

Rahul said: (Sat, Aug 7, 2010 02:10:13 AM)    
 
What about \r ? Why it is not reading? Can any one explain this please?

Nishanth Shetty K said: (Mon, Feb 14, 2011 01:57:18 AM)    
 
Because it is a carraige return. It does not store in str.

Sairamteja said: (Mon, Feb 14, 2011 06:04:30 AM)    
 
The question is good and \r is a carriage return.

Nayna said: (Wed, Mar 2, 2011 03:57:29 PM)    
 
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.

Sundar said: (Thu, Mar 3, 2011 12:03:28 AM)    
 
@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!

Vineet said: (Fri, Apr 22, 2011 05:39:44 AM)    
 
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???????

Riya said: (Mon, Apr 25, 2011 04:54:00 AM)    
 
@Sundar : Thanks for the perfect explanation.

Sushil said: (Wed, May 25, 2011 07:08:24 AM)    
 
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.

Varsha said: (Tue, Aug 9, 2011 09:44:13 PM)    
 
@Sundar

Thank you.

Nisha said: (Mon, Aug 22, 2011 03:20:58 PM)    
 
Everyone wrote whatever is already known, but the question is still unanswered. Why the hell \r didn't get stored while \n does!

Anyone. Give the to 'to the point answer' plz!

Nischal said: (Fri, Sep 2, 2011 09:29:13 AM)    
 
@Sundar

I didn't get your example.

\r is to move cursor to left most position in the current line

printf("india\rbix");

Here output must be, first it prints india then \r so again cursor is moved to left most position right?

Please help me this doubt.

Nikita said: (Tue, Sep 20, 2011 09:03:25 PM)    
 
Why in ans there is '\0'.

Jyoti said: (Fri, Oct 7, 2011 10:07:47 PM)    
 
Nikita, because In C string always ended with null (\0) character.

Kamal Dua said: (Sun, Oct 9, 2011 11:00:24 AM)    
 
This question is on I/O.
In C when we attempt to write a string(which is done using function fputs()).

It converts "\n" into "\r\n".

But while reading using function(which is done using function fgets()). "\r\n" is converted to "\n".

Actually this is a feature of standard library functions.

Hope you guys understand. Have a nice day!

Vivek said: (Fri, Dec 2, 2011 02:24:31 AM)    
 
Please any one clear exlpain.

I don't understand.

Nutan said: (Mon, Jan 9, 2012 12:05:34 AM)    
 
@kamal dua

Thanks for explain it.....

Surendra said: (Fri, Jan 27, 2012 07:39:13 PM)    
 
@kamal Dua
You are right dude.... That's why it printed \n instead of "\r\n".
Thnx.........

Yami said: (Tue, Feb 7, 2012 10:19:33 AM)    
 
Sundar : Thanks for the nice explanation.

Write your comments here:
Name *:     Email:


© 2008-2011 by IndiaBIX™ Technologies. All Rights Reserved | Copyright | Terms of Use & Privacy Policy

Advertise     Contact us: info@indiabix.com     Follow us on twitter!