QUESTION 32 Predict the output or error(s) if any in the program given below?
Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.
QUESTION 32
Predict the output or error(s) if any in the program given below?
#include <stdio.h>
#define MAX 128
int main()
{
const int max = 128;
char array[max];
char string[MAX];
array[0] = string[0] = 'A';
printf("%c %c", array[0], string[0]);
return 0;
}
Show answer & explanation
In C, Variable Length Arrays (VLAs) are not standard in C89/C90, and most compilers reject them at compile time when declared as char array[max] where max is a runtime constant (even if marked const). The #define MAX 128 creates a compile-time constant, allowing char string[MAX] to compile, but char array[max] fails because const int max is a runtime constant, not a compile-time constant. Therefore, a compile-time error occurs.
Step-by-step Derivation:
Step 1: Analyze variable declarations.
#define MAX 128→ Preprocessor macro (compile-time constant)const int max = 128→ Runtime constant (const-qualified variable)
Step 2: Check array declarations.
char string[MAX]→ Uses macroMAX; valid because MAX is known at compile timechar array[max]→ Usesconst int max; in standard C, array sizes must be compile-time constant expressions. Aconst intvariable is NOT a compile-time constant expression per C99 and earlier standards.
Step 3: Compiler behavior.
Most C compilers (gcc, clang, MSVC) reject variable-length arrays or fail when the size is a non-constant expression. This code will not compile.
Conclusion: Compile time error is the correct answer.