简体   繁体   中英

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

I wanna know why it terminated after 1?

An if statement always tests to see if the expression inside the parentheses evaluates to true.

In this case, var is an positive integer, so it evaluates to true . Since 0 always evaluates to false , as soon as var = 0 the if statement evaluates to false and the loop exits.

Note that if(var) is not specific to C (re the question title), it applies to many languages.

if (var) checks whether var is not zero! If var is zero it finish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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