What will be the output of the program given below?
Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.
What will be the output of the program given below?
#include <stdio.h>
int main()
{
int x = 1;
{
int y = 2;
}
printf("%d %d ", x, y);
return 0;
}
Show answer & explanation
Answer: C. Compile time error
The variable y is declared inside an inner block (scope) with int y = 2;. Once that block ends, y goes out of scope and is no longer accessible. When printf() tries to reference y outside its block, the compiler cannot find the variable declaration, resulting in a compile-time error: 'undefined reference to y' or similar.
Step-by-step Derivation:
Step-by-step analysis:
int x = 1;declares x in main's scope—valid.{ int y = 2; }creates an inner block where y is declared with block scope.- After the closing brace
}, y is out of scope and no longer exists. printf("%d %d ", x, y);attempts to use y, but y is not visible in main's scope.- Compiler error: y is undeclared at this point.
- The program will NOT compile; it will produce a compile-time error.