Thursday, June 12, 2008

variable type checking in C

Put all the files in a directory. Run gcc *.c -o main.exe. The compilation will succeed. The reason is that C does not check type consistency of external variable declaration and reference.
1 one.c

int abc = 1;

2 main.c

#include <stdio.h>
extern float abc;
int main(void) {
printf( "%f", abc );
}

Function type checking in C

Put all the following 3 files in a directory. Run gcc *.c. The compilation will succeed. And running the resulted excecutable file will print I am here, man!. Run g++ *.c The compilation will fail. The reason is that during linking, C only check function names. But C++ check function type.
1. caller.c

#include "callee.h"
int main(void) {
foo();
}

2. callee.h

int foo(void);

3. callee.c

#include
void foo(int v) {
printf( "I am here, man!" );
}