if语句整数 [英] if statement integer

查看:113
本文介绍了if语句整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这是一个简单的问题,但我正在努力解决以下问题。在示例中,我有以下语句(语言是C):

i think this is a simple question, but i'm struggling with the following. in example i have the following statement (language is C):

int foobar

if (foobar)
{
// do something.
}

现在,如果我对此是正确的,当foobar是不是零。所以它应该与if(foobar!= 0)相同;

now, if I am correct about this, this statement is true when foobar is not zero. so it should be fairly the same as if(foobar!=0);

但是如果foobar成为负数会怎样?

But what happens if foobar becomes a negative number?

推荐答案

消极或积极。任何不是0的东西都是中的真值,如果

negative or positive. Anything that's not a 0 is a true value in if

另外,考虑一个负数:-1

Also, Consider a negative number: -1

-1在C内部表示为: 0xFFFFFFFF ,在这种情况下,如果我施放,它将是一个正数它是无符号整数。

-1 in C internally is represented as: 0xFFFFFFFF, in which case, it would be a positive number if I cast it to unsigned integer.

但是在C99标准编译器出现之后,我建议你使用
< stdbool.h> 相反。使猜测工作少了很多:

But after the advent of C99 standard compilers, I suggest you use <stdbool.h> instead. Makes the guessing work a lot less:

阅读这里关于stdbool.h

这篇关于if语句整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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