In C, identify the correct diagnostic for using the keyword static on function parameters and the result of returning their addresses. #include <stdio.h> int *check(static int, static int); int main() { int *c; c = check(10, 20); printf("%d ", c); return 0; } int *check(static int i, static int j) { int *p, *q; p = &i; q = &j; if (i >= 45) return p; else return q; }

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:

  • Function prototype and definition both declare parameters as static int.
  • The code attempts to return &i or &j, which are addresses of parameters (automatic storage).
  • We are asked for the appropriate outcome among the options provided.


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

More Questions from Pointers

Discussion & Comments

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