OA. free
Free
Qualcomm Embedded Systems & Hardware Embedded Systems & Hardware Medium

What will be the output of this C program?

Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.

#include <stdio.h>

int main()
{
    int a = 10, *i;
    void *k;
    j = k = &a;
    j++;
    k++;
    printf("%n %u %u %i, j, k);
    return 0;
}

What will be the output of this C program?

Choose one option.
Show answer & explanation
Answer: C. Garbage value Garbage value

The program has critical compilation errors: j is undeclared (causing a compilation failure), and the format string %n is invalid for output (it's used for writing to memory, not reading). Even if j were declared as a pointer, the program would not compile or produce defined behavior.

Step-by-step Derivation:
Step-by-step analysis:

  1. Undeclared variable j: The line j = k = &a; references variable j which is never declared. This causes a compilation error (implicit declaration or undefined identifier).

  2. Invalid format specifier %n: The format string printf("%n %u %u %i", j, k); uses %n, which is a write specifier (writes character count to a pointer argument), not a read specifier. It should not be used for output and is undefined behavior.

  3. Assuming compilation somehow succeeded: If j were a pointer (e.g., int *j), then:

    • j = k = &a; would assign the address of a to both j and k
    • j++ would increment j by sizeof(int) = 4 bytes
    • k++ would increment k by 1 byte (void pointers increment by 1 byte)
    • So j and k would point to different addresses
    • The printf would attempt to print using %n, which causes undefined behavior
  4. Actual behavior: The program will not execute successfully due to compilation errors. The output would be garbage or the program would crash, making C) Garbage value Garbage value the most appropriate answer among the given options.