在if语句中声明变量(ANSI C) [英] Declare variable in if statement (ANSI C)

查看:398
本文介绍了在if语句中声明变量(ANSI C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 if语句 中声明变量(仅使用 ANSI C )?

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

示例:

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

推荐答案

不,您不能这样做.

您可以做的是仅为if

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

请注意,在这种简单情况下,您可以将函数作为if的条件调用(不需要额外的变量)

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;

这篇关于在if语句中声明变量(ANSI C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆