edited by
12,522 views
50 votes
50 votes

Consider the following "C" program.

void f(int, short);
void main()
{
    int i = 100;
    short s = 12;
    short *p = &s;
    ____________;  // call to f()
}

 Which one of the following expressions , when placed in the blank above, will NOT result in a type checking error?

  1. $f(s, *s)$
  2. $i = f(i,s)$
  3. $f(i, *s)$
  4. $f(i, *p)$
edited by

2 Answers

Best answer
86 votes
86 votes
  1. $f(s, *s) - 1^{\text{st}}$ argument is short whereas the function expects an int. But in C language short gets implicitly converted to int during a function call and so this won’t be a type error. But second argument is a pointer (can be $64$ bits on a $\textsf{x64}$ machine) where as the function expects a short (can be even $16$ bits). So this will generate a type error. So, WRONG.
  2. $i = f(i,s)$ - return type is not void. So, WRONG.
  3. $f(i, \ast s) - 1^{\text{st}}$ argument is int, second is again syntax error. So, WRONG
  4. $f(i, \ast p)$ - Both the arguments and return type match. $p$ is a pointer to short, so $\ast p$ is value of short. So, D is ANSWER.
edited by
30 votes
30 votes
By option elimination method:
Options A & C are wrong because *s is an invalid argument as s is not a pointer

Option B is incorrect because the f function return type is void while in option it shows of type int implicitly.

Option D is correct because f(100,12) is a valid function call for f(int, short).
edited by
Answer:

Related questions