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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • WanHongbin@gmail.com

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

    #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
    =============== ======
  • Richard Tobin

    #2
    Re: without declare parameter [double square(paramete r)] return 0 in main

    In article <9429392e-6e44-417e-8e85-f88098ed799f@k3 0g2000hse.googl egroups.com>,
    WanHongbin@gmai l.com <WanHongbin@gma il.comwrote:
    >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.

    Comment

    • James Kuyper

      #3
      Re: without declare parameter [double square(paramete r)] return 0in main

      WanHongbin@gmai l.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.

      Comment

      • Ben Bacarisse

        #4
        Re: without declare parameter [double square(paramete r)] return 0 in main

        James Kuyper <jameskuyper@ve rizon.netwrites :
        WanHongbin@gmai l.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.
        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.

        Comment

        • James Kuyper

          #5
          Re: without declare parameter [double square(paramete r)] return 0in main

          Ben Bacarisse wrote:
          James Kuyper <jameskuyper@ve rizon.netwrites :
          >
          >WanHongbin@gmai l.com 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
          misunderstandin g, if it had been complete.

          Comment

          • WanHongbin@gmail.com

            #6
            Re: without declare parameter [double square(paramete r)] return 0 inmain

            On Sep 30, 8:24 pm, James Kuyper <jameskuy...@ve rizon.netwrote:
            Ben Bacarisse wrote:
            James Kuyper <jameskuy...@ve rizon.netwrites :
            >
            WanHong...@gmai l.com 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,withoutrep eating 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
            misunderstandin g, if it had been complete.
            Thanks, i slove the problem.

            Comment

            Working...