Home > Mobile >  Why does the array printing by not considering the positioin?
Why does the array printing by not considering the positioin?

Time:05-04

I am triying to find the output but it is not what I expected. Here is the code.

void main(void){
int var[2] = { 1,2 }; 
int car = 0; 
if (car = var[1] - 1) printf("YES"); 

else  printf("NO");

I thought that it would print No because var[1] is 2 and 2-1 = 1 != 0. But it always print yes. What happened to this code?

CodePudding user response:

== means equality, = means assignment.

if (car == var[2] - 1) printf("YES");

CodePudding user response:

var[2] - 1 is invalid as var[2] does not exist - undefined behavior.

  • Related