Thread: Logical not operator

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    129

    Logical not operator

    Logical NOT operator : True and false condition

    A ~A
    0 1
    1 0

    Code:
    #include <stdio.h>
    
    int main (void)
    {
        unsigned int A = 1;
         
        if (!A)
         {    
           printf (" True");
         }
         else
         {
            printf ("False"); 
         }
       
        return 0;
    }
    If A = 0 then condition become true if A = 1 then condition become false

    what is meaning of this if(!A) ?

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    When found inside a boolean expression, an integer is equivalent to False if zero and True otherwise. So in your code, "!A" essentially means "A == 0".
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    I don't perceive much of a question here, but I'll do my best to guess at it.

    Actually, 0 is always false and anything else is always true. Some functions return 0 in some circumstances and it may be those circumstances where you want to do something. For example, if you are using the function strcpy(char* a, char* b), this function returns 0 if a is equal to b. If you want to do something when a is not equal to b, you might use the logical not operator.
    Code:
    if (!strcpy(a, b)) {
            // do something
        }
    However, I think you might be assuming ! and ~ are the same thing. The ! is the logical not operator while ~ is the bitwise not operator. The bitwise not operator flips all bits that are one's to become zeros and all zeros to become ones. The logical not changes any non-zero decimal number (not bits) to become zero and any zero value to become one.
    Last edited by jack jordan; 10-27-2017 at 01:29 AM.

  4. #4
    Registered User
    Join Date
    May 2017
    Posts
    129
    Thanks @GReaper and @Jack jordan

    Code:
    #include <stdio.h>
    
    int main (void)
    {
        unsigned int A;
        printf("Enter Input");
        scanf("%d", &A);
         
        if (!A)
         {    
           printf (" True");
         }
         else
         {
            printf ("False"); 
         }
       
        return 0;
    }
    What is meaning of logic not (!) in define

    #define low (!low)
    #define low (!High)

  5. #5
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    I just saw I made a mistake, which seems to happen all the time. When I wrote strcpy, I meant strcmp.

  6. #6
    Registered User
    Join Date
    Oct 2017
    Posts
    36
    IF the user did input some value into the variable then the if expression becomes false and in this case the else portion of the code is printed.
    In essence if the variable has a value, the not operator makes the expression which should be true false which then leads the the printing out of the else statement.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. logical operator query
    By cfcorp in forum C Programming
    Replies: 1
    Last Post: 12-01-2015, 07:22 PM
  2. logical operator
    By acpower in forum C Programming
    Replies: 5
    Last Post: 04-16-2012, 02:43 AM
  3. Preincrement and Logical Operator
    By NKP in forum C Programming
    Replies: 2
    Last Post: 01-26-2010, 02:14 AM
  4. logical operator !
    By scrapper in forum C Programming
    Replies: 1
    Last Post: 02-18-2005, 08:30 AM
  5. Logical Operator not working?
    By xshapirox in forum C++ Programming
    Replies: 2
    Last Post: 09-27-2004, 05:21 AM

Tags for this Thread