Please condiser the following snippet
//foo is just a random function which returns error code
if(int err = foo() == 0){
//err should contain the error code
...
}
The problem with that is err
contains the result of foo() == 0
, and what I want to evalute is int err = foo();
then if(err == 0)
but inside the if
statement.
I have already tried if((int err = foo()) == 0)
and it doesn't work.
Also, I don't want to do int err; if((err = foo) != 0))
.
Is this possible ?
CodePudding user response:
For C 17 and onwards you can use if statements with initializers:
if (int err = foo(); err == 0) {
...
}