True or false in C

By | December 26, 2015

A simple concept in the C language is the evaluation of a boolean expression with an if statement. But what if the condition is not a typical relational or boolean expression? In C, it is possible to write:

if (x){ // if x is true or great than zero
  printf("x is true/has a value/great than zero");
  do something;
} else {
  printf("x is false/does not have a value/is zero");

This is straight forward. The opposite can be a bit confusing.

if (!x){ // if x is zero
  printf("x is false/does not have a value");
  do something;
} else {
  printf("x is true/has a value");

Reference:
https://www.eskimo.com/~scs/cclass/notes/sx3c.html

Leave a Reply

Your email address will not be published. Required fields are marked *