How does the compiler do with this code? - Linux

This is a discussion on How does the compiler do with this code? - Linux ; //it's all int main() { printf("hello\n"); } Without ``#include ", it can be compiled, linked and run. Will there be a chance for bugs?...

+ Reply to Thread
Page 1 of 3 1 2 3 LastLast
Results 1 to 20 of 49

Thread: How does the compiler do with this code?

  1. How does the compiler do with this code?

    //it's all
    int main()
    {
    printf("hello\n");
    }

    Without ``#include ", it can be compiled, linked and run.
    Will there be a chance for bugs?

  2. Re: How does the compiler do with this code?

    Ronald wrote:
    > //it's all
    > int main()
    > {
    > printf("hello\n");
    > }
    >
    > Without ``#include ", it can be compiled, linked and run.
    > Will there be a chance for bugs?

    It seems that the glibc functions are built in gcc.
    Why is it done this way?

  3. Re: How does the compiler do with this code?

    Ronald writes:

    > Ronald wrote:
    >> //it's all
    >> int main()
    >> {
    >> printf("hello\n");
    >> }
    >>
    >> Without ``#include ", it can be compiled, linked and run.


    Yes.


    >> Will there be a chance for bugs?


    Yes.


    > It seems that the glibc functions are built in gcc.


    No.


    > Why is it done this way?



    C was invented at a time when punched cards were common, and teletype
    rare. It was the time when ed(1) was invented. Try to edit some file
    with ed(1) for half an hour and you'll understand how interesting it
    is to be able to use functions without declaring them.

    So you get a declaration gratis: int printf(...);

    You can get bugs if some of the arguments are not of the same size as
    an int.


    That's why you should always compile asking for errors and warnings:


    % make CFLAGS='-Wall -Werror -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align \
    -Wwrite-strings -Wconversion -Wstrict-prototypes \
    -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls \
    -Wnested-externs -Winline' p

    cc -Wall -Werror -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -Wconversion -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wnested-externs -Winline p.c -o p
    p.c:2: warning: function declaration isn't a prototype
    p.c: In function `main':
    p.c:3: warning: implicit declaration of function `printf'
    make: *** [p] Error 1



    --
    __Pascal Bourguignon__ http://www.informatimago.com/

    "Indentation! -- I will show you how to indent when I indent your skull!"

  4. Re: How does the compiler do with this code?



    On Dec 16, 6:28 pm, Pascal Bourguignon wrote:
    > Ronald writes:
    > > Ronald wrote:
    > >> //it's all
    > >> int main()
    > >> {
    > >> printf("hello\n");
    > >> }

    >
    > >> Without ``#include ", it can be compiled, linked and run.Yes.

    >
    > >> Will there be a chance for bugs?Yes.

    >
    > > It seems that the glibc functions are built in gcc.No.

    >
    > > Why is it done this way?C was invented at a time when punched cards were common, and teletype

    > rare. It was the time when ed(1) was invented. Try to edit some file
    > with ed(1) for half an hour and you'll understand how interesting it
    > is to be able to use functions without declaring them.
    >
    > So you get a declaration gratis: int printf(...);
    >
    > You can get bugs if some of the arguments are not of the same size as
    > an int.
    >
    > That's why you should always compile asking for errors and warnings:
    >
    > % make CFLAGS='-Wall -Werror -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align \
    > -Wwrite-strings -Wconversion -Wstrict-prototypes \
    > -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls \
    > -Wnested-externs -Winline' p
    >
    > cc -Wall -Werror -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -Wconversion -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wnested-externs -Winline p.c -o p

    You have specified -Wall, is the following -W* necessary?

    > p.c:2: warning: function declaration isn't a prototype
    > p.c: In function `main':
    > p.c:3: warning: implicit declaration of function `printf'
    > make: *** [p] Error 1
    >
    > --
    > __Pascal Bourguignon__ http://www.informatimago.com/
    >
    > "Indentation! -- I will show you how to indent when I indent your skull!"



  5. Re: How does the compiler do with this code?

    Pascal Bourguignon wrote:
    > Ronald writes:
    >
    >> Ronald wrote:
    >>> //it's all
    >>> int main()
    >>> {
    >>> printf("hello\n");
    >>> }
    >>>
    >>> Without ``#include ", it can be compiled, linked and run.

    >
    > Yes.
    >
    >
    >>> Will there be a chance for bugs?

    >
    > Yes.
    >
    >
    >> It seems that the glibc functions are built in gcc.

    >
    > No.


    What about names of glibc?


    >
    >
    >> Why is it done this way?

    >
    >
    > C was invented at a time when punched cards were common, and teletype
    > rare. It was the time when ed(1) was invented. Try to edit some file
    > with ed(1) for half an hour and you'll understand how interesting it
    > is to be able to use functions without declaring them.


    I think it's true. :-)

    > So you get a declaration gratis: int printf(...);
    >
    > You can get bugs if some of the arguments are not of the same size as
    > an int.
    >
    >
    > That's why you should always compile asking for errors and warnings:
    >
    >
    > % make CFLAGS='-Wall -Werror -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align \
    > -Wwrite-strings -Wconversion -Wstrict-prototypes \
    > -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls \
    > -Wnested-externs -Winline' p
    >
    > cc -Wall -Werror -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -Wconversion -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wnested-externs -Winline p.c -o p
    > p.c:2: warning: function declaration isn't a prototype
    > p.c: In function `main':
    > p.c:3: warning: implicit declaration of function `printf'
    > make: *** [p] Error 1
    >



  6. Re: How does the compiler do with this code?

    Ronald writes:

    > //it's all
    > int main()
    > {
    > printf("hello\n");
    > }
    >
    > Without ``#include ", it can be compiled, linked and run.
    > Will there be a chance for bugs?


    Yes, anything can happen. On some systems, arguments are passed
    differently depending on how the function is declared. Specifically,
    functions declared as taking a variable number of arguments often get
    them passed differently than functions with a fixed argument list.

    --
    M錸s Rullg錼d
    mru@inprovide.com

  7. Re: How does the compiler do with this code?



    On Dec 16, 6:54 pm, Ronald wrote:
    > Pascal Bourguignon wrote:
    > > Ronald writes:

    >
    > >> Ronald wrote:
    > >>> //it's all
    > >>> int main()
    > >>> {
    > >>> printf("hello\n");
    > >>> }

    >
    > >>> Without ``#include ", it can be compiled, linked and run.

    >
    > > Yes.

    >
    > >>> Will there be a chance for bugs?

    >
    > > Yes.

    >
    > >> It seems that the glibc functions are built in gcc.

    >
    > > No.What about names of glibc?


    Actually, in C language, the declare is not necessary for function
    call, but if the function is used as value(such as function pointer),
    it must be declared first.
    >
    >
    >
    > >> Why is it done this way?

    >
    > > C was invented at a time when punched cards were common, and teletype
    > > rare. It was the time when ed(1) was invented. Try to edit some file
    > > with ed(1) for half an hour and you'll understand how interesting it
    > > is to be able to use functions without declaring them.I think it's true. :-)

    >
    > > So you get a declaration gratis: int printf(...);

    >
    > > You can get bugs if some of the arguments are not of the same size as
    > > an int.

    >
    > > That's why you should always compile asking for errors and warnings:

    >
    > > % make CFLAGS='-Wall -Werror -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align \
    > > -Wwrite-strings -Wconversion -Wstrict-prototypes \
    > > -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls \
    > > -Wnested-externs -Winline' p

    >
    > > cc -Wall -Werror -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -Wconversion -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wnested-externs -Winline p.c -o p
    > > p.c:2: warning: function declaration isn't a prototype
    > > p.c: In function `main':
    > > p.c:3: warning: implicit declaration of function `printf'
    > > make: *** [p] Error 1



  8. Re: How does the compiler do with this code?

    Ronald wrote:
    > Pascal Bourguignon wrote:
    > > Ronald writes:
    > >> It seems that the glibc functions are built in gcc.

    > > No.

    > What about names of glibc?


    I don't know what you mean with this question, but if a function
    is called that hasn't been defined or at least declared before
    then in C the compiler assumes per default that the function
    returns an int and does take an unspecified number of arguments.
    There's nothing special about this with respect to functions from
    the libc - for gcc (or any other C compiler) the libc is just
    another library. And while in some cases the default may work,
    it sometimes can lead to hard to find bugs. So never forget to
    include all the required include files and always crank up the
    warning level of your compiler so that it complains about missing
    declarations.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \__________________________ http://toerring.de

  9. Re: How does the compiler do with this code?

    "Bin Chen" wrote:
    >On Dec 16, 6:28 pm, Pascal Bourguignon wrote:
    >> That's why you should always compile asking for errors and warnings:
    >>
    >> % make CFLAGS='-Wall -Werror -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align \
    >> -Wwrite-strings -Wconversion -Wstrict-prototypes \
    >> -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls \
    >> -Wnested-externs -Winline' p
    >>
    >> cc -Wall -Werror -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -Wconversion -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wnested-externs -Winline p.c -o p

    >
    >You have specified -Wall, is the following -W* necessary?


    The -Wall option to gcc provides a useful set of warnings. It
    by no means provides what many would consider even a minimum set
    though!

    In addition to -Wall I always add -W which provides another set
    of several warning options, plus this set of individual warning
    options (which is interesting to compair to the above list,

    -Wcast-align -Wcast-qual -Wmissing-prototypes \
    -Wshadow -Wnested-externs -Wstrict-prototypes \
    -Waggregate-return -Wpointer-arith -Wtraditional

    The significant difference is probably that I have not reviewed
    that list for several years, and last went through the manual
    back when gcc was at version 2.95, hence it should be rather out of
    date.

    --
    Floyd L. Davidson
    Ukpeagvik (Barrow, Alaska) floyd@apaflo.com

  10. Re: How does the compiler do with this code?

    Bin Chen wrote:
    >
    > On Dec 16, 6:54 pm, Ronald wrote:
    >> Pascal Bourguignon wrote:
    >>> Ronald writes:
    >>>> Ronald wrote:
    >>>>> //it's all
    >>>>> int main()
    >>>>> {
    >>>>> printf("hello\n");
    >>>>> }
    >>>>> Without ``#include ", it can be compiled, linked and run.
    >>> Yes.
    >>>>> Will there be a chance for bugs?
    >>> Yes.
    >>>> It seems that the glibc functions are built in gcc.
    >>> No.What about names of glibc?

    >
    > Actually, in C language, the declare is not necessary for function
    > call, but if the function is used as value(such as function pointer),
    > it must be declared first.

    I see, the code would be compilable, but not linkable with an arbitrary
    function name.

  11. Re: How does the compiler do with this code?

    Jens Thoms Toerring wrote:
    > Ronald wrote:
    >> Pascal Bourguignon wrote:
    >>> Ronald writes:
    >>>> It seems that the glibc functions are built in gcc.
    >>> No.

    >> What about names of glibc?

    >
    > I don't know what you mean with this question, but if a function
    > is called that hasn't been defined or at least declared before
    > then in C the compiler assumes per default that the function
    > returns an int and does take an unspecified number of arguments.
    > There's nothing special about this with respect to functions from
    > the libc - for gcc (or any other C compiler) the libc is just
    > another library. And while in some cases the default may work,
    > it sometimes can lead to hard to find bugs. So never forget to
    > include all the required include files and always crank up the
    > warning level of your compiler so that it complains about missing
    > declarations.


    I see, the code would be compilable, but not linkable with an arbitrary
    function name. Studied.

  12. Re: How does the compiler do with this code?

    "Bin Chen" writes:

    > On Dec 16, 6:28 pm, Pascal Bourguignon wrote:
    >> cc -Wall -Werror -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -Wconversion -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wnested-externs -Winline p.c -o p

    > You have specified -Wall, is the following -W* necessary?


    AFAIK, yes. They're not included in -Wall...

    --
    __Pascal Bourguignon__ http://www.informatimago.com/

    PUBLIC NOTICE AS REQUIRED BY LAW: Any use of this product, in any
    manner whatsoever, will increase the amount of disorder in the
    universe. Although no liability is implied herein, the consumer is
    warned that this process will ultimately lead to the heat death of
    the universe.

  13. Re: How does the compiler do with this code?

    > You have specified -Wall, is the following -W* necessary?

    confusing name, maybe it's true in the early time. :-)



  14. Why doesn't it fixed? It's reasonable.

    "Ronald" 写入消息新闻:em1n1h$96l$1@news.yaako.com...
    >> You have specified -Wall, is the following -W* necessary?

    >
    > confusing name, maybe it's true in the early time. :-)




  15. Re: How does the compiler do with this code?

    On 2006-12-16, Ronald wrote:
    > //it's all
    > int main()
    > {
    > printf("hello\n");
    > }
    >
    > Without ``#include ", it can be compiled, linked and run.
    > Will there be a chance for bugs?


    not unless you count the non-zero (and possibly random) return value as a bug.

    --

    Bye.
    Jasen

  16. Re: How does the compiler do with this code?

    On 2006-12-16, Ronald wrote:
    > Ronald wrote:
    >> //it's all
    >> int main()
    >> {
    >> printf("hello\n");
    >> }
    >>
    >> Without ``#include ", it can be compiled, linked and run.
    >> Will there be a chance for bugs?

    > It seems that the glibc functions are built in gcc.
    > Why is it done this way?


    it's*not.

    only a few easily optimisable ones are. (like strlen and memcpy)

    --

    Bye.
    Jasen

  17. Re: How does the compiler do with this code?

    jasen writes:

    > On 2006-12-16, Ronald wrote:
    >> //it's all
    >> int main()
    >> {
    >> printf("hello\n");
    >> }
    >>
    >> Without ``#include ", it can be compiled, linked and run.
    >> Will there be a chance for bugs?

    >
    > not unless you count the non-zero (and possibly random) return value
    > as a bug.


    What about the undefined effects of a printf() call without a
    prototype in scope?

    --
    M錸s Rullg錼d
    mru@inprovide.com

  18. Re: How does the compiler do with this code?

    jasen writes:

    > On 2006-12-16, Ronald wrote:
    >> Ronald wrote:
    >>> //it's all
    >>> int main()
    >>> {
    >>> printf("hello\n");
    >>> }
    >>>
    >>> Without ``#include ", it can be compiled, linked and run.
    >>> Will there be a chance for bugs?

    >> It seems that the glibc functions are built in gcc.
    >> Why is it done this way?

    >
    > it's*not.
    >
    > only a few easily optimisable ones are. (like strlen and memcpy)


    And even then, the C library has a full implementation of these
    functions.

    --
    M錸s Rullg錼d
    mru@inprovide.com

  19. Re: How does the compiler do with this code?

    2006-12-17 ,
    jasen wrote:
    > On 2006-12-16, Ronald wrote:
    >> Ronald wrote:
    >>> //it's all
    >>> int main()
    >>> {
    >>> printf("hello\n");
    >>> }
    >>>
    >>> Without ``#include ", it can be compiled, linked and run.
    >>> Will there be a chance for bugs?

    >> It seems that the glibc functions are built in gcc.
    >> Why is it done this way?

    >
    > it's*not.
    >
    > only a few easily optimisable ones are. (like strlen and memcpy)


    Actually, printf is as well, to some extent - not a full implementation,
    but gcc knows enough to translate the above to puts("hello")

  20. Re: How does the compiler do with this code?

    Ronald wrote:
    > Jens Thoms Toerring wrote:
    >
    >> Ronald wrote:
    >>
    >>> Pascal Bourguignon wrote:
    >>>
    >>>> Ronald writes:
    >>>>
    >>>>> It seems that the glibc functions are built in gcc.
    >>>>
    >>>> No.
    >>>
    >>> What about names of glibc?

    >>
    >>
    >> I don't know what you mean with this question, but if a function
    >> is called that hasn't been defined or at least declared before
    >> then in C the compiler assumes per default that the function
    >> returns an int and does take an unspecified number of arguments.
    >> There's nothing special about this with respect to functions from
    >> the libc - for gcc (or any other C compiler) the libc is just
    >> another library. And while in some cases the default may work,
    >> it sometimes can lead to hard to find bugs. So never forget to
    >> include all the required include files and always crank up the
    >> warning level of your compiler so that it complains about missing
    >> declarations.

    >
    >
    > I see, the code would be compilable, but not linkable with an arbitrary
    > function name. Studied.


    Yes, it would be compilable because the compiler would provide a default
    prototype of
    extern int function_with_an_arbitrary_name(...);
    Then your intermediate code would contain an unresolved reference to
    this function and if the function were called in an expression, the
    compiled code would pick up an integer return value from wherever it
    assumes this was deposited.

    When being linked, the linker would scan all libraries it was instructed
    to search and look for an external function
    "function_with_an_arbitrary_name". As it will not find it, it will
    generate an error message and abort.

    So, the program _compiles_ OK, due to the default prototype, but it will
    not link correctly.

    Note that you could specify such a function as having a weak binding,
    but calling that function would be fatal if it were not defined:

    bad:
    extern function_with_an_arbitrary_name() __attribute__ ((weak));
    ...
    function_with_an_arbitrary_name(argument);

    better:
    extern function_with_an_arbitrary_name() __attribute__ ((weak));
    ...
    if (function_with_an_arbitrary_name != NULL)
    function_with_an_arbitrary_name(argument);

    Josef
    --
    Josef M鰈lers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett


+ Reply to Thread
Page 1 of 3 1 2 3 LastLast