How to declare a variable in the brackets of if statement?
c++, c++11
Solution
If it's the namespace pollution you are worrying about you can always define the `if` statement within a block:
{
char c = getc(stdin);
if(c == 0x01)
{
// ...
}
}
So that `c` will only last until the end of the block is reached.
Problem
I want to declare a local variable in the brackets of an if statement. For example. ``` if((char c = getc(stdin)) == 0x01)//This is not OK with g++. { ungetc(c, stdin); } ``` What I want is, to see if the character is the one I want. To say it commonly, I want to use the variable(char c) both in the line of if and the body of if, but not outside the if. But g++(GCC 4.8.1) says expected primary-expression before 'char'. I wonder if there's a way to do that, because I don't want something like ``` char c = getc(stdin); if(c == 0x01) { bla... } ```