Reputation: 7239
I am new to C and have a question. I think I made a mistake, but the compiler is not giving a warning and the program runs fine.
I believe there is an error in line 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;
}
I do not define a type for the first argument to the function definition void void_int(x) {
, however, gcc is not giving me any errors. The program runs just fine.
I compile with: gcc -Wall -o fp fp.c
.
Would anybody help me trying to understand? Is there a default type for function arguments and is it possibly int?
Thanks and best wishes
-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)
Upvotes: 3
Views: 1153
Reputation: 9811
to keep the compatibility with arcaic versions of the C
language, if a declaration specifies no type, the default type is assumed to be int
.
If you simply specify a version of the language, the compiler will surely help you spotting this
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
^
try to specify a version of the language, it's always a good habit that will help you.
Upvotes: 5