What does if (x) mean in C programming ?

The general syntax of  ' if  ' operator is :

if ( <var> <relational operator> <var>/<constant> ) 
 {
    ............
    ............ 
 }
But here only  if(<var>) is present. This represents nothing but :-
if ( <var> != 0 )
 {
    ............
    ............
 }
So here, in layman's word if(x) represents nothing but if(x!=0).

The if condition works till x doesn't become zero or in the language of computer Science, zero represents FALSE and one represents TRUE, so while a variable is false or 0, the ' if  ' condition doesn't allow the compiler to pass through it. 

An Program using this concept : 

Write a program to check a number it is odd or even.

#include <stdio.h>

int main()
{
    int n;
    printf("number= ");
    scanf("%d",&n);
    if(n%2)
    {
        printf("\nThe number is odd");
    }
    else
    {
        printf("\nThe number is even");
    }

    return 0;
}




Comments

Popular posts from this blog

Bubble Sort ( C & Python 3)

Comparison Logical and Bitwise Operator ( Java Part - 4 )

Something about me