c++ - How to declare a variable in the brackets of if statement? -
i want declare local variable in brackets of if statement. example.
if((char c = getc(stdin)) == 0x01)//this not ok g++. { ungetc(c, stdin); }
what want is, see if character 1 want. commonly, want use variable(char c) both in line of if , body of if, not outside if.
but g++(gcc 4.8.1) says expected primary-expression before 'char'. wonder if there's way that, because don't want like
char c = getc(stdin); if(c == 0x01) { bla... }
if it's namespace pollution worrying can define if
statement within block:
{ char c = getc(stdin); if(c == 0x01) { // ... } }
so c
last until end of block reached.
Comments
Post a Comment