Click here to Skip to main content
15,891,856 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
why k variable can not be increase in the given program?

What I have tried:

#include <stdio.h>
void main()
{
int k = 8;
int x = 0 == 1 && k++;
printf("%d %d\n", x, k);
}
OUTPUT:0 8
Posted
Updated 24-Dec-17 2:33am

This is because of a phenomenon called short-circuiting. This means that the right-hand of the && operator will not be evaluated if the result is already decided from the left-hand. 0 == 1 is 0/false. So, it doesn't matter what the right-hand (k++) would evaluate to, the final result is always 0/false (false && true is false, just like false && false). This means that the right-hand k++ never gets evaluated, so k doesn't get incremented.
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900