Input / Output

Q1:
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

ANS:A - Print the contents of file "myfile.c"

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.



img not found
img

For help Students Orientation
Mcqs Questions

One stop destination for examination, preparation, recruitment, and more. Specially designed online test to solve all your preparation worries. Go wherever you want to and practice whenever you want, using the online test platform.