C Logical Operators

Previous
Next

Logical operators:

  • These are binary operators
  • These operators return a true or false by evaluating more than 1 expression
  • Operators are
    • Logical AND (&&)
    • Logical OR (||)
    • Logical NOT (!)
  • We can find the return value of logical operator according to TRUTH TABLE
Expression1Expression2&&||!Expression1!Expression2
TrueTrueTrueTrueFalseFalse
TrueFalseFalseTrueFalseTrue
FalseTrueFalseTrueTrueFalse
FalseFalseFalseFalseTrueTrue

Important points to be noted:

  • logical AND operator evaluates the expression2 only when expression1 is TRUE else directly returns 0.
  • logical OR opertator evaluates the expression2 only when expression1 is FALSE else directly returns 1.
#include<stdio.h>
main()
{
	int x=5 , y = 0 , a, b;
	a = ++y || ++x ;
	b = --y && ++x ;
	printf("%d,%d,%d,%d", x, y, a, b);
}

Analyze the code:

#include<stdio.h>
main()
{
	printf("%d\n", 3 && -13);
}

Crack the code:

#include<stdio.h>
main()
{
	printf("%d\n", 3 || !-13);
}

Crack the code:

#include<stdio.h>
void main()
{
	int x;
	x = 4%5==4%2+4;
	printf ("x = %d",x);
}

Crack the code:

#include<stdio.h>
void main()
{
	int a=1, b=5, c=3, d ;
	d = --a || --b && ++c ;
	printf("%d,%d,%d,%d",a,b,c,d);
}

Crack It:

#include<stdio.h>
void main()
{
	int x=-13;
	x = !!x ;
	printf ("x = %d",x);
}

Crack it:

#include<stdio.h>
void main()
{
	int a=10, b=5, c=3;
 	c = b != !a; 	
	b = !a != !!c;
   	printf("%d,%d,%d",a,b,c);
}

Crack this code:

#include<stdio.h>
main()
{
	int a=5,b=3,c=2,d=3,e,f,g,h;
	e = (b++ == ++c)	||	 (--b != d--);
	f  = (b-- != d++)	&&	 (--c <= --a);
	g = (c++ >= --d)	||	 (++b != --c);	
	h = (e++ <= --f)	&&   (++g != --c);
	printf("%d,%d,%d" , a,c,e);
}
Previous
Next

Add Comment

Courses Enquiry Form