Verdagon
Verdagon

Reputation: 2610

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;
}

Upvotes: 4

Views: 113

Answers (4)

tomahh
tomahh

Reputation: 13651

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.

Upvotes: 2

Daniel Mart&#237;n
Daniel Mart&#237;n

Reputation: 7845

In C an empty argument list in a function declaration means that the function takes an undefined number of parameters. That's why the C compiler doesn't complain. The same code would be an error in C++ because an empty parameter list in the declaration means that the function takes no arguments.

Upvotes: 1

ouah
ouah

Reputation: 145829

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.

Upvotes: 3

Mark Ransom
Mark Ransom

Reputation: 308138

Originally in C there were no prototypes; there were simply function declarations, and those declarations did not include the parameter list. Later when prototypes were added, the old form of declaration had to be retained for backward compatibility.

Upvotes: 1

Related Questions