C programming: what will be the output of this program on a standards-compliant compiler?
#include
int main()
{
const c = -11;
const int d = 34;
printf("%d, %d
", c, d);
return 0;
}
-
AError
-
B-11, 34
-
C11, 34
-
DNone of these
Answer
Correct Answer: Error
Explanation
Introduction / Context:This question checks your understanding of declarations, type qualifiers (const), and compilation rules in C. While many compilers offer extensions, a strictly conforming C compiler requires that every object declaration include a type. Printing behavior only matters if the code compiles, so the key concept is whether the declaration of c is valid C.
Given Data / Assumptions:
- c is declared as
const c = -11;(no explicit type). - d is declared as
const int d = 34;(valid). - printf uses the %d format for both values.
- Assume a standard-conforming compiler (ISO C), not compiler-specific extensions.
Concept / Approach:
In ISO C, const is a type qualifier that must qualify a complete type such as int, double, etc. A declaration like const c = -11; omits the base type, making the declaration ill-formed and causing a compile-time error. Some historical or non-conforming modes might treat it as int, but that is not guaranteed by the C standard.
Step-by-Step Solution:
1) Parse declaration:const appears without a type ⇒ invalid declaration. 2) The compiler reports a syntax or type error before linking or running. 3) Because compilation fails, no program output is produced. Verification / Alternative check:
Try compiling with a standards-conforming mode (for example, enable ISO C warnings). The compiler will reject the declaration of c. Changing it to const int c = -11; makes the program valid and then the output would be -11, 34.
Why Other Options Are Wrong:
-11, 34: Would only be correct if c had a valid type, e.g., const int.
11, 34: The sign would not flip; there is no operation that negates -11.
None of these: The precise correct choice is “Error”.
Common Pitfalls:
- Assuming
constimpliesintby default—this is not guaranteed by the standard. - Testing on permissive compilers and generalizing the behavior as portable.
Final Answer:
Error