Thursday 15 May 2014

c++ - Where should i initialiaze variables? -



c++ - Where should i initialiaze variables? -

hello question simple. best technique (standards, speed, readability etc...) initializing variables in let's simple program? example:

is improve (standards, speed, readability etc...)

int n1=0, n2=3;

or improve (standards, speed, readability etc...)

int n1, n2; n1=0; n2=3;

how (given fact variable values of course of study known?) have searched online teacher says there same in 3 categories thought should inquire experts here.

the reason opt approach #2 if forced utilize old c compiler, prohibits approach #1. should seek declaring variables in smallest scope create sense.

one exception when unconditionally assign value variable outer scope within inner scope, i.e.

int x; { x = next_int(); // x assigned unconditionally } while (x < 0); printf("%d\n", x); // x used after loop on

in situations ok not initialize x @ point of declaration.

note thought behind improve readability. execution speed not change, because optimizing compilers able eliminate unnecessary code.

c++ c variables initialization

No comments:

Post a Comment