πŸ“Š C Programming
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
πŸ’¬ Discuss
βœ… Correct Answer: (A) Display x

Explanation: The output is as follows:

$gcc prog3.c
$ a.out
x

Explanation by: Admin
The output is as follows:

$gcc prog3.c
$ a.out
x

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
166
Total Visits
πŸ“½οΈ
4 y ago
Published
πŸŽ–οΈ
Admin
Publisher
πŸ“ˆ
98%
Success Rate