GATE / CS ARCHIVE

GATE CSE 2016 SET 1 / QUESTION 12

GATE CSE 2016 Set 1 Q12

Programming in C · Expressions, Operators & Types · Full GATE CSE 2016 Set 1 paper

GATE 2016 Set 1, Q12 1 mark · MCQ
Report issue

Expressions, Operators & Types

Question 12, GATE 2016: Given void f(int,short); and a main body declaring int i=100; short s=12; short *p=&s;, which expression can fill a call-site blank without a type-checking error? A: f(s,*s). B: i=f(i,s). C: f(i,*s). D: f(i,*p). The printed wrapper uses void main(); judge the candidate expressions.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

The function expects int and short and returns void. In D, i is int and *p is short, so f(i,*p) is well typed. A and C incorrectly dereference the nonpointer s; B tries assigning a void result to i. Thus D, evaluating the blank expression independently of the nonstandard void main wrapper.

The unary * operator requires a pointer operand. Since s is a short integer, *s is invalid regardless of the value 12 stored in it. By contrast p has type short*, so *p is a short lvalue compatible with f's second parameter. In B the arguments themselves are compatible, but f returns no value, so its call cannot supply an integer assignment value. Separate argument-type checking from return-value checking.

Original paper, page 6 · Answer key