#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Operators

Logical Operators in C Programming

What are Logical Operators?

Logical operators combine multiple conditions and return a boolean-like result.

List of Logical Operators

  • && — logical AND, true only if both conditions are true
  • || — logical OR, true if at least one condition is true
  • ! — logical NOT, reverses the result
int a = 5, b = 10;
if (a > 0 && b > 0) {
    printf("Both positive");
}
if (!(a == b)) {
    printf("a is not equal to b");
}

Short-Circuit Evaluation

C evaluates logical expressions left to right and stops as soon as the result is determined — e.g. in &&, if the first condition is false the second is never evaluated.

Truth Table and What C Really Returns

C did not originally have a boolean type. Any non-zero value counts as true and zero counts as false. The logical operators always produce the int value 1 for true and 0 for false.

ABA && BA || B!A
00001
0non-zero011
non-zero0010
non-zeronon-zero110

So printf("%d", 7 && -3); prints 1, not 7 or -3. If you prefer readable code, include stdbool.h and use bool, true and false.

Precedence: Where Parentheses Save You

From highest to lowest, the relevant order is: !, then relational operators like < and >=, then equality == and !=, then &&, and finally ||. Because AND binds tighter than OR, this condition may not mean what you think:

/* Reads as: isAdmin || (isOwner && isActive) */
if (isAdmin || isOwner && isActive) { ... }

/* If you meant "admin or owner, and in any case active": */
if ((isAdmin || isOwner) && isActive) { ... }

Another trap is !x == 5. Since ! runs first, it becomes (!x) == 5, which is almost always false. Write !(x == 5) or simply x != 5.

Putting Short-Circuiting to Work

Short-circuit evaluation is not just a speed trick; it lets you guard risky operations. The right side runs only when the left side allows it.

#include <stdio.h>
#include <string.h>

int main(void) {
    int count = 0, total = 50;
    const char *name = NULL;

    /* Safe: division happens only when count is non-zero */
    if (count != 0 && total / count > 10) {
        printf("High average\n");
    }

    /* Safe: strlen is never called on a NULL pointer */
    if (name != NULL && strlen(name) > 0) {
        printf("Hello, %s\n", name);
    } else {
        printf("No name given\n");
    }
    return 0;
}

Swap the two sides of either condition and the program could crash. Order matters: put the safety check first.

Mistakes and Handy Rules

  • Using & instead of &&. The single ampersand is bitwise AND. It does not short-circuit, and 2 & 4 is 0 even though both values are true.
  • Writing = instead of ==. if (x = 0) assigns zero and is always false. Turn on compiler warnings with -Wall to catch it.
  • Chaining comparisons. if (1 < x < 10) does not check a range. Write if (x > 1 && x < 10).
  • Side effects on the right. In a > 0 && i++, the increment may never happen. Keep conditions free of updates.

De Morgan's laws help you simplify negations: !(a && b) equals !a || !b, and !(a || b) equals !a && !b. Use them to remove confusing double negatives from your conditions.

Ready to master real-world C Programming development?

Learn C Programming hands-on with mentor-led, live sessions.

Explore Course