I was having some problems with a sample code i was testing, since my abs function was not returning the correct result. abs(-2) was outputing -2 (this, by the way, is supos
GCC is playing tricks on you due to the interplay of the following:
abs
is a built-in function;abs
to return unsigned int
while the standard (and built-in) abs
returns signed int
.Try compiling with gcc -fno-builtin
; on my box, that gives the expected result of 1
. Compiling without that option but with abs
declared as returning signed int
causes the program to print 2
.
(The real solution to this problem is to not use library identifiers for your own functions. Also note that you shouldn't be printing an unsigned int
with %d
.)