Home / C Programming / Input / Output :: Discussion

Discussion :: Input / Output

  1. What will be the output of the program ?

    #include 
     int main() 
     {   
          FILE *fp;  
          unsigned char ch;   
          /* file 'abc.c' contains "This is FresherGATE " */    
          fp=fopen("abc.c", "r");     
          if(fp == NULL)  
          {      
              printf("Unable to open file");      
             exit(1);    
     }     
     while((ch=getc(fp)) != EOF)         
         printf("%c", ch);      
         
       fclose(fp);   
       printf("\n", ch);   
       return 0; 
    } 
    

  2. A.

    This is fresherGATE

    B.

    This is

    C.

    Infinite loop

    D.

    Error

    View Answer

    Workspace

    Answer : Option C

    Explanation :

    The macro EOF means -1.

    while((ch=getc(fp)) != EOF) Here getc function read the character and convert it to an integer value and store it in the variable ch, but it is declared as an unsigned char. So the while loop runs infinitely.

     

     


Be The First To Comment