C Programming - Input / Output - Discussion

9. 

What will be the output of the program ?

#include<stdio.h>

int main()
{
    FILE *ptr;
    char i;
    ptr = fopen("myfile.c", "r");
    while((i=fgetc(ptr))!=NULL)
        printf("%c", i);
    return 0;
}

[A]. Print the contents of file "myfile.c"
[B]. Print the contents of file "myfile.c" upto NULL character
[C]. Infinite loop
[D]. Error in program

Answer: Option C

Explanation:

The program will generate infinite loop. When an EOF is encountered fgetc() returns EOF. Instead of checking the condition for EOF we have checked it for NULL. so the program will generate infinite loop.

Fran├žois said: (Oct 28, 2010)  
Well B is the right answer for me on Linux gcc.

Bhandari said: (Dec 26, 2011)  
According to me output depends upon the contents of file if it encounters null character than it will terminate else there will be infinite loop.

Niyati said: (Feb 18, 2012)  
We are opening file in read mode then how is it possible to access the file contents(printing the contents of this file)?

Kiran said: (May 28, 2012)  
@Niyati.

Here we are accessing file from read mode ("r").
So the value accessed is printing on the screen, not into the file.
If you want to print (write) into the file then you should open in "w" mode.

Cherry said: (Jul 1, 2012)  
It depends upon contents of the file.

If the file contains NULL.

Then the o/p is.

Print the contents of file "myfile.c" up to NULL character.

Otherwise infinite loop.

Zahir said: (Aug 11, 2012)  
What if the file has a NULL character in it? Then the correct answer would be option B. The content of the file should be displayed with the question.

Akanchha said: (Oct 19, 2013)  
When is null used and when is EOF?

Sasuke said: (May 17, 2014)  
Its coming segmentation fault for me.

Compilation:
temp.c: In function \'main\':
temp.c:8:25: warning: comparison between pointer and integer [enabled by default]
while((i=fgetc(ptr))!=NULL)

Running:
Segmentation fault (core dumped)

GAMI NIPULKUMAR K. said: (Jun 4, 2015)  
fgetc returns EOF.
fgets returns NULL.

At the end of file, otherwise nonzero value.

Divya said: (Jun 7, 2017)  
On failure file returns EOF so, here we are checking up to NULL, so, up to NULL, it will take. Because after that no content will be there in a file for checking.

Correct me, If I am Wrong.

Onurag said: (Sep 24, 2017)  
If Null comes before some text and Eof I think It will execute upto Null.

"I Want to run" "I want" will get printed.

Rohan said: (Jun 10, 2018)  
FILE *ptr;

Step 1: A pointer ptr is created for FILE

char i;

Step 2: A variable i is declared as character...

ptr = fopen("myfile.c", "r");

Step 3: ptr is used to store the content of "myfile.c" which is opened in readable mode..

while((i=fgetc(ptr))!=NULL)
printf("%c", i);

Step 4: (i) while loop is used , when an EOF is encountered fgetc() returns EOF.
(ii) Instead of checking the condition for EOF we have checked it for NULL.


Ans ->> [c] infinite loop.

Utkarsh said: (Jun 3, 2019)  
What if actually have a NULL character in our file somewhere in between? Please tell me.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.