c - Is this code undefined behaviour? -


i have variable i:

int i; if(b){     i=1; } else{     i=-1; } 

is i undefined behavior because of int i; exists? or should int i=0 first?

absolutely fine.

you initialising i on program control paths, , not reading value until initialisation complete.

i prefer using ternary operator in such instances.

int = b ? 1 : -1;

as that's less vulnerable accidental reference uninitialised i.


Comments

Popular posts from this blog

javascript - Trigger mouseenter when an animated element touches mouse -

json - Zend error Connection -

java - Using Spring @Transactional with a combination of readOnly and write, when does this entity get committed? -