Reputation: 1219
I would like to ask what is the behaviour of the C language on the following:
int n = 0;
.
.
// n in this block may change value to something else but a (positive) number
.
.
if(n)
{
// do something
}
.
.
So what the if(n)
means when the n
is an integer?
Upvotes: 3
Views: 26852
Reputation: 1
Just wanting to add that if(i) will be the same as if (!!i), checking if it is non-zero. While, if (!i) is to check for zero. Since true is 1, and false is 0, !!(true) returns true and !!(false) returns false. So, it is sometimes useful when the integer is storing perhaps an error code and wanting to normalize its result into a boolean and it is quite short, but I would recommend writing it out to be clear. (ie; if (i != 0) rather than if (i))
Hope this below helps.
int i1 = -50, i2 = 0, i3 = 50;
if (i1)
printf("if(-50) passed\n");
if (i2)
printf("if(0) passed\n");
if (i3)
printf("if(50) passed\n");
if (!!i1)
printf("if(!!-50) passed\n");
if (!!i2)
printf("if(!!0) passed\n");
if (!!i3)
printf("if(!!50) passed\n");
if (!!true)
printf("if(!!true) passed\n");
if (!!false)
printf("if(!!false) passed\n");
OUTPUT:
if(-50) passed
if(50) passed
if(!!-50) passed
if(!!50) passed
if(!!true) passed
Upvotes: 0
Reputation: 3872
Its a bin difficult to understand what you want to ask here, but I'll toss up my 2 cents and you decide if it helps you understand the way if
statements work.
int uiStupidFunction(int num)
{
return num++;
}
int uiAnotherStupidFunction(int num)
{
return 0;
}
int main(void)
{
int n = 0;
// n in this block may change value to something else but a (positive) number
//suppose in this way
n = uiStupidFunction(10);
if(n)
{
printf("n = %d", n); //prints 10
//do something
}
n = uiAnotherStupidFunction(10);
if(n)
{
printf("n = %d", n); //prints?
//do something
}
else
{
printf("\nHumpty Dumpty sat on a wall"); //
}
}
Upvotes: 0
Reputation: 3735
In C, all integer types that have 0 value, evaluate to false otherwise they evaluate to true. Note that even negative numbers evaluate to true.
In your case if n
remains 0, the body of the if statement will not execute, because the expression evaluates to false. If n
gets set to something else than 0, the if statement body will execute.
Upvotes: 1
Reputation: 134286
As per the C11
standard document, chapter 6.8.4, Selection statements,
if ( expression ) statement
A selection statement selects among a set of statements depending on the value of a controlling expression.
and from chapter 6.8.4.1, The if
statement
In both forms, the first substatement is executed if the expression compares unequal to 0.
So, essentially, here, if(n)
will
if(n)
results TRUE (n
unequal to 0, includes both +ve
and -ve
values)if(n)
results FALSE (n
equal to 0)Upvotes: 4
Reputation: 2927
In the language C, the boolean type is representing with integer so every positive number is considered like the boolean true and only the integer 0 is considered like the boolean false.
Upvotes: 1
Reputation: 1277
You seem to have misunderstood what a condition means. A condition need not necessarily contain comparators such as ==
or <
etc. A condition can be any expression. Now, if the if expression evaluates to a zero value it is considered false and the if statement is not evaluated. Otherwise, it is considered true and the if statement is evaluated.
Upvotes: 0