Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > without declare parameter [double square(parameter)] return 0 in main

Reply
Thread Tools

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

 
 
WanHongbin@gmail.com
Guest
Posts: n/a
 
      09-30-2008
#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
=====================
 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      09-30-2008
In article <(E-Mail Removed)>,
http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:

>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
--
Please remember to mention me / in tapes you leave behind.
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      09-30-2008
(E-Mail Removed) 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.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-30-2008
James Kuyper <(E-Mail Removed)> writes:

> (E-Mail Removed) 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.


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.

--
Ben.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      09-30-2008
Ben Bacarisse wrote:
> James Kuyper <(E-Mail Removed)> writes:
>
>> (E-Mail Removed) wrote:
>>> #include <stdio.h>
>>> double square(); /*without declare

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

....
> 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.
 
Reply With Quote
 
WanHongbin@gmail.com
Guest
Posts: n/a
 
      10-01-2008
On Sep 30, 8:24*pm, James Kuyper <(E-Mail Removed)> wrote:
> Ben Bacarisse wrote:
> > James Kuyper <(E-Mail Removed)> writes:

>
> >> (E-Mail Removed) wrote:
> >>> #include <stdio.h>
> >>> double square(); * /*withoutdeclare
> >> Your comment got truncated; I'm not sure exactly what you're saying.

> ...
> > 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,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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
main without a return statement axr0284 C Programming 20 11-13-2007 07:08 PM
int main(void) { return main(); } Army1987 C Programming 37 04-03-2007 06:45 AM
Command line options: using an [option: parameter] without a parameter passed to it soren625 Perl Misc 10 12-28-2005 09:26 PM
How do I return a return-code from main? wl Java 2 03-05-2004 05:15 PM



Advertisments