! the logical operator not in C

In this article we will learn about 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 ! operator to it results into a 0. Vice versa, if the expression evaluates to zero then on applying ! 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.

Example:

! ( y < 10 )

This means “not y less than 10”. In other words, if y is less than 10, the expression will be false, since ( y < 10 ) is true. We can express the same condition as ( y >= 10 ).

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

if ( ! flag )

This is another way of saying the same this as below :

if ( flag == 0 )

Does the NOT operator sound confusing? Avoid it if you want, as the same thing can be achieved without using the NOT operator.

not operator
not operator

Related Posts

  • 88
    C allows usage of three logical operators, namely, &&, || and !. These are to be read as ‘AND’ ‘OR’ and ‘NOT’ respectively. There are several things to note about these logical operators. Most obviously, two of them are composed of double symbols: || and &&. Don’t use the single…
    Tags: program, logical, operator, learn, write, programming, tutorial
  • 77
    Since we have now added the logical operators to the list of operators we know, it is time to review these operators and their priorities. The higher the position of an operator is in the table, higher is its priority. The following figure summarizes the working of all the three…
    Tags: logical, operator, learn, program, programming, example, write, tutorial
  • 72
    Like most languages, C uses the keyword if to implement the decision control instruction. The general form of if statement looks like this: if ( this condition is true )       execute this statement ; The keyword if tells the compiler that what follows is a decision control…
    Tags: true, non-zero, program, example, operator, learn, programming, write, tutorial
  • 69
    The if statement can take any of the following forms: if ( condition ) do this ; if ( condition ) { do this ; and this ; } if ( condition ) do this ; else do this ; if ( condition ) { do this ; and this…
    Tags: learn, program, write, programming, tutorial
  • 68
    Let's have an example to understand else if clause : /* else if ladder demo */ main( ) { int m1, m2, m3, m4, m5, per ; per = ( m1+ m2 + m3 + m4+ m5 ) / per ; if ( per >= 60 ) printf ( "First…
    Tags: program, learn, programming, write, tutorial

Satya Prakash

VOIP Expert: More than 8 years of experience in Asterisk Development and Call Center operation Management. Unique Combination of Skill Set as IT, Analytics and operation management.

Leave a Reply

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

%d bloggers like this: