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?
Show answer & explanation
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:
Undeclared variable
j: The linej = k = &a;references variablejwhich is never declared. This causes a compilation error (implicit declaration or undefined identifier).Invalid format specifier
%n: The format stringprintf("%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.Assuming compilation somehow succeeded: If
jwere a pointer (e.g.,int *j), then:j = k = &a;would assign the address ofato bothjandkj++would incrementjbysizeof(int)= 4 bytesk++would incrementkby 1 byte (void pointers increment by 1 byte)- So
jandkwould point to different addresses - The printf would attempt to print using
%n, which causes undefined behavior
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.