Declare variable in if statement (ANSI C)

不问归期 提交于 2020-06-25 06:41:49

问题


Is there any way to declare variable in if statement (using ANSI C only) ?

Example:

if(int variable = some_function())
{
    return 1;
}

回答1:


No, you cannot do that.

What you can do is create a block just for the if

    {
        int variable;
        variable = some_function();
        if (variable) return 1;
    }
    /* variable is out of scope here */

Note that for this simple case you can call the function as the condition of the if (no need for an extra variable)

if (some_function()) return 1;


来源:https://stackoverflow.com/questions/43643349/declare-variable-in-if-statement-ansi-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!