for(;0;) printf("hello");

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

    #16
    Re: for(;0;) printf("he llo");

    Spidey wrote:
    >
    am using turbo c3 n it wont put up any warning.
    and the code produced the hello output on my screen
    What is it? What code? What is turbo c3 n? Why are the wonts of
    'it' of interest? (Possibly you meant won't?) Without context your
    post is meaningless.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home .att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
    Warning: Do not use Ultimate-Anonymity
    They are worthless spammers that are running a scam.

    Comment

    • Richard Heathfield

      #17
      Re: for(;0;) printf(&quot;he llo&quot;);

      Bill Pursell said:
      >
      Richard Heathfield wrote:
      >Simon Biber said:
      >>
      Richard Heathfield wrote:
      >>
      >Y h n p e c.
      >
      "You have not posted entire code."?
      >>
      >Not what I had in mind, although you did get the first three words right.
      >
      "You have not provided enough context" ?
      Gv th mn a bnn!

      --
      Richard Heathfield
      "Usenet is a strange place" - dmr 29/7/1999

      email: rjh at above domain (but drop the www, obviously)

      Comment

      • Richard Heathfield

        #18
        Re: for(;0;) printf(&quot;he llo&quot;);

        Frederick Gotham said:
        Simon Biber posted:
        >
        >#include <stdio.h>
        >>
        >int main(void)
        >{
        > for(;0;)
        > printf("hello") ;
        >>
        > return 0;
        >}
        >>
        >I tried it on 4 different compilers and none of them printed hello on
        >the screen. One of them warned about unreachable code.
        >
        >
        Lookup how a "for" loop works and all will become clear.
        Simon knows how a for-loop works.
        HINT: The conditional to be tested in the above snippet is always false.
        Did you actually /read/ the original article in this thread?

        --
        Richard Heathfield
        "Usenet is a strange place" - dmr 29/7/1999

        email: rjh at above domain (but drop the www, obviously)

        Comment

        • Richard Heathfield

          #19
          Re: for(;0;) printf(&quot;he llo&quot;);

          Simon Biber said:

          <snip>
          >
          #include <stdio.h>
          >
          int main(void)
          {
          for(;0;)
          printf("hello") ;
          >
          return 0;
          }
          >
          I tried it on 4 different compilers and none of them printed hello on
          the screen. One of them warned about unreachable code.
          On reflection, and after entering into mind-reader mode, I think I know why
          the OP is getting the result that he is in fact getting; if I am right, he
          hasn't pasted the code he actually compiled; he's typed, from scratch, the
          code he /thought/ he compiled; the style of this paragraph will, I think,
          indicate to you what I think the problem is.

          --
          Richard Heathfield
          "Usenet is a strange place" - dmr 29/7/1999

          email: rjh at above domain (but drop the www, obviously)

          Comment

          • Keith Thompson

            #20
            Re: for(;0;) printf(&quot;he llo&quot;);

            Richard Heathfield <invalid@invali d.invalidwrites :
            [...]
            Gv th mn a bnn!
            Give the man a bunny?

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
            We must do something. This is something. Therefore, we must do this.

            Comment

            • Spiros Bousbouras

              #21
              Re: for(;0;) printf(&quot;he llo&quot;);

              Keith Thompson wrote:
              Richard Heathfield <invalid@invali d.invalidwrites :
              [...]
              Gv th mn a bnn!
              >
              Give the man a bunny?
              Banana !

              Comment

              • Keith Thompson

                #22
                Re: for(;0;) printf(&quot;he llo&quot;);

                "Spidey" <amalhashim@gma il.comwrites:
                for(;0;)
                printf("hello") ;
                >
                even the condition s wrong i get a hello printed on the screen
                y s this happening
                Your problem is that you don't yet understand how this newsgroup works.

                Please don't use silly abbreviations. This is a technical discusssion
                forum, not a chat room. Most of us can guess that "s" means "is" and
                "y" means "why", but not everyone here has English as a first
                language, and those of us who do have better things to do with our
                time than figuring out what your abbreviations mean. (That's what
                people have been trying to tell you with responses like
                "Y h n p e c.".) If you want our help, take the time to write plain
                English. (We won't complain about minor spelling and grammatical
                errors.) Proper capitalization and punctuation are also very helpful.

                What you *should* have written was something like this:

                Even the condition is wrong. I get a "hello" printed on the
                screen. Why is this happening?

                (The first sentence is still unclear, but at least we can understand
                the words.)

                If you post a followup, provide context. You need to quote the article
                you're replying to, or at least enough of it so that your followup
                makes sense to someone who hasn't seen the previous article (but
                delete anything that's not relevant to your reply). The Google Groups
                interface does this for you. <http://cfaj.freeshell. org/google/has
                some good links. See also <http://www.caliburn.nl/topposting.html >.
                And see most of the articles in this newsgroup for examples of how
                to do it right.

                If you post a code sample, don't try to re-type it; copy and paste the
                *exact* code that you compiled and executed. We can't tell which
                errors were in your original code and which were introduced when you
                transcribed it; don't ask us to guess. If at all possible, post a
                small complete program that we can try, not just a code fragment.
                Very often the actual error is not where you think it is. (If you
                knew where the error was, you wouldn't have to ask us.)

                I'm going to make a guess about what your problem is. You posted
                this:

                for(;0;)
                printf("hello") ;

                but your program really contains this:

                for(;0;);
                printf("hello") ;

                The semicolon on the first line indicates a null statement, which is
                the statement controlled by the for loop. The printf() call follows
                the for loop; it's not part of it. So you do nothing zero times, then
                call printf() unconditionally .

                (Why would you write "for(;0;)" anyway? There are easier ways to do
                nothing.)

                Recommended reading:




                --
                Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                We must do something. This is something. Therefore, we must do this.

                Comment

                • Keith Thompson

                  #23
                  Re: for(;0;) printf(&quot;he llo&quot;);

                  "Spiros Bousbouras" <spibou@gmail.c omwrites:
                  Keith Thompson wrote:
                  >
                  >Richard Heathfield <invalid@invali d.invalidwrites :
                  >[...]
                  Gv th mn a bnn!
                  >>
                  >Give the man a bunny?
                  >
                  Banana !
                  Give the banana a bunny?

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                  San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                  We must do something. This is something. Therefore, we must do this.

                  Comment

                  • Michal Nazarewicz

                    #24
                    Re: for(;0;) printf(&quot;he llo&quot;);

                    Frederick Gotham <fgothamNO@SPAM .comwrites:
                    Michal Nazarewicz posted:
                    >You have put ';' after ')' and haven't noticed it?
                    >
                    I see nothing wrong with the code fragment:
                    >
                    for(;0;)
                    printf("hello") ;
                    Nighter do I but who can guarantee that OP compiled this code? If you
                    look at indention it may seem that he has compiled something
                    completely different which correctly produces /hello/.

                    Seems like Richard Heathfield came to the same conclusion.

                    --
                    Best regards, _ _
                    .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
                    ..o | Computer Science, Michal "mina86" Nazarewicz (o o)
                    ooo +--<mina86*tlen.pl >--<jid:mina86*jab ber.org>--ooO--(_)--Ooo--

                    Comment

                    • Scorpio

                      #25
                      Re: for(;0;) printf(&quot;he llo&quot;);

                      Keith Thompson wrote:
                      "Spidey" <amalhashim@gma il.comwrites:
                      for(;0;)
                      printf("hello") ;

                      even the condition s wrong i get a hello printed on the screen
                      y s this happening
                      >
                      [snip]
                      I'm going to make a guess about what your problem is. You posted
                      this:
                      >
                      for(;0;)
                      printf("hello") ;
                      >
                      but your program really contains this:
                      >
                      for(;0;);
                      printf("hello") ;
                      No. His program does contain the code he has provided here (without the

                      semicolon at the end of for statement). That is actually a compiler bug
                      of
                      Turbo C++ 3.0. Its big time he got an upgrade from that compiler.

                      To OP(Spidey):
                      Turbo C++ has released a new compiler recently and its for free, so,
                      get an
                      upgrade and your code will work fine. Its available from
                      Free Turbo Backgrounds for Zoom, Microsoft Teams, Google Meet and presentations. High-resolution, no watermark, instant download.


                      Comment

                      • Simon Biber

                        #26
                        Re: for(;0;) printf(&quot;he llo&quot;);

                        Spidey wrote:
                        am using turbo c3 n it wont put up any warning.
                        and the code produced the hello output on my screen
                        Turbo C is known to be buggy.

                        Here is a program that exposes bugs on many compilers:

                        #include <stdio.h>

                        int main(void)
                        {
                        int arr[5];
                        printf("%d\n", (int)sizeof arr);
                        printf("%d\n", (int)sizeof &arr);
                        return 0;
                        }

                        The size of &arr should be the size of a pointer, probably just 2, 4 or
                        8 bytes. Many compilers miss the & and instead give the same value as
                        the size of arr, a multiple of 5.

                        Turbo C 2.01: 10, 10 (wrong)
                        Borland C/C++ 5.5: 20, 20 (wrong)
                        MS cl 13.10.3077: 20, 20 (wrong)
                        LCC-Win32 3.8: 20, 4 (right)
                        GCC 3.4.4: 20, 4 (right)

                        Not a good showing for Borland or Microsoft. Even when invoked in
                        "standard-conforming" mode, they fail this basic C89 test.

                        --
                        Simon.

                        Comment

                        • Simon Biber

                          #27
                          Re: for(;0;) printf(&quot;he llo&quot;);

                          Spidey wrote:
                          am using turbo c3 n it wont put up any warning.
                          and the code produced the hello output on my screen
                          C:\docs\prog\c> type for0.c
                          #include <stdio.h>

                          int main(void)
                          {
                          for(;0;)
                          printf("hello") ;

                          return 0;
                          }

                          C:\docs\prog\c> tcc for0.c
                          Turbo C Version 2.01 Copyright (c) 1987, 1988 Borland International
                          for0.c:
                          Warning for0.c 6: Unreachable code in function main
                          Turbo Link Version 2.0 Copyright (c) 1987, 1988 Borland International

                          Available memory 392912

                          C:\docs\prog\c> for0


                          As you can see, it does put up a warning, and it does not produce hello
                          on my screen. It works perfectly. This code does not expose a bug in the
                          compiler (but see my other article which gives an example that does
                          expose a bug).

                          You must not be compiling the same code as me.

                          --
                          Simon.

                          Comment

                          • Igmar Palsenberg

                            #28
                            Re: for(;0;) printf(&quot;he llo&quot;);

                            Spidey wrote:
                            am using turbo c3 n it wont put up any warning.
                            and the code produced the hello output on my screen
                            Get a decent compiler, and until you do, *please* don't post questions.


                            Igmar

                            Comment

                            • jmcgill

                              #29
                              Re: for(;0;) printf(&quot;he llo&quot;);

                              Simon Biber wrote:
                              for(;0;)
                              printf("hello") ;
                              Would it kill you to use curly braces?

                              Comment

                              • Joe Wright

                                #30
                                Re: for(;0;) printf(&quot;he llo&quot;);

                                jmcgill wrote:
                                Simon Biber wrote:
                                >
                                > for(;0;)
                                > printf("hello") ;
                                >
                                Would it kill you to use curly braces?
                                To what end? Add clutter?

                                Constructs like..

                                for (;0;) {
                                printf("world") ;
                                }

                                ...add no special readability for me (except the indent of course).
                                --
                                Joe Wright
                                "Everything should be made as simple as possible, but not simpler."
                                --- Albert Einstein ---

                                Comment

                                Working...