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.