To test a condition in a if statement without any condition, the result has to be other than 0 to test a true condition.
Let's see an example of a if statement without any condition:
#include <stdio.h> int checkIt() { int a; a = 0; if (a == 0) return (-1); return (0); } int main() { int number; number = checkIt(); if (number) printf("True - Value was %d.\n", number); else printf("False - Value was %d.\n", number); return (0); }
Compile and execute it:
$ gcc main.c && ./a.out
Result:
$ True - Value was -1.
In this example above we can see that if the return value of the number variable is 0.
If this value is equal to 0, the if statement is considered as false.
Note that this value can be a negative or a positive one.
Example: -1, -900, 892, 12909093 are the same.
Let's try another example of a if statement with the contrary of a value, with the ! operator.
#include <stdio.h> int checkIt() { int a; a = 0; if (a == 0) return (-5451); return (0); } int main() { int number; number = checkIt(); if (!number) printf("True - Value was %d.\n", number); else printf("False - Value was %d.\n", number); return (0); }
Result:
$ False - Value was -5451.
Comments
Yuvaraj (not verified)
Tuesday, March 20, 2012 - 8:14am
Permalink
The Result of the First
The Result of the First example specified is NOT correct. Please change the same as it leads to confusion for those who learn. If the value is less than or equal to zero then its considered as FALSE. And TRUE if greater than 0.
Narendra (not verified)
Sunday, June 4, 2017 - 5:23am
Permalink
It's true because ,in
It's true because ,in condition statement it considers true or false.if o equals false then any of other values consider as true
Mi-K
Tuesday, March 20, 2012 - 7:33pm
Permalink
Hello Yuvaraj,
Hello Yuvaraj,
I agree with you this example is not easy to understand, especially for beginners.
But it's correct.
Indeed, in C programming language, only the 0 (zero) value avoids the program to enter in a if statement.
You can test it.
A negative or a positive int will allow the program to enter in.
The number variable in the first example is equal to -1.
So the program enter in the if statement and display:
I hope it is more clear.
kaushik bose (not verified)
Monday, July 16, 2012 - 3:23pm
Permalink
does negative value inside if
does negative value inside if statement is executed in c99 standards?
santhan kumar.N (not verified)
Tuesday, July 21, 2015 - 11:56am
Permalink
i have a doubt
i have a doubt
float x=0.7;
if(x<0.7)
printf("true");
else
printf("false");
and the output is true.whats the matter with it?
what value does the id statement will return?
Mi-K
Tuesday, July 21, 2015 - 12:36pm
Permalink
Hello santhan,
Hello santhan,
Yes indeed, to avoid this kind of error, you should always test your float number with the "f" extension, like this:
I hope it helps
santhan kumar.N (not verified)
Tuesday, July 21, 2015 - 1:05pm
Permalink
yeah,thank you!
yeah,thank you!
so,technically in if statement 0.7 cannot be taken as exact floating point value 0.7.so to mention it as a floating point value we use 'f' extension.
vaishali (not verified)
Monday, July 10, 2017 - 2:22pm
Permalink
if (!number) //what is
if (!number) // --> what is happening here?
printf("True - Value was %d.\n", number);
else
printf("False - Value was %d.\n", number);
return (0);
Mi-K
Monday, July 10, 2017 - 4:52pm
Permalink
Vaishali,
Vaishali,
Actually with the statement: if (!number) we are just testing if the value is equal or not to the number variable value.
In this case, the number variable value is equal to: -5451.
So by writing !number we ask the program if this value is different from -5451.
As it's the same, we return false.
Add new comment