How printf() works???????

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sant.tarun@gmail.com

    How printf() works???????


    Hello,

    I would appreciate some comments about the piece of code given below
    and explanation about the result of this specified code.

    int x = 20;

    printf("%d %d %d",x < 30, x = 40; x 10);


    The output of these code will be :: 0 40 1

    Thanks and Regards,
    Tarun
  • Ulrich Eckhardt

    #2
    Re: How printf() works???????

    sant.tarun@gmai l.com wrote:
    I would appreciate some comments about the piece of code given below
    and explanation about the result of this specified code.
    >
    int x = 20;
    >
    printf("%d %d %d",x < 30, x = 40; x 10);
    It doesn't compile.
    The output of these code will be :: 0 40 1
    No, see above.

    Uli

    Comment

    • Barry Schwarz

      #3
      Re: How printf() works???????

      On Thu, 6 Mar 2008 21:59:49 -0800 (PST), sant.tarun@gmai l.com wrote:
      >
      >Hello,
      >
      >I would appreciate some comments about the piece of code given below
      >and explanation about the result of this specified code.
      >
      >int x = 20;
      >
      >printf("%d %d %d",x < 30, x = 40; x 10);
      >
      >
      >The output of these code will be :: 0 40 1
      >
      The code invokes undefined behavior. It modifies x and it evaluates x
      more than once and also for a purpose other than determining the new
      value. Either condition causes undefined behavior. Therefore,
      depending on your frame of mind, any output you receive is correct or
      there is no correct output from the program.


      Remove del for email

      Comment

      • Richard

        #4
        Re: How printf() works???????

        Richard Heathfield <rjh@see.sig.in validwrites:
        sant.tarun@gmai l.com said:
        >
        >>
        >Hello,
        >>
        >I would appreciate some comments about the piece of code given below
        >and explanation about the result of this specified code.
        >>
        >int x = 20;
        >>
        >printf("%d %d %d",x < 30, x = 40; x 10);
        >>
        >>
        >The output of these code will be :: 0 40 1
        >
        Well, it might be, once you wrap a program around it and fix the syntax
        error (a semicolon instead of a comma). First, I'll explain why you might
        get that output, and then I'll explain why you might not.
        >
        x < 30 is a relational expression, and all expressions have values.
        Relational expressions have the value 0 if they're false, and 1 if they're
        true. So x < 30 will evaluate to 0 if x is less than 30, and 1
        otherwise.
        It won't actually.

        Comment

        • Robbie Hatley

          #5
          Re: How printf() works???????


          "Richard" <devr_@gmail.co mwrote:
          Richard Heathfield <rjh@see.sig.in validwrites:
          >
          sant.tarun@gmai l.com said:
          >
          Hello,
          >
          I would appreciate some comments about the piece of code given below
          and explanation about the result of this specified code.
          >
          int x = 20;
          >
          printf("%d %d %d",x < 30, x = 40; x 10);
          >
          >
          The output of these code will be :: 0 40 1
          Well, it might be, once you wrap a program around it and fix the syntax
          error (a semicolon instead of a comma). First, I'll explain why you might
          get that output, and then I'll explain why you might not.

          x < 30 is a relational expression, and all expressions have values.
          Relational expressions have the value 0 if they're false, and 1 if they're
          true. So x < 30 will evaluate to 0 if x is less than 30, and 1
          otherwise.
          >
          It won't actually.
          How do you figure? It does in standard C. If it doesn't for you,
          then your compiler is broken and you should get a better one.

          --
          Cheers,
          Robbie Hatley
          lonewolf aatt well dott com
          www dott well dott com slant user slant lonewolf slant


          Comment

          • santosh

            #6
            Re: How printf() works???????

            Robbie Hatley wrote:
            >
            "Richard" <devr_@gmail.co mwrote:
            >
            >Richard Heathfield <rjh@see.sig.in validwrites:
            >>
            sant.tarun@gmai l.com said:
            >
            >>
            >Hello,
            >>
            >I would appreciate some comments about the piece of code given
            >below and explanation about the result of this specified code.
            >>
            >int x = 20;
            >>
            >printf("%d %d %d",x < 30, x = 40; x 10);
            >>
            >>
            >The output of these code will be :: 0 40 1
            >
            Well, it might be, once you wrap a program around it and fix the
            syntax error (a semicolon instead of a comma). First, I'll explain
            why you might get that output, and then I'll explain why you might
            not.
            >
            x < 30 is a relational expression, and all expressions have values.
            Relational expressions have the value 0 if they're false, and 1 if
            they're true. So x < 30 will evaluate to 0 if x is less than 30,
            and 1 otherwise.
            >>
            >It won't actually.
            >
            How do you figure? It does in standard C. If it doesn't for you,
            then your compiler is broken and you should get a better one.
            The expression x < 30 will evaluate to 1 if x is less than 30 and zero
            otherwise. Richard said the reverse, by mistake I'm sure.

            Comment

            • Robbie Hatley

              #7
              Re: How printf() works???????


              "santosh" <santosh.k83@gm ail.comwrote:
              The expression x < 30 will evaluate to 1 if x is less than 30
              and zero otherwise. Richard said the reverse, by mistake I'm sure.
              OOOPS!!! So *THAT'S* what you meant! I didn't even notice.
              I looked right at what Richard Heathfield wrote several times,
              and I didn't see the error. I guess my mind saw what it wanted
              to see.

              --
              Cheers,
              Robbie Hatley
              lonewolf aatt well dott com
              www dott well dott com slant user slant lonewolf slant


              Comment

              • Nick Keighley

                #8
                Re: How printf() works???????

                On 7 Mar, 09:10, "Robbie Hatley" <lonew...@well. comwrote:
                "Richard" <de...@gmail.co mwrote:
                Richard Heathfield <r...@see.sig.i nvalidwrites:
                >
                sant.ta...@gmai l.com said:
                >
                >Hello,
                >
                >I would appreciate some comments about the piece of code given below
                >and explanation about the result of this specified code.
                >
                >int x = 20;
                >
                >printf("%d %d %d",x < 30, x = 40; x 10);
                >
                >The output of these code will be :: 0 40 1
                >
                Well, it might be, once you wrap a program around it and fix the syntax
                error (a semicolon instead of a comma). First, I'll explain why you might
                get that output, and then I'll explain why you might not.
                >
                x < 30 is a relational expression, and all expressions have values.
                Relational expressions have the value 0 if they're false, and 1 if they're
                true. So x < 30 will evaluate to 0 if x is less than 30, and 1
                otherwise.
                >
                It won't actually.
                >
                How do you figure?  It does in standard C.  If it doesn't for you,
                then your compiler is broken and you should get a better one.

                Richard Heathfield made a typo. He meant "So x < 30 will evaluate to
                *1* if x
                is less than 30, and *0* otherwise."



                --
                Nick Keighley



                Comment

                • Kenny McCormack

                  #9
                  Re: How printf() works???????

                  In article <fqr1fj$68v$1@r egistered.motza rella.org>,
                  santosh <santosh.k83@gm ail.comwrote:
                  ....
                  >How do you figure? It does in standard C. If it doesn't for you,
                  >then your compiler is broken and you should get a better one.
                  >
                  >The expression x < 30 will evaluate to 1 if x is less than 30 and zero
                  >otherwise. Richard said the reverse, by mistake I'm sure.
                  Yes, it was a mistake.

                  Comment

                  • santosh

                    #10
                    Re: How printf() works???????

                    Robbie Hatley wrote:
                    >
                    "santosh" <santosh.k83@gm ail.comwrote:
                    >
                    >The expression x < 30 will evaluate to 1 if x is less than 30
                    >and zero otherwise. Richard said the reverse, by mistake I'm sure.
                    >
                    OOOPS!!! So *THAT'S* what you meant!
                    That's what Richard Riley meant when he said "It won't actually."
                    up-thread.

                    <snip>

                    Comment

                    • Richard Heathfield

                      #11
                      Re: How printf() works???????

                      Robbie Hatley said:
                      >
                      "Richard" <devr_@gmail.co mwrote:
                      >
                      >Richard Heathfield <rjh@see.sig.in validwrites:
                      >>
                      <snip>
                      >
                      x < 30 is a relational expression, and all expressions have values.
                      Relational expressions have the value 0 if they're false, and 1 if
                      they're true. So x < 30 will evaluate to 0 if x is less than 30, and 1
                      otherwise.
                      >>
                      >It won't actually.
                      >
                      How do you figure?
                      No, he's absolutely right, and it was a good spot. I meant to write: "So x
                      < 30 will evaluate to 1 if x is less than 30, and 0 otherwise", but
                      obviously I failed to achieve that objective. If I'd seen his original
                      correction, I'd have acknowledged it. Since he's in the ol' bozo bin,
                      however, it escaped my notice until I saw your reply.

                      And with that one useful article, his signal/noise ratio has just jumped
                      about a thousand percent. Unfortunately, that isn't particularly difficult
                      to achieve. But if he spent half, or even quarter, of his articles helping
                      people instead of carping and sniping, it might even become worth reading
                      them.

                      --
                      Richard Heathfield <http://www.cpax.org.uk >
                      Email: -http://www. +rjh@
                      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                      "Usenet is a strange place" - dmr 29 July 1999

                      Comment

                      • Kenny McCormack

                        #12
                        Re: How printf() works???????

                        In article <hLWdnXOAGZbuj0 zanZ2dnUVZ8s6in Z2d@bt.com>,
                        Richard Heathfield <rjh@see.sig.in validwrote:
                        ....
                        >And with that one useful article, his signal/noise ratio has just jumped
                        >about a thousand percent. Unfortunately, that isn't particularly difficult
                        >to achieve. But if he spent half, or even quarter, of his articles helping
                        >people instead of carping and sniping, it might even become worth reading
                        >them.
                        Oh. The. Irony...

                        Comment

                        • Antoninus Twink

                          #13
                          Re: How printf() works???????

                          On 7 Mar 2008 at 9:21, santosh wrote:
                          The expression x < 30 will evaluate to 1 if x is less than 30 and zero
                          otherwise. Richard said the reverse, by mistake I'm sure.
                          No, I don't think so. Richard HeathField never makes mistakes, as he
                          often tells us.

                          Comment

                          • Antoninus Twink

                            #14
                            Re: How printf() works???????

                            On 7 Mar 2008 at 10:05, Richard Heathfield wrote:
                            Robbie Hatley said:
                            >
                            >>
                            >"Richard" <devr_@gmail.co mwrote:
                            >>
                            >>Richard Heathfield <rjh@see.sig.in validwrites:
                            >>>
                            ><snip>
                            >>
                            >x < 30 is a relational expression, and all expressions have values.
                            >Relational expressions have the value 0 if they're false, and 1 if
                            >they're true. So x < 30 will evaluate to 0 if x is less than 30, and 1
                            >otherwise.
                            >>>
                            >>It won't actually.
                            >>
                            >How do you figure?
                            >
                            No, he's absolutely right, and it was a good spot. I meant to write: "So x
                            >< 30 will evaluate to 1 if x is less than 30, and 0 otherwise", but
                            obviously I failed to achieve that objective. If I'd seen his original
                            correction, I'd have acknowledged it. Since he's in the ol' bozo bin,
                            however, it escaped my notice until I saw your reply.
                            Amazing - you can't bring yourself to admit that you screwed up, but
                            dress it up in this absurd pomposity we've come to expect: "I failed to
                            achieve the objective of saying something true". FFS.
                            And with that one useful article, his signal/noise ratio has just
                            jumped about a thousand percent. Unfortunately, that isn't
                            particularly difficult to achieve. But if he spent half, or even
                            quarter, of his articles helping people instead of carping and
                            sniping, it might even become worth reading them.
                            Richard Riley has a long history of useful contributions to this group.
                            Your posting history, on the other hand, reveals a long history of nasty
                            snipes against Jacob, and complaints about topicality and netiquette. Go
                            figure.

                            Comment

                            • Richard

                              #15
                              Re: How printf() works???????

                              Richard Heathfield <rjh@see.sig.in validwrites:
                              Robbie Hatley said:
                              >
                              >>
                              >"Richard" <devr_@gmail.co mwrote:
                              >>
                              >>Richard Heathfield <rjh@see.sig.in validwrites:
                              >>>
                              <snip>
                              >>
                              >x < 30 is a relational expression, and all expressions have values.
                              >Relational expressions have the value 0 if they're false, and 1 if
                              >they're true. So x < 30 will evaluate to 0 if x is less than 30, and 1
                              >otherwise.
                              >>>
                              >>It won't actually.
                              >>
                              >How do you figure?
                              >
                              No, he's absolutely right, and it was a good spot. I meant to write: "So x
                              < 30 will evaluate to 1 if x is less than 30, and 0 otherwise", but
                              obviously I failed to achieve that objective. If I'd seen his original
                              correction, I'd have acknowledged it. Since he's in the ol' bozo bin,
                              however, it escaped my notice until I saw your reply.
                              >
                              And with that one useful article, his signal/noise ratio has just jumped
                              about a thousand percent. Unfortunately, that isn't particularly difficult
                              to achieve. But if he spent half, or even quarter, of his articles helping
                              people instead of carping and sniping, it might even become worth reading
                              them.
                              This why you see my posts as "carping and sniping". I think you and the
                              some of the regs make FAR more noise than I. I feel bringing attention
                              to some of the more outrageous posts here is a little light relief from
                              the pain of having to read some of the posing that goes on in here.

                              Robbie's reply is a great example.

                              Fly into "C God mode" and insult someone.

                              And yes "your compiler is broken and you should get another one" is
                              indeed playing the smart alek.

                              Where are all these "broken C compiler" that regs keep referring to in
                              here?

                              How does the world spin without the clc regs keeping it in check?

                              Comment

                              Working...