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

Discussion in 'C Programming' started by sant.tarun@gmail.com, Mar 7, 2008.

  1. Guest

    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
    , Mar 7, 2008
    #1
    1. Advertising

  2. 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
    Ulrich Eckhardt, Mar 7, 2008
    #2
    1. Advertising

  3. On Thu, 6 Mar 2008 21:59:49 -0800 (PST), 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
    Barry Schwarz, Mar 7, 2008
    #3
  4. 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.

    x = 40 is an assignment expression, and its value is the same as the value
    taken by its left operand. So the value of (x = 40) is 40.

    x > 10 works the same way - i.e. relationally - as x < 30. That is, if the
    expression is false, the value yielded is 0. Otherwise, it's 1.

    Now that would suggest an output of 1 40 1, wouldn't it? But C doesn't
    specify the order in which function argument expressions are evaluated,
    and it would appear that on this occasion your implementation has decided
    to evaluate from right to left. Thus, x starts at 20, the x > 10 is then
    evaluated and yields 1 (which will be printed last), then x becomes 40,
    and then x is tested against 30 - since it's greater (because it has
    become 40), x < 30 is false, which gives 0. And that's why you're getting
    0 40 1 instead of 1 40 1.

    But in fact, you're breaking a C rule:

    "Between the previous and next sequence point an object shall have
    its stored value modified at most once by the evaluation of an
    expression. Furthermore, the prior value shall be accessed only to
    determine the value to be stored."

    There is no sequence point between any of the function argument
    evaluations, and you're fetching the value of x not only to modify it, but
    also to compare against other information. This isn't a syntax error or a
    constraint violation, so the compiler is not required to display a
    diagnostic message, but it nevertheless violates a "shall" clause, and
    therefore the behaviour of the program is undefined.

    The fix is as follows:

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

    C is such a flexible language that it's tempting to take too much advantage
    of that flexibility. Resist the temptation! :)

    --
    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
    Richard Heathfield, Mar 7, 2008
    #4
  5. jaysome Guest

    On Thu, 6 Mar 2008 21:59:49 -0800 (PST), wrote:

    >Hello,


    Hi Tarun.

    >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 ";" in this statement is a syntax error. I assume you meant ','
    instead of ';'.

    After fixing this error and encompassing the fixed code in a proper
    program:

    /*foo.c*/
    #include <stdio.h>
    int main(void)
    {
    int x = 20;
    printf("%d %d %d",x < 30, x = 40, x > 10);
    return 0;
    }

    I get the same output as you:

    0 40 1

    But this output being the same as yours is just a coincidence, because
    the code has undefined behavior. In the printf call, the variable x
    depends on the order of evaluation. I suspected this, and PC-lint [1]
    confirms it:

    foo.c(6) : Info 730: Boolean argument to function
    foo.c(6) : Warning 564: variable 'x' depends on order of evaluation

    The 564 warning identifies the undefined behavior.

    The 730 warning suggests a potential problem (though not really a
    problem in the instant case [2]), as explained by the PC-lint
    documentation:

    730 Boolean argument to function -- A Boolean was used as an
    argument to a function. Was this intended? Or was the
    programmer confused by a particularly complex conditional
    statement. Experienced C programmers often suppress this
    message. [snip C++-specific stuff]

    Best regards
    --
    jay

    [1]
    http://www.gimpel.com/

    [2]
    Depending on whom you ask
    jaysome, Mar 7, 2008
    #5
  6. Richard Guest

    Richard Heathfield <> writes:

    > 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.
    Richard, Mar 7, 2008
    #6
  7. "Richard" <> wrote:

    > Richard Heathfield <> writes:
    >
    > > 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
    Robbie Hatley, Mar 7, 2008
    #7
  8. santosh Guest

    Robbie Hatley wrote:

    >
    > "Richard" <> wrote:
    >
    >> Richard Heathfield <> writes:
    >>
    >> > 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.
    santosh, Mar 7, 2008
    #8
  9. "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.


    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
    Robbie Hatley, Mar 7, 2008
    #9
  10. On 7 Mar, 09:10, "Robbie Hatley" <> wrote:
    > "Richard" <> wrote:
    > > Richard Heathfield <> writes:

    >
    > > > 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
    Nick Keighley, Mar 7, 2008
    #10
  11. In article <fqr1fj$68v$>,
    santosh <> wrote:
    ....
    >> 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.
    Kenny McCormack, Mar 7, 2008
    #11
  12. santosh Guest

    Robbie Hatley wrote:

    >
    > "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.

    >
    > OOOPS!!! So *THAT'S* what you meant!


    That's what Richard Riley meant when he said "It won't actually."
    up-thread.

    <snip>
    santosh, Mar 7, 2008
    #12
  13. Robbie Hatley said:

    >
    > "Richard" <> wrote:
    >
    >> Richard Heathfield <> writes:
    >>

    <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
    Richard Heathfield, Mar 7, 2008
    #13
  14. In article <>,
    Richard Heathfield <> wrote:
    ....
    >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...
    Kenny McCormack, Mar 7, 2008
    #14
  15. 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.
    Antoninus Twink, Mar 7, 2008
    #15
  16. On 7 Mar 2008 at 10:05, Richard Heathfield wrote:
    > Robbie Hatley said:
    >
    >>
    >> "Richard" <> wrote:
    >>
    >>> Richard Heathfield <> writes:
    >>>

    ><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.
    Antoninus Twink, Mar 7, 2008
    #16
  17. Richard Guest

    Richard Heathfield <> writes:

    > Robbie Hatley said:
    >
    >>
    >> "Richard" <> wrote:
    >>
    >>> Richard Heathfield <> writes:
    >>>

    > <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?
    Richard, Mar 7, 2008
    #17
  18. Richard Guest

    santosh <> writes:

    > Robbie Hatley wrote:
    >
    >>
    >> "Richard" <> wrote:
    >>
    >>> Richard Heathfield <> writes:
    >>>
    >>> > 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.


    Getting something totally backwards is,of course, a mistake. There is no
    evil intent in my correction. Why you feel the need to confirm his
    "mistake" is very strange. Possibly you should inform Robbie to be less
    quick to tell people their compilers are broken and that they should get
    a new one?

    Which leads to the question "Why didn't you explain why Heathfield was
    wrong". And the answer is simple : I don't believe in treating people
    like idiots. Let them look and think the problem over themselves. They
    can always come back and say "you have me stumped", what is the issue?".

    A good C programmer and "team player" thinks for themselves. RH had said
    enough about the comparison expression for anyone with half a clue to
    see why he had made a mistake in his final analysis.

    But of course, giving credence and credit to other posters is becoming a
    rarer and rarer thing these days with posters like CBF riding in on
    their chargers at a moments notice.

    People in clc are far too eager to see other people wrong so that they
    can score some clique points by being rude and obnoxious at the first
    possible point. See the races to post OT rejoinders for a good example -
    when they KNOW that Default User or CBF have already beaten them to it.

    It's why I come here to be honest. It's fun to watch and relieves the
    boredom of a typical C programming day. And one learns something here -
    not always standard C related either. Fortunately there are more posters
    willing to "break topicality" and, err, talk about real C.
    Richard, Mar 7, 2008
    #18
  19. Richard Guest

    santosh <> writes:

    > Robbie Hatley wrote:
    >
    >>
    >> "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.

    >>
    >> OOOPS!!! So *THAT'S* what you meant!

    >
    > That's what Richard Riley meant when he said "It won't actually."
    > up-thread.


    Do you think? :-;
    Richard, Mar 7, 2008
    #19
  20. In article <fqrdd4$tll$>,
    Richard <> wrote:
    >santosh <> writes:
    >
    >> Robbie Hatley wrote:
    >>
    >>>
    >>> "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.
    >>>
    >>> OOOPS!!! So *THAT'S* what you meant!

    >>
    >> That's what Richard Riley meant when he said "It won't actually."
    >> up-thread.

    >
    >Do you think? :-;


    Some folks are a little slow on the uptake.

    But they do (sometimes) get it eventually.
    Kenny McCormack, Mar 7, 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. ben
    Replies:
    4
    Views:
    615
    Martin Ambuhl
    Jun 26, 2004
  2. aditya

    how the following printf statement works

    aditya, Oct 29, 2004, in forum: C Programming
    Replies:
    9
    Views:
    463
    Joona I Palaste
    Oct 29, 2004
  3. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,241
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    431
  5. guru
    Replies:
    8
    Views:
    281
Loading...

Share This Page