C for beginners: The Decision Control Structure

The "!" Operator

So ftar, we have used only the logical operators && and ||. The third logical operator is the NOT operator, written as !. This operator reverses the result of the expression it operates on. For example, if the expression evaluates to a non-zero value, then applying the ! operator to it results into a 0. Vice versa, if the expression evaluates to zero, then applying the ! operator to it makes it 1, a non-zero value. The final result (after applying !) 0 or 1 is considered to be false or true respectively. Here’s an example of the NOT operator applied to a relational expression:

if (y != 10) {
  // This code is executed only if "y" is not equal to ten

This means “y is not equals to 10”. In other words, if y is not equals to 10, the expression will be false, since (y == 10) is true.

The NOT operator is often used to reverse the logical value of a single value, as in the expression:

if (!flag) {

This is another way of saying:

if (flag == 0) {