+ 3

A question

#include <iostream> using namespace std; int f(int a){ return ++a; } int f(unsigned int a){ return --a; } int main() { cout<<f(5); return 0; } How the compiler make a decision between these two function? Output is 6.

20th Apr 2018, 3:41 AM
Yusuf
Yusuf - avatar
3 Answers
+ 2
Numbers are handled as integers if not casted into something else. When casting the argument to unsigned into it runs the other function.
20th Apr 2018, 3:53 AM
Alex
Alex - avatar
+ 4
cout<<f((unsigned)5) displays 4 5 in your example defaults to type int, and thus displays 6.
20th Apr 2018, 3:53 AM
Emma
+ 2
f(5U) is 4 because the U tells the compiler that it's unsigned.
20th Apr 2018, 4:59 AM
Timon Paßlick