Turbo C (non-C99) diagnostic: using a const int variable as an array bound vs a #define constant\n\n#include<stdio.h>\n#define MAX 128\n\nint main()\n{\n const int max = 128; /* not an ICE in old Turbo C /\n char array[max]; / variable length array in old compiler → error /\n char string[MAX]; / OK: macro constant */\n array[0] = string[0] = 'A';\n printf("%c %c\n", array[0], string[0]);\n return 0;\n}

Difficulty: Medium

Correct Answer: Error: unknown max in declaration/Constant expression required

Explanation:


Introduction / Context:
This question contrasts classic Turbo C behavior with modern C99+. In Turbo C (pre-C99), array bounds must be compile-time integer constant expressions (ICEs). A const int variable is not an ICE for that compiler.



Given Data / Assumptions:

  • Compiler: Turbo C (16-bit, pre-C99).
  • #define MAX 128 provides a true compile-time constant.
  • const int max = 128 is not treated as an ICE by Turbo C.


Concept / Approach:
In old compilers without variable length arrays (VLAs), array sizes must be literal constants, enum constants, or macro constants. Using const int max as an array bound triggers an error similar to “constant expression required.”



Step-by-Step Solution:
Define const int max = 128; → not an ICE in Turbo C.Declare char array[max]; → Turbo C errors: constant expression required.Declare char string[MAX]; → OK because MAX is a macro constant.Hence the program fails to compile due to array[max].


Verification / Alternative check:
On a modern C99+ compiler with VLAs, char array[max]; would compile. Specifying -std=c89 or using Turbo C reproduces the error.



Why Other Options Are Wrong:
“invalid array string” misidentifies the line. “No error” is false for Turbo C. “None of above” is unnecessary because (a) states the correct diagnostic.



Common Pitfalls:
Assuming const int is always a compile-time constant; confusing language standards and compiler capabilities; forgetting that VLAs appeared only in C99.



Final Answer:
Error: unknown max in declaration/Constant expression required

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion