Why this works???

Discussion in 'C Programming' started by Kenny McCormack, Jan 11, 2008.

  1. #include <stdio.h>
    int main(void) { printf("hello, world\n"); }

    I compile it with all warning turned on and it still works fine.
     
    Kenny McCormack, Jan 11, 2008
    #1
    1. Advertising

  2. (Kenny McCormack) wrote in news:fm8ufo$hfj$1
    @news.xmission.com:

    > #include <stdio.h>
    > int main(void) { printf("hello, world\n"); }
    >
    > I compile it with all warning turned on and it still works fine.



    Forgetting for the moment that you're a troll -- a troll who's actually
    not half-bad at C programming, albeit, it's a shame you don't contribute
    your expertise -- we both already know why the C standard permits that
    program to do whatever it likes.

    I've plenty of friends that get a kick out of winding people up... but
    to be honest they've got better, more entertaining ways than posting to a
    newsgroup. Which is why I'm led to believe that you actually do have more
    than a passing interest in C.

    I think you'll find that if you were to start posting something other
    than tripe, that the regulars would welcome you with open arms. And no,
    that's not an invitation to start a debate. In fact, as a precaution, I'm
    going to killfile this thread right now.

    Best of luck to you in life though, nobody can deny that you're
    creative and inventive, even if it's misplaced.

    --
    Tomás Ó hÉilidhe
     
    Tomás Ó hÉilidhe, Jan 12, 2008
    #2
    1. Advertising

  3. On Sat, 12 Jan 2008 00:05:21 +0000, Tomás Ó hÉilidhe wrote:
    > (Kenny McCormack) wrote in
    > news:fm8ufo$hfj$1 @news.xmission.com:
    >> #include <stdio.h>
    >> int main(void) { printf("hello, world\n"); }
    >>
    >> I compile it with all warning turned on and it still works fine.

    >
    > we both already know why the C standard permits that
    > program to do whatever it likes.


    I don't. I don't see anything wrong with Kenny's program, and I suspect
    there was nothing meant to be wrong with it.

    And your message headers are still broken.
     
    Harald van Dijk, Jan 12, 2008
    #3
  4. In article <dea6$47880837$541dfcd3$1.nb.home.nl>,
    =?UTF-8?q?Harald_van_D=C4=B3k?= <> wrote:
    >On Sat, 12 Jan 2008 00:05:21 +0000, Tomás Ó hÉilidhe wrote:
    >> (Kenny McCormack) wrote in
    >> news:fm8ufo$hfj$1 @news.xmission.com:
    >>> #include <stdio.h>
    >>> int main(void) { printf("hello, world\n"); }


    >>> I compile it with all warning turned on and it still works fine.


    >I don't. I don't see anything wrong with Kenny's program, and I suspect
    >there was nothing meant to be wrong with it.


    In C89, there would be the failure to return a value from main.
    --
    "I will speculate that [...] applications [...] could actually see a
    performance boost for most users by going dual-core [...] because it
    is running the adware and spyware that [...] are otherwise slowing
    down the single CPU that user has today" -- Herb Sutter
     
    Walter Roberson, Jan 12, 2008
    #4
  5. On Sat, 12 Jan 2008 00:36:29 +0000, Walter Roberson wrote:
    > In article <dea6$47880837$541dfcd3$1.nb.home.nl>,
    > =?UTF-8?q?Harald_van_D=C4=B3k?= <> wrote:
    >>On Sat, 12 Jan 2008 00:05:21 +0000, Tomás Ó hÉilidhe wrote:
    >>> (Kenny McCormack) wrote in
    >>> news:fm8ufo$hfj$1 @news.xmission.com:
    >>>> #include <stdio.h>
    >>>> int main(void) { printf("hello, world\n"); }

    >
    >>>> I compile it with all warning turned on and it still works fine.

    >
    >>I don't. I don't see anything wrong with Kenny's program, and I suspect
    >>there was nothing meant to be wrong with it.

    >
    > In C89, there would be the failure to return a value from main.


    In C89, this causes an undefined exit status to be returned to the host
    environment, but it no more an error than printing the result of rand().
     
    Harald van Dijk, Jan 12, 2008
    #5
  6. In article <2ee2d$47880ddf$541dfcd3$1.nb.home.nl>,
    Harald van Dijk <> wrote:
    >On Sat, 12 Jan 2008 00:36:29 +0000, Walter Roberson wrote:
    >> In article <dea6$47880837$541dfcd3$1.nb.home.nl>,
    >> =?UTF-8?q?Harald_van_D=C4=B3k?= <> wrote:
    >>>On Sat, 12 Jan 2008 00:05:21 +0000, Tomás Ó hÉilidhe wrote:
    >>>> (Kenny McCormack) wrote in
    >>>> news:fm8ufo$hfj$1 @news.xmission.com:
    >>>>> #include <stdio.h>
    >>>>> int main(void) { printf("hello, world\n"); }

    >>
    >>>>> I compile it with all warning turned on and it still works fine.

    >>
    >>>I don't. I don't see anything wrong with Kenny's program, and I suspect
    >>>there was nothing meant to be wrong with it.

    >>
    >> In C89, there would be the failure to return a value from main.

    >
    >In C89, this causes an undefined exit status to be returned to the host
    >environment, but it no more an error than printing the result of rand().


    Actually, every time I post this sort of thing (which is my stock sort
    of response to idiotic "Why this works?" type posts), I forget about the
    need for a "return 0;" at the end (actually testing my code with gcc
    with all warnings turned on would have shown this up).

    Still, I have to ask this: The C standard of course says that once UB
    happens, all bets are off. And, let's assume for the sake of argument
    that the above program does cause UB. But the point is, it doesn't
    cause the UB until the function returns. I.e., the statement above is
    "... once UB happens". But it is not the case that future UB can cause
    current operations to fail. So, the above program would (under all the
    usual CLC rules) work, as far as printing "hello, world", even though it
    might start global thermonuclear war thereafter.

    P.S. Of course, I lied in the original post, since if I had compiled it
    with all warning turned on, I would have noticed the lack of "return 0;".

    Oh well...
     
    Kenny McCormack, Jan 12, 2008
    #6
  7. On Sat, 12 Jan 2008 01:06:48 +0000, Kenny McCormack wrote:
    >>>>> (Kenny McCormack) wrote in
    >>>>> news:fm8ufo$hfj$1 @news.xmission.com:
    >>>>>> #include <stdio.h>
    >>>>>> int main(void) { printf("hello, world\n"); }
    >>>

    > Actually, every time I post this sort of thing (which is my stock sort
    > of response to idiotic "Why this works?" type posts), I forget about the
    > need for a "return 0;" at the end (actually testing my code with gcc
    > with all warnings turned on would have shown this up).


    True, gcc can be convinced to warn about it in its default mode and in
    its C90 mode. It won't warn about it in its C99 mode, though.

    > Still, I have to ask this: The C standard of course says that once UB
    > happens, all bets are off. And, let's assume for the sake of argument
    > that the above program does cause UB. But the point is, it doesn't
    > cause the UB until the function returns. I.e., the statement above is
    > "... once UB happens". But it is not the case that future UB can cause
    > current operations to fail. So, the above program would (under all the
    > usual CLC rules) work, as far as printing "hello, world", even though it
    > might start global thermonuclear war thereafter.


    Indeed. I've asked a similar question in the past. If the compiler can
    prove that at a given point, there is absolutely no way to finish running
    the program without it invoking undefined behaviour, there are already
    effectively no requirements on the implementation. The implementation
    would be allowed to print "hello, world", and then immediately do
    everything in its power to undo it. It would also be allowed to optimise
    that and simply do nothing, even if stdout is redirected to a printer (or
    any other device where removing output is physically impossible).
     
    Harald van Dijk, Jan 12, 2008
    #7
  8. Kenny McCormack

    user923005 Guest

    On Jan 11, 5:18 pm, Harald van D©¦k <> wrote:
    > On Sat, 12 Jan 2008 01:06:48 +0000, Kenny McCormack wrote:
    > >>>>> (Kenny McCormack) wrote in
    > >>>>> news:fm8ufo$hfj$1 @news.xmission.com:
    > >>>>>> #include <stdio.h>
    > >>>>>> int main(void) { printf("hello, world\n"); }

    >
    > > Actually, every time I post this sort of thing (which is my stock sort
    > > of response to idiotic "Why this works?" type posts), I forget about the
    > > need for a "return 0;" at the end (actually testing my code with gcc
    > > with all warnings turned on would have shown this up).

    >
    > True, gcc can be convinced to warn about it in its default mode and in
    > its C90 mode. It won't warn about it in its C99 mode, though.
    >
    > > Still, I have to ask this: The C standard of course says that once UB
    > > happens, all bets are off. And, let's assume for the sake of argument
    > > that the above program does cause UB. But the point is, it doesn't
    > > cause the UB until the function returns. I.e., the statement above is
    > > "... once UB happens". But it is not the case that future UB can cause
    > > current operations to fail. So, the above program would (under all the
    > > usual CLC rules) work, as far as printing "hello, world", even though it
    > > might start global thermonuclear war thereafter.

    >
    > Indeed. I've asked a similar question in the past. If the compiler can
    > prove that at a given point, there is absolutely no way to finish running
    > the program without it invoking undefined behaviour, there are already
    > effectively no requirements on the implementation. The implementation
    > would be allowed to print "hello, world", and then immediately do
    > everything in its power to undo it. It would also be allowed to optimise
    > that and simply do nothing, even if stdout is redirected to a printer (or
    > any other device where removing output is physically impossible).


    /*
    OK, What am I missing? I do not see how undefined behavior comes into
    play here.

    I see nothing wrong with the program:
    */
    #include <stdio.h>
    int main(void) { printf("hello, world\n"); }

    /*
    5.1.2.2.3 Program termination
    1 If the return type of the main function is a type compatible with
    int, a return from the initial call to the main function is equivalent
    to calling the exit function with the value returned by the main
    function as its argument;10) reaching the } that terminates the main
    function returns a value of 0. If the return type is not compatible
    with int, the termination status returned to the host environment is
    unspecified.
    Forward references: definition of terms (7.1.1), the exit function
    (7.20.4.3).
    */
     
    user923005, Jan 12, 2008
    #8
  9. Harald van Dijk wrote:
    > On Sat, 12 Jan 2008 01:06:48 +0000, Kenny McCormack wrote:
    >>>>>> (Kenny McCormack) wrote in

    >> current operations to fail. So, the above program would (under all the
    >> usual CLC rules) work, as far as printing "hello, world", even though it
    >> might start global thermonuclear war thereafter.


    printf() sends data to the stream, and the \n instructs the stream to
    flush.

    Neither of these forces the output to definitively appear - so
    (certainly in a multitasking multithreaded world) the OS has a chance to
    say "oops, UB, replace contents of print buffer with the words 'your
    mother is a big hippo', rewind the mouse cable, connect 240V mains to
    the ESC key and wind DRA1: 65535 ells backwards from the start mark'.


    > Indeed. I've asked a similar question in the past. If the compiler can
    > prove that at a given point, there is absolutely no way to finish running
    > the program without it invoking undefined behaviour, there are already
    > effectively no requirements on the implementation.


    UB is an aspect of /runtime/ not compile-time behaviour.

    --
    Mark McIntyre

    CLC FAQ <http://c-faq.com/>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
     
    Mark McIntyre, Jan 12, 2008
    #9
  10. Kenny McCormack

    user923005 Guest

    On Jan 11, 5:29 pm, Mark McIntyre <> wrote:
    > Harald van D©¦k wrote:
    > > On Sat, 12 Jan 2008 01:06:48 +0000, Kenny McCormack wrote:
    > >>>>>> (Kenny McCormack) wrote in
    > >> current operations to fail. So, the above program would (under all the
    > >> usual CLC rules) work, as far as printing "hello, world", even though it
    > >> might start global thermonuclear war thereafter.

    >
    > printf() sends data to the stream, and the \n instructs the stream to
    > flush.
    >
    > Neither of these forces the output to definitively appear - so
    > (certainly in a multitasking multithreaded world) the OS has a chance to
    > say "oops, UB, replace contents of print buffer with the words 'your
    > mother is a big hippo', rewind the mouse cable, connect 240V mains to
    > the ESC key and wind DRA1: 65535 ells backwards from the start mark'.
    >
    > > Indeed. I've asked a similar question in the past. If the compiler can
    > > prove that at a given point, there is absolutely no way to finish running
    > > the program without it invoking undefined behaviour, there are already
    > > effectively no requirements on the implementation.

    >
    > UB is an aspect of /runtime/ not compile-time behaviour.


    Under 7.19.3:
    "4 A file may be disassociated from a controlling stream by closing
    the file. Output streams are flushed (any unwritten buffer contents
    are transmitted to the host environment) before the stream is
    disassociated from the file. the value of a pointer to a FILE object
    is indeterminate after the associated file is closed (including the
    standard text streams). Whether a file of zero length (on which no
    characters have been written by an output stream) actually exists is
    implementation-defined.
    5 The file may be subsequently reopened, by the same or another
    program execution, and its contents reclaimed or modified (if it can
    be repositioned at its start). If the main function returns to its
    original caller, or if the exit function is called, all open files are
    closed (hence all output streams are flushed) before program
    termination. Other paths to program termination, such as calling the
    abort function, need not close all files properly."

    The standard output stream stdout must be flushed when the program
    exits.
     
    user923005, Jan 12, 2008
    #10
  11. On Sat, 12 Jan 2008 01:29:16 +0000, Mark McIntyre wrote:
    > Harald van Dijk wrote:
    >> On Sat, 12 Jan 2008 01:06:48 +0000, Kenny McCormack wrote:
    >>>>>>> (Kenny McCormack) wrote in
    >>> current operations to fail. So, the above program would (under all
    >>> the usual CLC rules) work, as far as printing "hello, world", even
    >>> though it might start global thermonuclear war thereafter.

    >
    > printf() sends data to the stream, and the \n instructs the stream to
    > flush.
    >
    > Neither of these forces the output to definitively appear [...]


    Good point.

    >> Indeed. I've asked a similar question in the past. If the compiler can
    >> prove that at a given point, there is absolutely no way to finish
    >> running the program without it invoking undefined behaviour, there are
    >> already effectively no requirements on the implementation.

    >
    > UB is an aspect of /runtime/ not compile-time behaviour.


    If you mean that compiling a program that has UB should not reformat your
    hard drive if you don't actually try to run the program, then sure. If
    you mean that UB (as opposed to syntax errors or constraint violations)
    means a program must still be compiled by any conforming implementation,
    then that is not entirely correct, because compilers are allowed to
    reject programs when they can prove every execution path of the entire
    program will invoke UB. But if you meant either of those, then it's not
    relevant to my message, and if you meant something else, I didn't get it.
     
    Harald van Dijk, Jan 12, 2008
    #11
  12. On Fri, 11 Jan 2008 17:28:27 -0800, user923005 wrote:
    >> On Sat, 12 Jan 2008 01:06:48 +0000, Kenny McCormack wrote:
    >> > [...] And, let's assume for the sake of
    >> > argument that the above program does cause UB. [...]

    >
    > /*
    > OK, What am I missing? I do not see how undefined behavior comes into
    > play here.


    There was no undefined behaviour, just a "what if" question.
     
    Harald van Dijk, Jan 12, 2008
    #12
  13. Kenny McCormack

    Guest

    On Jan 12, 9:06 am, (Kenny McCormack)
    wrote:
    [...]
    > >>>>> #include <stdio.h>
    > >>>>> int main(void) { printf("hello, world\n"); }

    >
    > >>>>> I compile it with all warning turned on and it still works fine.

    >

    [...]
    >
    > Actually, every time I post this sort of thing (which is my stock sort
    > of response to idiotic "Why this works?" type posts), I forget about the
    > need for a "return 0;" at the end (actually testing my code with gcc
    > with all warnings turned on would have shown this up).
    >


    I might say it isn't idiotic.

    > P.S.  Of course, I lied in the original post, since if I had compiled it
    > with all warning turned on, I would have noticed the lack of "return 0;".


    The lack of a return statement means /*return (void) expr;*/; and the
    function main() itself is type of non-void. The undefined behavior
    definitively happens then. The whole program contains UB and may
    cause demon fly out of your nose.
     
    , Jan 12, 2008
    #13
  14. Kenny McCormack

    CBFalconer Guest

    Harald van D?k wrote:
    > Tomás Ó hÉilidhe wrote:
    >

    .... snip ...
    >
    >> we both already know why the C standard permits that
    >> program to do whatever it likes.

    >
    > I don't. I don't see anything wrong with Kenny's program, and I
    > suspect there was nothing meant to be wrong with it.
    >
    > And your message headers are still broken.


    How? I see nothing wrong, except (possibly) the char set for the
    senders name.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.


    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Jan 12, 2008
    #14
  15. In article <>,
    user923005 <> wrote:

    >/*
    >OK, What am I missing? I do not see how undefined behavior comes into
    >play here.


    >I see nothing wrong with the program:
    >*/
    >#include <stdio.h>
    >int main(void) { printf("hello, world\n"); }


    >/*
    >5.1.2.2.3 Program termination
    >1 If the return type of the main function is a type compatible with
    >int, a return from the initial call to the main function is equivalent
    >to calling the exit function with the value returned by the main
    >function as its argument;10) reaching the } that terminates the main
    >function returns a value of 0.
    >*/


    That's the case for C99, but not for C89. This subthread proceeded
    from my pointing out the lack of return value being an issue for C89.
    --
    "I was very young in those days, but I was also rather dim."
    -- Christopher Priest
     
    Walter Roberson, Jan 12, 2008
    #15
  16. Mark McIntyre <> writes:
    [...]
    > UB is an aspect of /runtime/ not compile-time behaviour.


    Not entirely. C99 3.4.3p2:

    NOTE Possible undefined behavior ranges from ignoring the
    situation completely with unpredictable results, to behaving
    during translation or program execution in a documented manner
    characteristic of the environment (with or without the issuance of
    a diagnostic message), to terminating a translation or execution
    (with the issuance of a diagnostic message).

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jan 12, 2008
    #16
  17. "" <> writes:
    [...]
    > The lack of a return statement means /*return (void) expr;*/;


    If you mean that a missing return statement is equivalent to a
    comment, you're right. If you mean that it's equivalent to
    return (void) expr;
    then you're mistaken.

    > and the
    > function main() itself is type of non-void. The undefined behavior
    > definitively happens then. The whole program contains UB and may
    > cause demon fly out of your nose.


    In C99, falling off the end of main is perfectly legal, and equivalent
    to ``return 0;''. In C90, "the termination status returned to the
    host environment is undefined". This is not undefined behavior; if
    demons fly out of your nose, it was caused by the host environment,
    not by the program.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jan 12, 2008
    #17
  18. On Fri, 11 Jan 2008 22:01:39 -0500, CBFalconer wrote:
    > Harald van D?k wrote:
    >> Tomás Ó hÉilidhe wrote:
    >> And your message headers are still broken.

    >
    > How? I see nothing wrong, except (possibly) the char set for the
    > senders name.


    That's exactly it. Message headers must not contain any non-ASCII
    characters. If his name contains non-ASCII characters, it should be
    encoded. Even if it weren't invalid, there is no sane way for a
    newsreader to interpret his name when no encoding is specified. Tomás has
    been told this before.
     
    Harald van Dijk, Jan 12, 2008
    #18
  19. =?UTF-8?q?Harald_van_D=C4=B3k?= <> wrote in
    news:db401$47888992$541dfcd3$1.nb.home.nl:

    >> How? I see nothing wrong, except (possibly) the char set for the
    >> senders name.

    >
    > That's exactly it. Message headers must not contain any non-ASCII
    > characters. If his name contains non-ASCII characters, it should be
    > encoded. Even if it weren't invalid, there is no sane way for a
    > newsreader to interpret his name when no encoding is specified. Tomás
    > has been told this before.



    Harald, how do you write your own name for Usenet? It comes up as the
    following on my newsreader:

    =?UTF-8?q?Harald_van_D=C4=B3k?=

    It's not that I'm too lazy to sort the problem out, it's just that I don't
    know how to get my newsreader to use UTF-8 properly.

    --
    Tomás Ó hÉilidhe
     
    Tomás Ó hÉilidhe, Jan 12, 2008
    #19
  20. Kenny McCormack

    Richard Guest

    "Tomás Ó hÉilidhe" <> writes:

    > (Kenny McCormack) wrote in news:fm8ufo$hfj$1
    > @news.xmission.com:
    >
    >> #include <stdio.h>
    >> int main(void) { printf("hello, world\n"); }
    >>
    >> I compile it with all warning turned on and it still works fine.

    >
    >
    > Forgetting for the moment that you're a troll -- a troll who's actually
    > not half-bad at C programming, albeit, it's a shame you don't contribute
    > your expertise -- we both already know why the C standard permits that
    > program to do whatever it likes.
    >
    > I've plenty of friends that get a kick out of winding people up... but
    > to be honest they've got better, more entertaining ways than posting to a
    > newsgroup. Which is why I'm led to believe that you actually do have more
    > than a passing interest in C.
    >
    > I think you'll find that if you were to start posting something other
    > than tripe, that the regulars would welcome you with open arms. And no,
    > that's not an invitation to start a debate. In fact, as a precaution, I'm
    > going to killfile this thread right now.
    >
    > Best of luck to you in life though, nobody can deny that you're
    > creative and inventive, even if it's misplaced.


    Laughing at the blown up ego maniacs and lunchtime C experts in this NG
    is not a waste of time. It helps to remind newcomers that not every one
    here is here for their own publicity and to prance around acting like
    the big I am. I enjoy Kenny's posts. The likes of Heathfield and CBF
    need a Kenny to laugh at their pompous posturings.
     
    Richard, Jan 12, 2008
    #20
    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. F. GEIGER
    Replies:
    3
    Views:
    775
    F. GEIGER
    Aug 6, 2004
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    905
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,975
    Smokey Grindel
    Dec 2, 2006
  4. Alexander Burger

    getMethod() works and works not

    Alexander Burger, Nov 27, 2010, in forum: Java
    Replies:
    25
    Views:
    1,765
    Alexander Burger
    Nov 29, 2010
  5. abargaddon
    Replies:
    1
    Views:
    197
    clintmazur
    Feb 4, 2008
Loading...

Share This Page