Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   How printf() works??????? (http://www.velocityreviews.com/forums/t596694-how-printf-works.html)

sant.tarun@gmail.com 03-07-2008 05:59 AM

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 03-07-2008 06:11 AM

Re: How printf() works???????
 
sant.tarun@gmail.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


Barry Schwarz 03-07-2008 06:32 AM

Re: How printf() works???????
 
On Thu, 6 Mar 2008 21:59:49 -0800 (PST), sant.tarun@gmail.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

Richard Heathfield 03-07-2008 06:36 AM

Re: How printf() works???????
 
sant.tarun@gmail.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.

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

jaysome 03-07-2008 06:51 AM

Re: How printf() works???????
 
On Thu, 6 Mar 2008 21:59:49 -0800 (PST), sant.tarun@gmail.com 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








Richard 03-07-2008 07:01 AM

Re: How printf() works???????
 
Richard Heathfield <rjh@see.sig.invalid> writes:

> sant.tarun@gmail.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.

Robbie Hatley 03-07-2008 09:10 AM

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

"Richard" <devr_@gmail.com> wrote:

> Richard Heathfield <rjh@see.sig.invalid> writes:
>
> > sant.tarun@gmail.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



santosh 03-07-2008 09:21 AM

Re: How printf() works???????
 
Robbie Hatley wrote:

>
> "Richard" <devr_@gmail.com> wrote:
>
>> Richard Heathfield <rjh@see.sig.invalid> writes:
>>
>> > sant.tarun@gmail.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.


Robbie Hatley 03-07-2008 09:42 AM

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

"santosh" <santosh.k83@gmail.com> 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



Nick Keighley 03-07-2008 09:46 AM

Re: How printf() works???????
 
On 7 Mar, 09:10, "Robbie Hatley" <lonew...@well.com> wrote:
> "Richard" <de...@gmail.com> wrote:
> > Richard Heathfield <r...@see.sig.invalid> writes:

>
> > > sant.ta...@gmail.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





All times are GMT. The time now is 02:02 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.