Under ISO C11 semantics, what is the correct classification of the following…

2025

Under ISO C11 semantics, what is the correct classification of the following program's behavior?

#include <stdio.h>

int main(void)
{
    printf("%nxy");
    printf("%bxy");
    printf("%rxy");
    return 0;
}
  1. A.

    Nothing gets printed

  2. B.

    Error

  3. C.

    Undefined behaviour

  4. D.

    %nxy%bxy%rxy

Attempted by 10 students.

Show answer & explanation

Correct answer: C

Concept

For a formatted variadic function such as printf, the format string determines which additional arguments are consumed and what types they must have.

If a conversion specification requires an argument but no matching argument is supplied, ISO C defines the behavior as undefined. Undefined behavior gives no guarantee about output, diagnostics, or termination.

Application

  1. In printf("%nxy"), the %n conversion consumes an int * argument and stores through that pointer the number of characters written so far.

  2. This call supplies only the format string; it does not supply the int * required by %n. The format therefore attempts to consume a missing argument, so a portable output sequence cannot be predicted.

  3. Even if the first mismatch were ignored, %r is not an ISO C11 conversion specification; an invalid conversion specification independently leaves the behavior undefined.

Cross-check and contrast

  • “Nothing gets printed” claims a fixed output of zero characters, but this execution has no guaranteed output.

  • “Error” claims a required diagnostic or termination, but undefined behavior does not require either one.

  • “%nxy%bxy%rxy” claims that every percent sequence is emitted literally, but the execution is not guaranteed to produce that string.

Result

Therefore, the standards-correct classification is undefined behaviour.

Explore the full course: Infosys Preparation

Loading lesson…