Technically, the answer is (d), depends on the compiler. This is because char
may be unsigned, in which case c = fgetc(file)
will never evaluate to -1. Conversely, char
may be signed, in which case c = fgetc(file)
may evaluate to -1.
The result of an assignment is the value assigned to the left side (which is not the value of the right side but rather the value of the right side converted to the type of the left side). Thus, if char
is unsigned, then c = fgetc(file)
always has some value of an unsigned char
, so it is never negative and can never be -1. The program then loops forever.
If char
is signed, then, in normal conditions, the second fgetc(file)
evaluation returns the character that was written to the file when -1
was written. Note that, for %c
, fprintf
converts its int
argument to unsigned char
(even if char
is signed) and writes that. Thus, writing -1
results in writing UCHAR_MAX
to the file. Similarly, fgetc
returns an unsigned char
converted to an int
. Thus, in c = fgetc(file)
, we have the signed char
being assigned the value UCHAR_MAX
(typically 255). For assignment, this value is converted to signed char
. When a value being converted does not fit in the new type, there is either an implementation-defined result or an implementation-defined signal. If the implementation produces -1, then the program normally stops after printing “a”. If the implementation raises a signal, it will be handled, typically by terminating the program. If the implementation neither signals nor produces -1, the program will continue by printing the resulting character and then printing “b”. After that, fgetc
will normally return EOF
. If the result of converting EOF
to char
is -1, then program will then stop. Otherwise, it will either raise a signal or continue printing the result of converting EOF
to char
indefinitely.
(Note: The question alludes to an error caused in or by fprintf
. In normal circumstances, there is no error in fprintf(file, "%c", -1)
. For this call, fprintf
writes the result of converting -1 to an unsigned char
to the file.)
Additionally, we must consider that fopen
could fail (e.g., due to permissions), fclose
could fail (e.g., due to available disk space or a device error), and fopen
could fail (e.g., due to another process removing the file between the fclose
and the fopen
). So none of the answers is complete.