in Programming in C edited by
12,570 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)$
in Programming in C edited by
12.6k views

1 comment

.

0
0

2 Answers

86 votes
86 votes
Best answer
  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

4 Comments

@Vicky rix... question seems to be correct as we are passing values of i and *p in the calling function instead of address.

1
1

@Vicky rix I also thought the same thing but here in this question we are passing *p , because we have derefernced p and then passed to it will not show any error the ultimate values that are going to be passed are (100 and 12 only), if in option D it is is f(i,p) then it is wrong that time we send an address of short and for catching it we placed a short varible so .. correct option is D only

 

0
0

in C it must be semantic error , as it is a type mismatch. but syntax is correct there.

0
0
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

4 Comments

What if it would be (i,&s) ??
0
0
Then it will return f(100,2000) as per previous comment assumptions. Therefore it would give error as short is a variable holding a value not address.

Correct me if I'm wrong
0
0
explain something in  Option B which is incorrect because of the f function return type is void while in option it shown of type int implicitly.
0
0
Answer:

Related questions