#function declaration C89 vs C99 or later

7 messages · Page 1 of 1 (latest)

rancid oak
#
#include <stdio.h>

int main(void) {
    double x, y;
    printf("Enter two numbers: ");
    scanf("%lf%lf%lf", &x, &y);
    
    printf("Average of %g and %g: %g\n", x, y, average(x, y));
    
    return 0;
}

int average(double a, double b) {
    return 0;
}```



In `C89`, when a function call is seen for the first time, without a prior declaration or definition of the function, an implicit declaration with int return type is created, right? Only if it comes across a conflicting function definition later (with a return type other than int), does it give an error. 

In `C99`, it's mandatory for a function call to come only after a prior declaration or definition of the function. If the compiler doesn't see a declaration or definition of the function before it's first call, it raises an error.

Is my understanding right?
When I compile the above code in `C89` using `gcc -std=c89 -o prog prog.c`, it compiles without any warnings or errors (which is understandable because the definition of the function that comes after the function call is not conflicting with the implicit declaration of the function with the default int return type). On the other hand, when I compile the code using `C99` standard with `gcc -std=c99 -o prog prog.c`, I only get a warning saying an implicit declaration is being created. I was expecting an error because the call to average() function did not come after either a declaration or definition of the function. Why is this happening? 
If I misunderstood something, please let me know. Just ping me when helping me :)
daring summitBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

meager crypt
#

probably just gcc being "nice" and letting you do this to be backwards compatible with old code

#

the warning is probably there because people don't expect it

#

a lot of things in c are actually warnings and not errors (in most major compilers). Especially when it comes to function call type-checking

tribal steeple
#

In C89 (ANSI C), if a function is called without a prototype in scope and the compiler hasn't seen a prior declaration or prototype of that function, it assumes that the function returns an int and that its arguments match the default argument promotions.

According to the default argument promotions specified in C:

Arguments: If the function is called without a prototype, the default argument promotions are applied to each argument before passing them to the function. This means that smaller integral types (char, short) are promoted to int, and float is promoted to double.

Return Value: If the function is called without a prototype, the compiler assumes that the function returns an int if no other return type is specified.

#

Obviously you don't want to rely on this behaviour and rather use proper prototypes for all functions before their first use.