如果我声明一个已在 C 包含的头文件中声明的函数,会发生什么情况?

Zar*_*uta -2 c

让我们假设下面的代码:

#include <stdio.h>
#include <math.h>

int abs(int x){
    return x*2;
}

int main(){
    printf("%d\n",abs(-2));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我编译并执行它时,我注意到正在使用math.h的abs函数。实际上,即使我删除#include <math.h>,也会发生这种情况。但我不明白这里发生了什么。

P__*_*J__ 5

  1. abs未在math.honly中声明stdlib.h
  2. GCC 使用内置的ABS 并忽略您的代码。
  3. 您需要使用-fno-builtin标志,程序将按照您的方式运行。

在此输入图像描述

https://godbolt.org/z/v9xM3jbbj