how come such code can compile?

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

    #1

    how come such code can compile?

    Hi,
    I am having some hard time in reading a code, to me it should not
    compile, but it does!

    Here are the code structure,

    file1.c

    #include "common_herders .h"

    main(){
    if (func1() != 0){
    ... some stuff ...
    }
    }

    I think this file should not compile because func1() is not declared
    before it is called! The definition of func1() is in another file,
    file2.c. The compile command is like this,

    gcc -O2 file1.c file2.c -o executable.elf \
    -Wl,-T -Wl,LinkScr -g -I./include/ -L./lib/

    I checked out that "common_heasers .h", there is no declaration of
    func1().

    Do you see any reason?

  • Jean-Claude Arbaut

    #2
    Re: how come such code can compile?


    To see the answer:

    gcc -Wall -W -c file1.c

    It should print
    "warning: implicit declaration of function 'func1'"


    On 23/06/2005 01:50, linq936@hotmail .com wrote:
    [color=blue]
    > Hi,
    > I am having some hard time in reading a code, to me it should not
    > compile, but it does!
    >
    > Here are the code structure,
    >
    > file1.c
    >
    > #include "common_herders .h"
    >
    > main(){
    > if (func1() != 0){
    > ... some stuff ...
    > }
    > }
    >
    > I think this file should not compile because func1() is not declared
    > before it is called! The definition of func1() is in another file,
    > file2.c. The compile command is like this,
    >
    > gcc -O2 file1.c file2.c -o executable.elf \
    > -Wl,-T -Wl,LinkScr -g -I./include/ -L./lib/
    >
    > I checked out that "common_heasers .h", there is no declaration of
    > func1().
    >
    > Do you see any reason?
    >[/color]

    Comment

    • linq936@hotmail.com

      #3
      Re: how come such code can compile?

      Thanks for your pointing, I do not see the warning.

      But I do get a new knowledge, forward declaration is not a hard
      requirement. It is good to know this.

      Thanks.

      Comment

      • Morris Dovey

        #4
        Re: how come such code can compile?

        linq936@hotmail .com affirmed:

        | Hi,
        | I am having some hard time in reading a code, to me it should not
        | compile, but it does!
        |
        | Here are the code structure,
        |
        | file1.c
        |
        | #include "common_herders .h"
        |
        | main(){
        | if (func1() != 0){
        | ... some stuff ...
        | }
        | }
        |
        | I think this file should not compile because func1() is not
        | declared before it is called! The definition of func1() is in
        | another file, file2.c. The compile command is like this,
        |
        | gcc -O2 file1.c file2.c -o executable.elf \
        | -Wl,-T -Wl,LinkScr -g -I./include/ -L./lib/
        |
        | I checked out that "common_heasers .h", there is no declaration of
        | func1().
        |
        | Do you see any reason?

        Sure. With no declaration, the compiler "assumes" that func1() returns
        an int and does not require arguments. If you enable the appropriate
        warning (sorry, I don't have a gcc reference handy) it'll give you a
        diagnostic message.

        --
        Morris Dovey
        DeSoto Solar
        DeSoto, Iowa USA



        Comment

        • Jean-Claude Arbaut

          #5
          Re: how come such code can compile?




          On 23/06/2005 02:33, linq936@hotmail .com wrote:
          [color=blue]
          > Thanks for your pointing, I do not see the warning.[/color]

          You have no warning with -Wall -W ? That looks strange, which version is
          your gcc ? Maybe you have a different option to enable all warnings, try
          "man gcc" or whatever.
          [color=blue]
          > But I do get a new knowledge, forward declaration is not a hard
          > requirement. It is good to know this.
          >
          > Thanks.
          >[/color]

          Comment

          • linq936@hotmail.com

            #6
            Re: how come such code can compile?

            Actually func1() does have argument, it is an integer. I did not show
            it in my original post.

            Because this is C, now C++, so there is no function name overriden,
            argument list is not important. This is my guess though.

            Comment

            • akarl

              #7
              Re: how come such code can compile?

              linq936@hotmail .com wrote:[color=blue]
              > But I do get a new knowledge, forward declaration is not a hard
              > requirement.[/color]

              Are there any hard requirements in C? ;-)

              Comment

              • Lawrence Kirby

                #8
                Re: how come such code can compile?

                On Wed, 22 Jun 2005 19:32:15 -0500, Morris Dovey wrote:
                [color=blue]
                > linq936@hotmail .com affirmed:
                >
                > | Hi,
                > | I am having some hard time in reading a code, to me it should not
                > | compile, but it does![/color]

                A C99 compiler should generate a diagnostic but there is no requirement
                that compilers for older versions of C do.
                [color=blue]
                > | Here are the code structure,
                > |
                > | file1.c
                > |
                > | #include "common_herders .h"
                > |
                > | main(){
                > | if (func1() != 0){
                > | ... some stuff ...
                > | }
                > | }
                > |
                > | I think this file should not compile because func1() is not
                > | declared before it is called! The definition of func1() is in
                > | another file, file2.c. The compile command is like this,[/color]

                Pre-C99 if no declaration is in scope the compiler acts as if there is an
                implicit declaration of the form

                extern int func();
                [color=blue]
                > |
                > | gcc -O2 file1.c file2.c -o executable.elf \
                > | -Wl,-T -Wl,LinkScr -g -I./include/ -L./lib/
                > |
                > | I checked out that "common_heasers .h", there is no declaration of
                > | func1().
                > |
                > | Do you see any reason?
                >
                > Sure. With no declaration, the compiler "assumes" that func1() returns
                > an int and does not require arguments. If you enable the appropriate
                > warning (sorry, I don't have a gcc reference handy) it'll give you a
                > diagnostic message.[/color]

                No, the *declaration* above does not specify no arguments, it specifies
                unknown arguments but no variable argument list. The caller must ensure
                that the arguments passed are compatible with the function definition
                after the default argument promotions have been applied.

                Lawrence


                Comment

                • Default User

                  #9
                  Re: how come such code can compile?



                  linq936@hotmail .com wrote:[color=blue]
                  > Actually func1() does have argument, it is an integer. I did not show
                  > it in my original post.
                  >
                  > Because this is C, now C++, so there is no function name overriden,
                  > argument list is not important. This is my guess though.[/color]


                  Please quote a relevant portion of the previous message when replying.
                  To do so from the Google interface, don't use the Reply at the bottom
                  of the message. Instead, click "show options" and use the Reply shown
                  in the expanded headers.



                  Brian

                  Comment

                  • Peter Shaggy Haywood

                    #10
                    Re: how come such code can compile?

                    Groovy hepcat Morris Dovey was jivin' on Wed, 22 Jun 2005 19:32:15
                    -0500 in comp.lang.c.
                    Re: how come such code can compile?'s a cool scene! Dig it!
                    [color=blue]
                    >linq936@hotmai l.com affirmed:
                    >
                    >| #include "common_herders .h"
                    >|
                    >| main(){
                    >| if (func1() != 0){
                    >| ... some stuff ...
                    >| }
                    >| }
                    >|
                    >| I think this file should not compile because func1() is not
                    >| declared before it is called! The definition of func1() is in
                    >| another file, file2.c. The compile command is like this,
                    >|
                    >| gcc -O2 file1.c file2.c -o executable.elf \
                    >| -Wl,-T -Wl,LinkScr -g -I./include/ -L./lib/
                    >|
                    >| I checked out that "common_heasers .h", there is no declaration of
                    >| func1().
                    >
                    >Sure. With no declaration, the compiler "assumes" that func1() returns
                    >an int and does not require arguments.[/color]

                    Not quite. Actually it should make no assumptions about what
                    arguments, if any, the function requires. If the number and types
                    (after default argument promotion) of arguments differ from those of
                    the function's formal parameters, the behaviour is undefined.

                    --

                    Dig the even newer still, yet more improved, sig!


                    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
                    I know it's not "technicall y correct" English; but since when was rock & roll "technicall y correct"?

                    Comment

                    • Old Wolf

                      #11
                      Re: how come such code can compile?

                      Morris Dovey wrote:[color=blue]
                      > linq936@hotmail .com affirmed:
                      >
                      > | main(){
                      > | if (func1() != 0){
                      > | ... some stuff ...
                      > | }
                      > | }
                      > |
                      > | I think this file should not compile because func1() is not
                      > | declared before it is called! The definition of func1() is in
                      > | another file, file2.c.
                      >
                      > Sure. With no declaration, the compiler "assumes" that func1()
                      > returns an int and does not require arguments.[/color]

                      It assumes that func1() returns an int, but makes no assumption
                      about the arguments.

                      Comment

                      Working...