without declare parameter [double square(parameter)] return 0 in main

W

WanHongbin

#include <stdio.h>
double square(); /*without declare

main()
{
double s;
s = square(2);
printf("%g\n", s);
}

double square(double x)
{
return x*x;
}

========result=========
0
=====================
 
R

Richard Tobin

double square(); /*without declare
s = square(2);
double square(double x)

Unless you have a prototype in scope (rather than just a declaration),
the compiler does not know the type of the argument, so you must
give it an argument of the right type. (More precisely, of a type
that will be promoted to the right type, so in this case a float
would be ok.)

I would have thought a compiler could spot this kind of error: the
function call is ok given the declaration, but when it reaches the
function definition, the compiler can see that it has been called as
if the argument type were int. But gcc doesn't complain. I suggest
using compiler options to warn about all non-prototype function
declarations. (For gcc, that's -Wstrict-prototypes.)

-- Richard
 
J

James Kuyper

#include <stdio.h>
double square(); /*without declare

Your comment got truncated; I'm not sure exactly what you're saying.

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.
 
B

Ben Bacarisse

James Kuyper said:
Your comment got truncated; I'm not sure exactly what you're saying.


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.


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.


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.


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.

Good advice, but I think a clarification (to the OP) might help. It
reads as though the use of implicit ints are in some way connected to
the OP's problem when they are not.

By writing double square(); the OP has provided a declaration (but not
a prototype) for the square function so both C90 and C99 will be happy
provided the call is valid. When a call is made to a function without
a prototype, the arguments are converted by a fiddly set of rules
called the default argument promotions and it is this (rather than
implicit int) that is being incorrectly relied on. Had the OP
written:

s = square(2.0);

then all would have been well.

But, as I say, your advice is sound: always use prototype, if possible.
 
J

James Kuyper

Ben said:
....
Good advice, but I think a clarification (to the OP) might help. It
reads as though the use of implicit ints are in some way connected to
the OP's problem when they are not.

My apologies. The OP made the mistake of asking his question in the
subject line, without repeating it in the body of his message. My news
reader truncated the long subject line about halfway through. I glanced
at the truncated subject line, but didn't pay much attention, and
mistakenly thought he was asking why the program worked incorrectly if
the declaration were commented out. I didn't realize that he was asking
only about why it misbehaved when the declaration was not missing, but
only lacked a parameter.

The example code contains a broken comment that might have corrected my
misunderstanding, if it had been complete.
 
W

WanHongbin

My apologies. The OP made the mistake of asking his question in the
subject line,withoutrepeating it in the body of his message. My news
reader truncated the long subject line about halfway through. I glanced
at the truncated subject line, but didn't pay much attention, and
mistakenly thought he was asking why the program worked incorrectly if
the declaration were commented out. I didn't realize that he was asking
only about why it misbehaved when the declaration was not missing, but
only lacked a parameter.

The example code contains a broken comment that might have corrected my
misunderstanding, if it had been complete.

Thanks, i slove the problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top