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.

Niyati said:   1 decade ago
We are opening file in read mode then how is it possible to access the file contents(printing the contents of this file)?

Bhandari said:   1 decade ago
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.

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


Post your comments here:

Your comments will be displayed after verification.