0

The following code appears to have a number of bugs in it that I would have assumed would produce compilation errors. In particular the bracing of the circle function, and incorrect prototyping. What does the compiler think is going on and why does it think it is acceptable?

#include <stdio.h>
#include <math.h>

double circle ();

int main(void)
{
    double r;
    double a;

    printf("Please enter a radius value: ");
    scanf("%lf", &r);

    a = circle(a, r);
    printf("%lf\n", a);
}

double circle(a, r)

double a, r;
{    
    a = M_PI*(r*r);

    return a;
}
Stumbler
  • 2,056
  • 7
  • 35
  • 61
  • 3
    The function declaration is of a very old style, which is still supported http://stackoverflow.com/questions/1585390/c-function-syntax-parameter-types-declared-after-parameter-list – Sami Kuhmonen Dec 26 '15 at 11:36

1 Answers1

4

When you have a function declaration without specifying parameters like:

double circle ();

the function is specified to take an unspecified number of arguments. So compiler accepts it. This is valid in C. If you declare the prototype as:

double circle (void); /* double takes no arguments */

you'll see that the compiler will issue a diagnostic about it.

And in the actual function definition, you are using the old declaration, which is known as K&R style. It's obsolete but still compilers support it.

P.P
  • 117,907
  • 20
  • 175
  • 238
  • 1
    That's not a prototype! The standard calls this kind of function declaration specifically “declaration without a prototype.” – fuz Dec 26 '15 at 11:39