Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
Logical Expressions and Short-Circuit Evaluation in C
Next article icon

Logical Expressions and Short-Circuit Evaluation in C

Last Updated : 15 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C, logical expressions are used to perform decision-making using logical operators such as && (AND) or || (OR) by combining multiple conditions. These expressions return either true (non-zero) or false (0).

Example:

C
#include <stdio.h>  int main() {     int a = 10, b = 0;          if (a != 0 && b != 0) {         printf("a and b are non zero.");     }     else {         printf("Either a or b or both are zero");     } } 

Output
Either a or b or both are zero

In the above program, we combined two conditions (a != 0) and (b != 0) logically using && operator which finally results in false value, that is why the else body is executed.

We now know we can create logical expressions using logical operators but let's see what each operator means.

Meaning of Different Logical Operator

The logical expression evaluation or its meaning depends on its truth table. Let's take a look:

First Operand (X)

Second Operand (Y)

AND Evaluation (X && Y)

OR Evaluation (X || Y)

0

0

0

0

0

1

0

1

1

0

0

1

1

1

1

1

Logical AND (&&) Meaning

In the above table, we see that && AND evaluates true only when both of its operand is true. We can verify this in the first example:

  • Expression a != 0 is true.
  • Expression b != 0 is false.

First operand of && is true while the second one is false. It resembles third case in the above truth table. That is why, the result of the logical expression is false.

Note: Any non-zero value can be considered true or 1. But only zero is considered false.

Logical OR (||) Meaning

We can see that || OR evaluates true if any of its operands is true. We can change the above example and replace && with || and see what happens:

C
#include <stdio.h>  int main() {     int a = 10, b = 0;          if (a != 0 || b != 0) {         printf("Either a or b are non zero.");     }     else {         printf("Both are zero");     } } 

Output
Either a or b are non zero.

The output is changed. The body of the if block is now being executed, which means that the logical expression evaluates to true. Let's break it down:

  • Expression a != 0 is true.
  • Expression b != 0 is false.

First operand of || is true while the second one is false. But || it only needs one of them to be true.

Example

We can combine many different conditions into a compound expression using logical operators:

C
#include <stdio.h>  int main() {     int a = 10, b = 182;          if (a > 0 && b > 0 || a - b == 0) {         printf("Either a and b are equal or greater than 0 or both");     }     else {         printf("else block");     } } 

Output
Either a and b are equal or greater than 0 or both

Keep in mind the operator precedence and associativity while writing these expressions to make sure the expression is evaluated as intended.

Short-Circuit Evaluation

From the above, we can infer that:

  • For && (AND): If any operand is false, then it is false.
  • For || (OR): If any operand is true, it is true.

This property can lead to an interesting optimization in the evaluation of logical expressions.

  • AND (&&) can determine the result if the first operand is false without evaluating the second operand.
  • OR (||) AND (&&) can determine the result if the first operand is true without evaluating the second operand.

This property is called short circuit evaluation. We can verify this property using the following code:

Logical AND Short-Circuit Example

C
#include <stdio.h>  int main() {     int a = 10;          if (a < 0 && (a = 22)) {         printf("if block\n");     }     else {         printf("else block\n");     }          printf("Value of a: %d", a);     return 0; } 

Output
else block Value of a: 10

In the above expression, condition (a < 0) is false, so the AND expression should result in false leading to the execution of else block. This is what happens in the program.

But the second operand of the AND is an assignment expression which changes the value of the variable a. But when we print the value of a at the end, it is still not changed. This means that second operand is not even evaluated, confirming the short circuit evaluation.

Logical OR Short-Circuit Example

Just like logical AND example, we can also verify the short circuit evaluation for logical OR expressions.

C
#include <stdio.h>  int main() {     int a = 10;          if (a > 0 || (a = 22)) {         printf("if block\n");     }     else {         printf("else block\n");     }          printf("Value of a: %d", a);     return 0; } 

Output
if block Value of a: 10

The first condition is true, so second operand is not even evaluated (shown by no value change in a variable) in the OR expression.


Next Article
Logical Expressions and Short-Circuit Evaluation in C

A

abhishekcpp
Improve
Article Tags :
  • C Language
  • C-Operators
  • C Decision Making

Similar Reads

    What are the differences between bitwise and logical AND operators in C/C++?
    A Bitwise And operator is represented as '&' and a logical operator is represented as '&&'. The following are some basic differences between the two operators. a) The logical and operator '&&' expects its operands to be boolean expressions (either 1 or 0) and returns a boolean va
    4 min read
    Interesting facts about switch statement in C
    Prerequisite - Switch Statement in C Switch is a control statement that allows a value to change control of execution. C // Following is a simple program to demonstrate syntax of switch. #include <stdio.h> int main() { int x = 2; switch (x) { case 1: printf("Choice is 1"); break; cas
    3 min read
    C Program for Difference between sums of odd and even digits
    Write a C program for a given long integer, the task is to find if the difference between sum of odd digits and sum of even digits is 0 or not. The indexes start from zero (0 index is for leftmost digit). Examples: Input : 1212112Output : YesExplanation:the odd position element is 2+2+1=5the even po
    2 min read
    Implicit Type Conversion in C with Examples
    Prerequisite: Data Types, Type Conversion Implicit Type Conversion is also known as 'automatic type conversion'. It is done by the compiler on its own, without any external trigger from the user. It generally takes place when in an expression more than one data type is present. In such condition typ
    5 min read
    Sequence Points in C | Set 1
    In this post, we will try to cover many ambiguous questions like following. Guess the output of following programs. C // PROGRAM 1 #include <stdio.h> int f1() { printf ("Geeks"); return 1;} int f2() { printf ("forGeeks"); return 1;} int main() { int p = f1() + f2(); return
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences