evalutation order for logical operators

G

gallows

Hi,

I wondering if there is a standard specification for evalutation of
logical expressions. For instance:

if (p != NULL && strlen(p) > x )
do_something();

If p is actually NULL, should this code (built with a generic
compiler) crash?
Considering that the first element is FALSE, evalutation of strlen()
is useless, so it (theoretically) should be skipped...
My compiler (gcc) works as I expected, is it a standard behaviour?

Thanks,

s.
 
P

pete

gallows said:
Hi,

I wondering if there is a standard specification for evalutation of
logical expressions.

N869
6.5.13 Logical AND operator
[#4] Unlike the bitwise binary & operator, the && operator
guarantees left-to-right evaluation; there is a sequence
point after the evaluation of the first operand. If the
first operand compares equal to 0, the second operand is not
evaluated.
For instance:

if (p != NULL && strlen(p) > x )
do_something();

If p is actually NULL, should this code (built with a generic
compiler) crash?
No.

Considering that the first element is FALSE, evalutation of strlen()
is useless, so it (theoretically) should be skipped...
My compiler (gcc) works as I expected, is it a standard behaviour?

Yes.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top