C Logical Operators

Beginner
6 min read
Updated: Nov 2026
1 example

What Is Logical Operators?

Logical operators combine truth values. && and || use short-circuit evaluation: the right side is skipped when the left side already decides the result. Any non-zero value counts as true.

Remember
a && b    /* true if both true; skips b if a is false */
a || b    /* true if either true; skips b if a is true */
!a        /* true if a is zero */

Cheat Sheet

Keep these facts handy while you work with Logical Operators in C.

TopicDetail
AND&& stops at the first false
OR|| stops at the first true
NOT!x is 1 when x is 0
vs bitwise&& is logical; & works on bits

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Assignment Operators.

Example 1 — Short-circuit evaluation

A function with a side effect shows whether the right operand ran.

C
#include <stdio.h>

static int calls = 0;

static int check(void) {
    calls++;
    return 1;
}

int main(void) {
    int a = 0;

    if (a != 0 && check()) { }
    printf("after false &&: calls = %d\n", calls);

    if (a == 0 && check()) { }
    printf("after true &&:  calls = %d\n", calls);

    if (a == 0 || check()) { }
    printf("after true ||:  calls = %d\n", calls);
    return 0;
}

How It Works

  1. a != 0 is false, so check() never runs.
  2. a == 0 is true, so && must evaluate check().
  3. For ||, a true left side makes the right side unnecessary.

Edge Cases & Pitfalls

Check these before relying on Logical Operators in real programs.

&|

Using & instead of &&

& evaluates both sides and works on bits. 1 & 2 is 0 while 1 && 2 is 1.

side

Side effects on the right

Code like x > 0 && y++ may skip y++. Keep side effects out of conditions.

Key Takeaways

  • Short-circuit: right side may not run.
  • Guard: p != NULL && p->x > 0 is safe.
  • Truth: any non-zero value is true.

One line: Logical operators combine truth values.

Frequently Asked Questions

For && and ||, C stops evaluating as soon as the answer is known. This is useful for guards such as ptr && ptr->value.
No. && is logical and short-circuits; & is a bitwise AND on every bit of both operands.
Continue with "C Assignment Operators" next. Store values with = and update them with += -= *= /= %= and friends. The C Operators hub lists every topic in order.

Next: Assignment Operators

Store values with = and update them with += -= *= /= %= and friends.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful