Logical Operators in C

Last Updated :

Prerequisite – Operators in C
In C, logical operators are used to perform logical operations on expressions that evaluate to either true (nonzero) or false (zero). They are typically used in conditional statements and loops to make decisions based on whether certain conditions are met or not.

Types of Logical Operators in C:
There are three logical operators in C:

  1. && (logical AND)
  2. || (logical OR)
  3. ! (logical NOT)

Here’s a description of each operator and how they work:

  1. Logical AND (&&): This operator returns true (nonzero) if both expressions it connects are true, and false (zero) otherwise. For example, if a and b are both true, a && b will be true, but if one or both are false, a && b will be false. The logical AND operator has higher precedence than the logical OR operator.
  2. Logical OR (||): This operator returns true (nonzero) if at least one of the expressions it connects is true, and false (zero) otherwise. For example, if a is true and b is false, a || b will be true, but if both are false, a || b will be false.
  3. Logical NOT (!): This operator reverses the logical value of its operand. If the operand is true, it returns false (zero), and if the operand is false, it returns true (nonzero). For example, if a is true, !a will be false, and if a is false, !a will be true.

Example:
Here’s an example code that demonstrates the use of logical operators in C:

#include <stdio.h>

int main() {
    int a = 1;
    int b = 0;

    printf("a && b: %d\n", a && b);
    printf("a || b: %d\n", a || b);
    printf("!a: %d\n", !a);
    printf("!b: %d\n", !b);

    return 0;
}

When you run this code, you will see the following output:

a && b: 0
a || b: 1
!a: 0
!b: 1

This output shows the results of using each of the logical operators with the variables a and b. The value of each expression is printed to the console. In this example, a && b evaluates to false because b is false, a || b evaluates to true because a is true, and !a evaluates to false because a is true.

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Comment
Next Article
Mithlesh Upadhyay Published 18 Feb, 2023 · 2 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Article Tags :

Similar Reads

  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. C89/C90: Released in 1989/1990. It introduced key language features. C99: This…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read
  • C Language Introduction

    C is a programming language developed by Dennis Ritchie in 1972. It has a significant historical impact. It was originally created at Bell Laboratories of AT&T Labs for…

    4 min read
  • Code editors

    Code editors are where programmers spend most of their time. There are two main types of code editors: IDEs and Lightweight editors. IDEs (Integrated Development Environments) IDEs (Integrated…

    1 min read