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

Discussion in 'C Programming' started by WanHongbin@gmail.com, Sep 30, 2008.

  1. Guest

    #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
    =====================
    , Sep 30, 2008
    #1
    1. Advertising

  2. In article <>,
    <> 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.
    Richard Tobin, Sep 30, 2008
    #2
    1. Advertising

  3. James Kuyper Guest

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

    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.
    James Kuyper, Sep 30, 2008
    #3
  4. James Kuyper <> writes:

    > 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.
    Ben Bacarisse, Sep 30, 2008
    #4
  5. James Kuyper Guest

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

    Ben Bacarisse wrote:
    > James Kuyper <> writes:
    >
    >> 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.
    James Kuyper, Sep 30, 2008
    #5
  6. Guest

    Re: without declare parameter [double square(parameter)] return 0 inmain

    On Sep 30, 8:24 pm, James Kuyper <> wrote:
    > Ben Bacarisse wrote:
    > > James Kuyper <> writes:

    >
    > >> 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.
    , Oct 1, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,202
  2. wl
    Replies:
    2
    Views:
    570
    Dimitri Maziuk
    Mar 5, 2004
  3. Sydex
    Replies:
    12
    Views:
    6,453
    Victor Bazarov
    Feb 17, 2005
  4. Army1987

    int main(void) { return main(); }

    Army1987, Mar 29, 2007, in forum: C Programming
    Replies:
    37
    Views:
    1,424
    Daniel Rudy
    Apr 3, 2007
  5. aregee
    Replies:
    7
    Views:
    579
    Alan Mackenzie
    Jan 9, 2011
Loading...

Share This Page