int a=-3,b=2,c=0,d;
d=++a&&++b||++c;
printf("a=%d,b=%d,c=%d,d=%d",a,b,c,d);

why value of 'c' is not incremented in output?

Recommended Answers

All 2 Replies

Because a and b were successfully incrementented? You have putten an 'conditional or' operator between the two statements. Although I am not an C expert, usually, when the first part appears to be true the second part will not performed at all.

Correct C#Jaap, the logical and (&&) and or (||) operators use shortcut evaluation, that is if they can determin the result after evaluating the left hand expression they do not bother evaulating the right hand expression. For && this means if the left hand expression is false the right hand expression is not evaluated and for || this means if the left hand expression is true the right hand expression is not evaluated.

It means things like you can safely test a pointer and dereference it in the same if statement

if (valid != 0 && (*valid) != 0)
{
  // Its valid
}
else
{
  // Its not valid or valid is NULL which is also probably not valid
}   
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.