In C programming, when you declare a variable as 'const int x = 5;', what happens if you try to modify it within the program?
AThe compiler will generate a warning but allow modification at runtime
BThe compiler will generate a compilation error and prevent modification
CThe variable will be modified but will revert to original value after each operation
DThe modification will be allowed only in the main() function
Correct Answer:
B. The compiler will generate a compilation error and prevent modification
EXPLANATION
The 'const' keyword creates a read-only variable. Once initialized, attempting to modify a const variable results in a compilation error (error: assignment of read-only variable). This is enforced at compile-time, not runtime.
Which keyword is used to declare a variable that cannot be modified after initialization?
Astatic
Bvolatile
Cconst
Dextern
Correct Answer:
C. const
EXPLANATION
The 'const' keyword declares a constant variable whose value cannot be modified after initialization. Attempting to modify it results in a compile-time error.