EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Logical Operators in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Logical Operators in C

Logical Operators in C

Introduction to Logical Operators in C

Logical operators are part of binary operators. These operators are specifically used when we are going to combine two or more requirements together. These can be used in many conditional and relational expressions. On evaluating these conditions, these are the Boolean expressions which give an output of either 1/0 for True/False respectively. Let us below learn about different logical operators in the C programming language.

Different Logical Operators in C

The three main logical operators are ‘&&’, ‘||’ and ‘!’. The truth tables can be understood by:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

a

b a && b

a || b

0

0 0 0

0

1 0

1

1 1 1

1

1 0 0 1

And for NOT operator:

a

!a

1

0
0

1

The output ‘1’ and ‘0’ denotes the True and False respectively. Through these, the conditional operations that are being performed can be very well understood.

Examples to Implement Logical Operators in C

Types of logical operators with their examples and implementation are explained below.

1. AND Operator

This operator is symbolized by ‘&&’. This operator gives the true as the output if all the conditions.

Example #1: Let us see a simple example using the AND operator given below.

Code:

#include <stdio.h>
int main()
{
int n;
printf("Enter a digit between 1 to 10: ");
scanf("%d",&n);
if((n>0) && (n<=10))
{
printf(" Given number is in between 0 and 10");
}
else if((n>10) && (n<=20))
{
printf("Given number is in between 10 and 20");
}
else
{
printf("Please enter a number in the given range");
}
return 0;
}

This above example has our and condition which has many conditions and all the conditions must be satisfied.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,570 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Output:

Logical Operators in C eg1

Example #2: In a similar way, we can write another example using AND operator.

Code:

#include <stdio.h>
int main()
{
int a,b;
a=855;
b=1300;
if((a>=500) && (b<1000))
{
printf(" This is the first condition");
}
else
{
printf(" This is the second condition");
}
}

Output:

Logical Operators in C eg1.1

So, this is how we are going to have the AND condition.

2. OR Operator

This is the condition where any one of the given scenario can be true.

Example #1: Let us check this operator with a small example given below.

Code:

#include <stdio.h>
int main()
{
int n;
printf("Enter a digit between 1 to 20: ");
scanf("%d",&n);
if((n%2==0) || (n%5==0))
{
printf(" Number given is divisible by either 2 or 5");
}
else{
printf(" Number is not divisible by 2 or 5");
}
return 0;
}

Output:

Logical Operators in C eg2

So, if the given number is either divisible by 2 or 5, then the condition is executed.

Example #2: Now let us see what happens if the same condition is executed with ‘and’ condition.

Code:

#include <stdio.h>
int main()
{
int n;
printf("Enter a digit between 1 to 20: ");
scanf("%d",&n);
if((n%2==0) && (n%5==0))
{
printf(" Number given is divisible by 2 and 5");
}
else{
printf(" Number is not divisible by 2 and 5");
}
return 0;
}

Output:

Logical Operators in C eg2.2

After comparing the two examples we can understand the main difference between ‘AND’ and ‘OR’ logical operators. As the output for these logical operators is a Boolean expression, True/False which is the outcome executes the code inside those conditional statements.

3. NOT Operator

This logical operator is usually defined by symbol ‘!’. This operator is equal to “not equal to”.

Example #1: Let us see a small example of this below.

Code:

#include <stdio.h>
int main()
{
int n;
printf("Enter a number: ");
scanf("%d",&n);
if(n %2 != 0)
{
printf(" This is an odd number");
}
else
{
printf(" This is an even number");
}
return 0;
}

Output:

eg3.1

Example #2: In a similar way, we can write another example using Not operator.

Code:

#include <stdio.h>
int main()
{
int a,b;
printf("Enter a number : ");
scanf("%d",&a);
printf("Enter b number: ");
scanf("%d",&b);
if(a!=b)
{
printf(" A and B values given are different");
}
else
{
printf(" A and B values given are same");
}
return 0;
}

Output:

eg3.2

So, these are the three logical operators defined through the C programming language.

Example #3: Here let us see one more example where all three of them can be used together.

Code:

#include <stdio.h>
int main()
{
int x,y,z;
printf("Enter first number : ");
scanf("%d",&x);
printf("Enter second number: ");
scanf("%d",&y);
printf("Enter third number: ");
scanf("%d",&z);
if((x>=y) && (y>=z))
{
printf(" X is the highest number");
}
else if((x>=y) || (x>=z) )
{
printf(" X is either greater than y or z");
}
else if((x>=y) || (x>=z) && (y>=z))
{
printf(" Both 'and' and 'or' are used and X is definitely larger ");
}
else if( x!=y)
{
printf(" Checking for a condition that X is not equal to Y");
}
else
{
printf("Finally");
}
return 0;
}

Output:

eg3.3

This is just an example of using all these logical operators in one program.

Recommended Articles

This is a guide to Logical Operators in C. Here we discuss the basic concept, different logical operators in C along with examples and code implementation. You can also go through our other suggested articles to learn more –

  1. Collections in C#
  2. Logical Operators in JavaScript
  3. Logical Operators in PHP
  4. SQL Logical Operators

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More