Re: How printf() works???????
- From: jaysome <jaysome@xxxxxxxxxxx>
- Date: Thu, 06 Mar 2008 22:51:30 -0800
On Thu, 6 Mar 2008 21:59:49 -0800 (PST), sant.tarun@xxxxxxxxx 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
.
- References:
- How printf() works???????
- From: sant . tarun
- How printf() works???????
- Prev by Date: Re: How printf() works???????
- Next by Date: Re: How printf() works???????
- Previous by thread: Re: How printf() works???????
- Next by thread: predict the answer
- Index(es):
Relevant Pages
|