Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding bug in simple add function

Tags:

c

function

add

This was asked in an interview. "There is a bug in the below function, what is it?". It is simple add c function and main function calls it. Given some clue -- "Give different set of input values, test and find bug".

int add (int x, int y)
{ 
    return x + y;
}
like image 304
raghavender reddy Avatar asked Dec 21 '22 03:12

raghavender reddy


1 Answers

The problem can be integer overflow occurs if x+y is greater than INT_MAX or less than INT_MIN. So use long long as return type.

like image 127
banarun Avatar answered Dec 30 '22 22:12

banarun