Talk About Network

Google





Programming > C - C++ Learning > Re: [C and C++]...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 8 of 12 Topic 4095 of 4400
Post > Topic >>

Re: [C and C++] && Operator precedence

by Philip Potter <pgp@[EMAIL PROTECTED] > Apr 7, 2008 at 11:12 PM

Aggro wrote:
> I would like to know the answer to this for both C and C++ as I use both

> languages (usually in differen projects).
> 
> Assume the code
> if( A && B )
> 
> Normally A is executed before B and this is how I have always believed 
> it is guaranteed to work. But recently I got second hand information 
> that the execution order is not quaranteed. E.g. B could be executed 
> before A by some compilers.
> 
> Different execution order could cause e.g. application to crash if we 
> first test is the pointer valid and then call a function for that
pointer:
> if( p && p->func() )
> 
> 
> So. Is the execution order guaranteed by the standard in both C and C++ 
> to be first A and then B or can it change e.g. because of optimization? 
> Should I start writing my code in this format to be safe?
> 
> if( p )
>    if( p->func() )
>      ...

Ulrich has answered your question; in C (and probably C++, though I'm
not a C++ expert) both && and || are guaranteed to be executed
left-to-right; and if the result can be generated based on the LHS
alone, the RHS is guaranteed not to be evaluated. Moreover, && and ||
introduce a sequence point between LHS and RHS, so expressions such as
(p && x = *p++) are valid (though not recommended).

Few operators (except ?:, the comma operator, and probably one or two
I've forgotten) have similar semantics. The order of evaluation of the
arguments of most operators is unspecified; in particular, f(x) * 0 and
0 * f(x) may (will?) call f() in either case, even though the result
will always be zero. Similarly A & 0 and 0 & A have no guarantees over
order of evaluation and no sequence points. And finally, x++ * x++ is
undefined behaviour, even though x++ || x++ or x++ && x++ are
well-defined.

Only && and || are "magical" in this way.

Phil
 




 12 Posts in Topic:
[C and C++] && Operator precedence
Aggro <spammerdream@[E  2008-04-07 20:24:45 
Re: [C and C++] && Operator precedence
Ulrich Eckhardt <dooms  2008-04-07 22:34:34 
Re: [C and C++] && Operator precedence
Hal Vaughan <hal@[EMAI  2008-04-08 00:20:31 
Re: [C and C++] && Operator precedence
Jack Klein <jackklein@  2008-04-07 21:48:22 
Re: [C and C++] && Operator precedence
Philip Potter <pgp@[EM  2008-04-08 08:55:35 
Re: && Operator precedence
=?ISO-8859-1?Q?Andr=E9_Ca  2008-04-08 04:06:10 
Re: && Operator precedence
Philip Potter <pgp@[EM  2008-04-08 13:21:46 
Re: [C and C++] && Operator precedence
Philip Potter <pgp@[EM  2008-04-07 23:12:16 
Re: [C and C++] && Operator precedence
Francis Glassborow <fr  2008-04-07 23:42:49 
Re: [C and C++] && Operator precedence
Anand Hariharan <znvyg  2008-04-08 04:48:45 
Re: [C and C++] && Operator precedence
Francis Glassborow <fr  2008-04-08 09:15:35 
Re: [C and C++] && Operator precedence
Anand Hariharan <znvyg  2008-04-08 16:00:12 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
localhost-V2008-12-19 Wed Jan 7 11:34:49 PST 2009.