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

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

问题描述

有没有办法在if语句中声明变量(仅使用ANSI C)?>

示例:

if(int 变量 = some_function()){返回 1;}

解决方案

不,你不能那样做.

你可以做的是为 if

创建一个块

<代码> {整数变量;变量 = some_function();如果(变量)返回 1;}/* 变量在这里超出范围 */

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

if (some_function()) return 1;

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

Example:

if(int variable = some_function())
{
    return 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;

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

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