int f(int a) { return ++a; }int f(unsigned int a) { return --a; }cout << f(5)

Thread Starter

summeranson

Joined Feb 12, 2009
28
2. What will be the result of:

int f(int a) { return ++a; }
int f(unsigned int a) { return --a; }
cout << f(5);


A. Undefined behaviour

B. I don't know

C. Compiler error

D. 6

E. 5

F. 4
 

X-Istence

Joined Sep 24, 2009
1
D is indeed the correct answer.

The web page located at http://cpp.comsci.us/etymology/literals.html describes the various literals that are allowed in C/C++ to describe the various data types. A number on its own is considered an int, and as such the first function list will be executed as it is the closest match for the variable type.

If the author had put 5U it would have become an unsigned integer and the program would have output an unsigned int 4.
 
Top