Wa********@gmail.com wrote:
#include <stdio.h>
double square(); /*without declare
Your comment got truncated; I'm not sure exactly what you're saying.
main()
You appear to be using a C90 compiler; implicit int has been removed in
C99. You should make that "int main(void)", even though this isn't
necessary in C90.
{
double s;
s = square(2);
In C99, with no declaration of square() in scope, that's a constraint
violation.
In C90, with no declaration in scope, using square() in this fashion
would cause square() to be treated as if it takes one integer argument,
and returns an int.
printf("%g\n", s);
}
double square(double x)
In reality, you have defined square() as taking one double argument and
returning a double. Because of the mismatch, the behavior of your
program is undefined. That is the single worst thing the C standard can
say about any program. It means that your program could do anything. It
could print a message saying "This was a mistake". It could print
3.14159. Realistically, there are many compilers where it might produce
a memory violation when square attempts to read a double variable from a
location that only has enough room for an int, or when it attempts to
write a double variable into a location that only has enough room for an
an it.
{
return x*x;
}
========result=========
0
=====================
That is also legal behavior for such a program.
Moral: never rely upon implicit int. That's why implicit int was dropped
in C99. Always use function prototypes, if possible.