GCC function name conflict

前端 未结 3 1220
盖世英雄少女心
盖世英雄少女心 2021-01-13 06:43

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

3条回答
  •  再見小時候
    2021-01-13 07:14

    GCC is playing tricks on you due to the interplay of the following:

    • abs is a built-in function;
    • you're declaring 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.)

提交回复
热议问题