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

S

sant.tarun

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
 
U

Ulrich Eckhardt

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
 
B

Barry Schwarz

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
 
R

Richard Heathfield

(e-mail address removed) 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! :)
 
J

jaysome


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
 
R

Richard

Richard Heathfield said:
(e-mail address removed) said:


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

Robbie Hatley

Richard said:
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.
 
S

santosh

Robbie said:
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.
 
R

Robbie Hatley

santosh said:
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.
 
N

Nick Keighley

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

Kenny McCormack

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.[/QUOTE]

Yes, it was a mistake.
 
R

Richard Heathfield

Robbie Hatley said:
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.
 
K

Kenny McCormack

Richard Heathfield said:
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...
 
A

Antoninus Twink

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

Antoninus Twink

Robbie Hatley said:


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

Richard

Richard Heathfield said:
Robbie Hatley said:


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?
 
R

Richard

santosh said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top