Close
Close

TOPIC: Logical Operators Can you please explain the logic behind the not operator mentioned in the example(program)?

   UTHAPPA


Answers

  •   

    Not operator is just used to reverse a condition. If the condition is true, it will make it false and vice versa.

    #include <stdio.h>
    int main()
    {
        int a = 5, b = 0; 
     
        printf("\n%d", !a);
        printf("\n%d", !b);
        return 0;
    }
    

    In this example, the value of b is (false) and so !b will reverse it thus making it 1 (true). Similarly, the value of a is non-zero (true), so !a will make it 0 (false). 


    • Thankyou Aakhya. There is a mistake in the example. As you said "the value of a is non-zero (true), so !a will make it 0 (false)", but it has been given as TRUE. Please do rectify it.
      - UTHAPPA
    • it simplay conversts the value ..i mine makes oposite.. (1)true CONVERTS false (2)false CONVERTS true thats it nothing more.. true is converted to false and if condition is false then is converted to true.. one point more that is it is converted once only example: a=5; printf("\n%d", !a); so the condition is true if value is non zero okay and conclusion this is true but bcz of not operator it will be reversed and then finally its falseeeeee.......
      - jenny_uppal_1722
    • Thanks for pointing this out. It has been corrected.
      - Aakhya Singh

Ask Yours
Post Yours
Write your answer