简体   繁体   English

C语言中if(变量)是什么意思?

[英]What does if (variable) mean in C language?

I'm currently preparing for gate;我目前正在准备登机口; I have come across a question我遇到了一个问题

#include<stdio.h>
main()
{
    static int var=6;
    printf("%d\t",var--);
    if(var)
        main();
}

The output is 6 5 4 3 2 1输出为 6 5 4 3 2 1

I wanna know why it terminated after 1?我想知道为什么它在 1 之后终止?

An if statement always tests to see if the expression inside the parentheses evaluates to true. if语句始终测试以查看括号内的表达式的计算结果是否为真。

In this case, var is an positive integer, so it evaluates to true .在这种情况下, var是一个正整数,因此它的计算结果为true Since 0 always evaluates to false , as soon as var = 0 the if statement evaluates to false and the loop exits.由于 0 总是评估为false ,一旦var = 0 if语句评估为false并且循环退出。

Note that if(var) is not specific to C (re the question title), it applies to many languages.请注意, if(var)并非特定于 C(问题标题),它适用于许多语言。

if (var) checks whether var is not zero! if (var)检查var是否不为零! If var is zero it finish.如果var为零,则结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM