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

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -