if 语句整数 [英] if statement integer
问题描述
我认为这是一个简单的问题,但我正在努力解决以下问题.在我的示例中,我有以下语句(语言为 C):
I think this is a simple question, but I'm struggling with the following. In my 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 much the same as if (foobar!=0)
.
但是如果 foobar
变成负数会怎样?
But what happens if foobar
becomes a negative number?
推荐答案
否定或肯定.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标准编译器出现之后,我建议你使用
代替.使猜测工作少很多:
But after the advent of C99 standard compilers, I suggest you use
<stdbool.h>
instead. Makes the guessing work a lot less:
这篇关于if 语句整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!