Why this works???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kenny McCormack

    Why this works???

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

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


  • Tomás Ó hÉilidhe

    #2
    Re: Why this works???

    gazelle@xmissio n.xmission.com (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

    Comment

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

      #3
      Re: Why this works???

      On Sat, 12 Jan 2008 00:05:21 +0000, Tomás Ó hÉilidhe wrote:
      gazelle@xmissio n.xmission.com (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.

      Comment

      • Walter Roberson

        #4
        Re: Why this works???

        In article <dea6$47880837$ 541dfcd3$12327@ cache3.tilbu1.n b.home.nl>,
        =?UTF-8?q?Harald_van_ D=C4=B3k?= <truedfx@gmail. comwrote:
        >On Sat, 12 Jan 2008 00:05:21 +0000, Tomás Ó hÉilidhe wrote:
        >gazelle@xmissio n.xmission.com (Kenny McCormack) wrote in
        >news:fm8ufo$hf j$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

        Comment

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

          #5
          Re: Why this works???

          On Sat, 12 Jan 2008 00:36:29 +0000, Walter Roberson wrote:
          In article <dea6$47880837$ 541dfcd3$12327@ cache3.tilbu1.n b.home.nl>,
          =?UTF-8?q?Harald_van_ D=C4=B3k?= <truedfx@gmail. comwrote:
          >>On Sat, 12 Jan 2008 00:05:21 +0000, Tomás Ó hÉilidhe wrote:
          >>gazelle@xmissio n.xmission.com (Kenny McCormack) wrote in
          >>news:fm8ufo$h fj$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().

          Comment

          • Kenny McCormack

            #6
            Re: Why this works???

            In article <2ee2d$47880ddf $541dfcd3$669@c ache1.tilbu1.nb .home.nl>,
            Harald van Dijk <truedfx@gmail. comwrote:
            >On Sat, 12 Jan 2008 00:36:29 +0000, Walter Roberson wrote:
            >In article <dea6$47880837$ 541dfcd3$12327@ cache3.tilbu1.n b.home.nl>,
            >=?UTF-8?q?Harald_van_ D=C4=B3k?= <truedfx@gmail. comwrote:
            >>>On Sat, 12 Jan 2008 00:05:21 +0000, Tomás Ó hÉilidhe wrote:
            >>>gazelle@xmissio n.xmission.com (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...

            Comment

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

              #7
              Re: Why this works???

              On Sat, 12 Jan 2008 01:06:48 +0000, Kenny McCormack wrote:
              >>>>gazelle@xmissio n.xmission.com (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).

              Comment

              • user923005

                #8
                Re: Why this works???

                On Jan 11, 5:18 pm, Harald van D©¦k <true...@gmail. comwrote:
                On Sat, 12 Jan 2008 01:06:48 +0000, Kenny McCormack wrote:
                >>>gaze...@xmis sion.xmission.c om (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).
                */

                Comment

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

                  #9
                  Re: Why this works???

                  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:
                  >>>>>>gazelle@xmissio n.xmission.com (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.

                  Comment

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

                    #10
                    Re: Why this works???

                    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.

                    Comment

                    • lovecreatesbea...@gmail.com

                      #11
                      Re: Why this works???

                      On Jan 12, 9:06 am, gaze...@xmissio n.xmission.com (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.

                      Comment

                      • CBFalconer

                        #12
                        Re: Why this works???

                        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

                        Comment

                        • Walter Roberson

                          #13
                          Re: Why this works???

                          In article <7d9da3bb-b82e-4576-b60e-04cf69617200@m3 4g2000hsf.googl egroups.com>,
                          user923005 <dcorbit@connx. comwrote:
                          >/*
                          >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

                          Comment

                          • Keith Thompson

                            #14
                            Re: Why this works???

                            "lovecreatesbea ...@gmail.com" <lovecreatesbea uty@gmail.comwr ites:
                            [...]
                            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_Keit h) <kst-u@mib.org>
                            Nokia
                            "We must do something. This is something. Therefore, we must do this."
                            -- Antony Jay and Jonathan Lynn, "Yes Minister"

                            Comment

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

                              #15
                              Re: Why this works???

                              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.

                              Comment

                              Working...