C - General Programming - Return value of a if statement without any condition

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

Comment: 

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.

Comment: 

It's true because ,in condition statement it considers true or false.if o equals false then any of other values consider as true

Comment: 

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:

$ True - Value was -1.

I hope it is more clear. laugh
 

Comment: 

does negative value inside if statement is executed in c99 standards?

Comment: 

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?

Comment: 

Hello santhan,

Yes indeed, to avoid this kind of error, you should always test your float number with the "f" extension, like this:

#include "stdio.h"

int main() {
    float x = 0.7;

    if (x < 0.7f) {
        printf("LOWER");
    } else if (x == 0.7f){
        printf("EQUAL");
    } else {
        printf("GREATER");
    }
    return 0;
}

I hope it helps laugh

 

Comment: 

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.

Comment: 

if (!number) // --> what is happening here?
printf("True - Value was %d.\n", number);
else
printf("False - Value was %d.\n", number);
return (0);

Comment: 

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. smiley

 

 

 

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.