Question discussion

What will be the output of the following C program code if the file "test.txt" is empty, readable, and exists in the appropriate directory?

358860Medium DSSSB TGT Computer Science 0 replies 0 views
0
What will be the output of the following C program code if the file "test.txt" is empty, readable, and exists in the appropriate directory?
#include <stdio.h>
int main() {
    FILE *fp = fopen("test.txt", "r");
    if (fp == NULL) {
        printf("Error\n");
    } else {
        printf("Success\n");
        fclose(fp);
    }
    return 0;
}
  • ASuccess
  • BNULL
  • CGarbage value
  • DError
Correct answer: A

Explanation

An existing, readable file opens successfully even if it is empty, so fopen returns a valid pointer and the program prints Success.

Discussion 0

Nobody has replied yet. Start the discussion!