Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the forward declaration need to be identical to its counterpart in the definition?

Notice how in this code, the double quadratic(); at the top doesn't match the **double quadratic(double a, double b, double c) in the definition below main.

Yet oddly, this compiles! I'm using gcc -ansi -Wall -pedantic weird.c and no matter what flags I use, it still works.

This goes against what I've been taught. Why does this compile and work properly?

#include <stdio.h>
#include <math.h>
double inputValues();
double quadratic();

int main()
{
        inputValues();
        inputValues();
        inputValues();
        return 0;
}

double inputValues()
{
        double a, b, c,derp;

        printf("Enter a value a: ");
        scanf("%lf", &a);

        printf("Enter a value b: ");
        scanf("%lf", &b);

        printf("Enter a value c: ");
        scanf("%lf", &c);

        derp = quadratic(a, b, c);

        printf("One x-value for this equation is %0.3f: \n", derp);
        return 0;
}

double quadratic(double a, double b, double c)
{
        double quad;

        quad = (-b + sqrt(b*b-4*a*c))/(2*a);
        return quad;
}
like image 988
Verdagon Avatar asked Mar 25 '23 06:03

Verdagon


2 Answers

double quadratic();

declares a function that returns a double with an unspecified (but fixed) number of parameters.

It matches with the prototype of your function:

double quadratic(double a, double b, double c)
{
   /* ... */
}

This is not equivalent to:

double quadratic(void);

which is a declaration in the prototype form of a function that returns a double with no parameter.

like image 102
ouah Avatar answered Mar 30 '23 01:03

ouah


If you omit the parameter list, the compiler assumes that the function exists, but do not check if parameters match.

Your code would not compile if you had declared double quadratic(void); because their, the signatures would be complete and would not match.

C standard, Committee Draft — April 12, 2011, §6.7.6.3.15

For two function types to be compatible, both shall specify compatible return types. Moreover, the parameter type lists, if both are present, shall agree in the number of parameters and in use of the ellipsis terminator; corresponding parameters shall have compatible types. If one type has a parameter type list and the other type is specified by a function declarator that is not part of a function definition and that contains an empty identifier list, the parameter list shall not have an ellipsis terminator and the type of each parameter shall be compatible with the type that results from the application of the default argument promotions.

like image 27
tomahh Avatar answered Mar 30 '23 00:03

tomahh