Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring and initializing a variable in a Conditional or Control statement in C++

In Stroustrup's The C++ Programming Language: Special Edition (3rd Ed), Stroustrup writes that the declaration and initialization of variables in the conditionals of control statements is not only allowed, but encouraged. He writes that he encourages it because it reduces the scope of the variables to only the scope that they are required for. So something like this...

if ((int i = read(socket)) < 0) {     // handle error } else if (i > 0) {     // handle input } else {     return true; } 

...is good programming style and practice. The variable i only exists for the block of if statements for which it is needed and then goes out of scope.

However, this feature of the programming language doesn't seem to be supported by g++ (version 4.3.3 Ubuntu specific compile), which is surprising to me. Perhaps I'm just calling g++ with a flag that turns it off (the flags I've called are -g and -Wall). My version of g++ returns the following compile error when compiling with those flags:

socket.cpp:130: error: expected primary-expression before ‘int’ socket.cpp:130: error: expected `)' before ‘int’ 

On further research I discovered that I didn't seem to be the only one with a compiler that doesn't support this. And there seemed to be some confusion in this question as to exactly what syntax was supposedly standard in the language and what compilers compile with it.

So the question is, what compilers support this feature and what flags need to be set for it to compile? Is it an issue of being in certain standards and not in others?

Also, just out of curiosity, do people generally agree with Stroustrup that this is good style? Or is this a situation where the creator of a language gets an idea in his head which is not necessarily supported by the language's community?

like image 891
Daniel Bingham Avatar asked Oct 04 '09 17:10

Daniel Bingham


People also ask

What is declaring and initializing variables?

Declaration tells the compiler about the existence of an entity in the program and its location. When you declare a variable, you should also initialize it. Initialization is the process of assigning a value to the Variable. Every programming language has its own method of initializing the variable.

What does the conditional statement Control in C?

C has the following conditional statements: Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.

What is meant by initializing a variable in C?

Variables are names given to these memory locations. The memory location referred to by this variable holds a value of our interest. Now, these variables once declared, are assigned some value. This assignment of value to these variables is called initialization of variables.


2 Answers

It is allowed to declare a variable in the control part of a nested block, but in the case of if and while, the variable must be initialized to a numeric or boolean value that will be interpreted as the condition. It cannot be included in a more complex expression!

In the particular case you show, it doesn't seem you can find a way to comply unfortunately.

I personally think it's good practice to keep the local variables as close as possible to their actual lifetime in the code, even if that sounds shocking when you switch from C to C++ or from Pascal to C++ - we were used to see all the variables at one place. With some habit, you find it more readable, and you don't have to look elsewhere to find the declaration. Moreover, you know that it is not used before that point.


Edit:

That being said, I don't find it a good practice to mix too much in a single statement, and I think it's a shared opinion. If you affect a value to a variable, then use it in another expression, the code will be more readable and less confusing by separating both parts.

So rather than using this:

int i; if((i = read(socket)) < 0) {     // handle error } else if(i > 0) {     // handle input } else {     return true; } 

I would prefer that:

int i = read(socket); if(i < 0) {     // handle error } else if(i > 0) {     // handle input } else {     return true; } 
like image 67
RedGlyph Avatar answered Oct 16 '22 08:10

RedGlyph


I consider it a good style when used with possibly NULL pointer:

if(CObj* p = GetOptionalValue()) {    //Do something with p } 

This way whether p is declared, it is a valid pointer. No dangling pointer access danger.

On the other hand at least in VC++ it is the only use supported (i.e. checking whether assignment is true)

like image 39
EFraim Avatar answered Oct 16 '22 09:10

EFraim