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

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;
}
Choose one option.
Show answer & explanation
Answer: B. Compile time error

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 macro MAX; valid because MAX is known at compile time
  • char array[max] → Uses const int max; in standard C, array sizes must be compile-time constant expressions. A const int variable 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.