C Programming - Input / Output - Discussion

Discussion Forum : Input / Output - Find Output of Program (Q.No. 9)
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;
}
Print the contents of file "myfile.c"
Print the contents of file "myfile.c" upto NULL character
Infinite loop
Error in program
Answer: Option
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.
Discussion:
13 comments Page 2 of 2.

GAMI NIPULKUMAR K. said:   1 decade ago
fgetc returns EOF.
fgets returns NULL.

At the end of file, otherwise nonzero value.

François said:   1 decade ago
Well B is the right answer for me on Linux gcc.

Akanchha said:   1 decade ago
When is null used and when is EOF?


Post your comments here:

Your comments will be displayed after verification.