c - C中参数的默认数据类型?

标签 c gcc

我是 C 的新手,有一个问题。我想我犯了一个错误,但编译器没有给出警告,程序运行正常。

我认为第 4 行有错误:

#include <stdio.h>

void void_int(int x);
void void_int(x) { // <-- no type definition for parameter x
        printf( "void_int: %d\n", x);
}

int main(int argc, char **argv) {
        printf("Hello world\n");

        // declare a function pointer
        void (*by_ref_void_int)(int);

        // assign pointer to a function
        by_ref_void_int = &void_int;

        // run referenced function
        by_ref_void_int(2);

        return 0;
}

我没有为函数定义 void void_int(x) { 的第一个参数定义类型,但是,gcc 没有给我任何错误。程序运行良好。

我用:gcc -Wall -o fp fp.c 编译。

有人能帮助我理解吗?函数参数是否有默认类型?它可能是 int 吗?

感谢和祝福

-S

$ gcc -v
Using built-in specs.
COLLECT_GCC=/usr/bin/gcc-4.7.real
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.7.2-5' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.7.2 (Debian 4.7.2-5) 

最佳答案

为了保持与 C 语言的原始版本的兼容性,如果声明未指定类型,则假定默认类型为 int

如果你简单地指定语言的一个版本,编译器肯定会帮助你发现这一点

gcc -std=c99 a.c
a.c: In function ‘void_int’:
a.c:4:6: warning: type of ‘x’ defaults to ‘int’ [enabled by default]
 void void_int(x) { // <-- no type definition for parameter x
      ^

尝试指定一个语言版本,这总是一个对你有帮助的好习惯。

关于c - C中参数的默认数据类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23874660/

相关文章:

c++ - 除了直接访问之外,还有其他方法可以更改内存中的值吗?

c++ - 更新数组中给定单元格范围的值的有效方法?

关于c : increment logic in c language中for循环的混淆

读取行缓冲流可以产生多行吗?

c - 无法使用从 fgets 输入的字符串搜索字符

c++ - 在suse10下安装gcc 4.9, "checking whether to enable maintainer-specific portions of Makefiles... no"

linux - 使用 "-fPIC"标志编译可执行文件(无共享库)

c - Makefile 与 obj 文件不匹配

将 D 项目编译为库 - 依赖项会发生什么情况?

mysql - 如何在 Ubuntu 的一个 Makefile 中编译 libssh、mysql 和 net-snmp?