Difficulty: Easy
Correct Answer: Error: cannot use static for function parameters
Explanation:
Introduction / Context:
This item asks you to spot improper usage of the keyword static
in function parameter declarations and to think about the validity of returning addresses of local variables. Both topics are common sources of compile-time or runtime issues in C.
Given Data / Assumptions:
static int
.&i
or &j
, which are addresses of parameters (automatic storage).
Concept / Approach:
In standard C, the keyword static
cannot be applied to ordinary scalar parameters. (The only valid use of static
in parameter lists is as an array-parameter qualifier in C99 to specify minimum bounds, not for scalars.) Therefore, the translation unit should be diagnosed at compile time.
Step-by-Step Solution:
Parsing the prototype int *check(static int, static int);
is invalid for scalar parameters.A conforming compiler emits an error about illegal storage-class specifier for function parameter.Because compilation fails, no runtime output is produced.
Verification / Alternative check:
Even if the static
keywords were removed, returning &i
or &j
would still be wrong because those addresses become invalid after the function returns.
Why Other Options Are Wrong:
Options showing numeric outputs assume successful compilation and defined behavior, which does not occur. The “non portable pointer conversion” message is unrelated; the real issue is illegal use of static
on parameters.
Common Pitfalls:
Confusing static
storage duration with parameter passing, and assuming you can return addresses of parameters.
Final Answer:
Error: cannot use static for function parameters
Discussion & Comments