|
extern int x; // Declare x
int main( int argc, char *argv[] )
{
printf("x = %d\n", x);
}
int x = 4; /* Definition of global var x */
|
|
Answer:
|
This practice of making the C compiler assume about the data types of parameters of an undefined function has been preserved in the C programming language (maybe to accomodate older C programs)
|
Let me rephrase this rule to make it easier to understand:
|
Example:
|
#include <stdio.h> /* --------------------------------------------------- Function f( ) is defined before main's use of f( ) --------------------------------------------------- */ int f(int x) { return(x*x); } int main(int argc, char *argv[]) { short x = 2; // *** Short !! int y = 0; y = f(x); // Compiler needs to convert short x --> int and pass to func printf("x = %d, y = %d\n", x, y); } |
The function f( ) is defined before it was used
Therefore:
|
This program will compile without any error or warnings
How to run the program:
|
#include <stdio.h> int main(int argc, char *argv[]) { short x = 2; // *** Short !! int y = 0; y = f(x); // C compiler will assume: int f(int x) !!! // Compiler will convert short x --> int and pass to func !! printf("x = %d, y = %d\n", x, y); } /* --------------------------------------------------- Function f( ) is defined AFTER main's use of f( ) --------------------------------------------------- */ int f(int x) { return(x*x); } |
The function f( ) is defined after it was used !!!
Therefore:
|
Here's the compiler's message:
cs255-1@aruba (4837)> gcc impl-declare2.c impl-declare2.c: In function 'main': impl-declare2.c:9:8: warning: implicit declaration of function 'f' [-Wimplicit-function-declaration] y = f(x); // **** Call f( ) **** ^ |
The compiler issues a warning but there is no (fatal) error !!!
The compilation is successful !! (You can run the program !!!)
The program will run correctly:
cs255-1@aruba (4840)> a.out x = 2, y = 4 |
How to run the program:
|
|
#include <stdio.h> int main(int argc, char *argv[]) { short x = 2; // *** short !!! int y = 0; y = f(x); // Assumes: int f(int x) printf("x = %d, y = %d\n", x, y); } /* --------------------------------------------------- Function f( ) is defined AFTER main's use of f( ) --------------------------------------------------- */ int f(short x) { return(x*x); } |
The compiler error message is:
cs255-1@aruba (4845)> gcc impl-declare3.c impl-declare3.c: In function 'main': impl-declare3.c:9:8: warning: implicit declaration of function 'f' [-Wimplicit-function-declaration] y = f(x); // C compiler will assume: int f(int x) ^ impl-declare3.c: At top level: impl-declare3.c:24:5: error: conflicting types for 'f' int f( short x ) ^ impl-declare3.c:25:1: note: an argument type that has a default promotion can't match an empty parameter name list declaration { ^ impl-declare3.c:9:8: note: previous implicit declaration of 'f' was here y = f(x); // C compiler will assume: int f(int x) ^ |
Explanation:
|
How to run the program:
|
|
We discuss how to declare a function next....