A

Admin • 36.96K Points
Coach

Q. What is the output of the following C program?

Code:
#include <stdio.h>

int main()
{
    char c;
    int i = 0;
    FILE *file;

    // write to the text file
    file = fopen("test.txt", "w+");
    fprintf(file, "%c", 'x');
    fprintf(file, "%c", -1);
    fprintf(file, "%c", 'y');
    fclose(file);

    // read from the text file
    file = fopen("test.txt", "r");
    while ((c = fgetc(file)) != -1)
        printf("%c", c);
    return 0;
}
  • (A) Display x
  • (B) Infinite loop
  • (C) Depends on what fgetc returns
  • (D) Depends on compiler

Explanation by: Admin
The output is as follows:

$gcc prog3.c
$ a.out
x

You must be Logged in to update hint/solution

Discusssion

Login to discuss.

Be the first to start discuss.


Question analytics