Solved Examples · Example 11
Q.Write a C program to illustrate the logical ANDing and ORing operations.
#include<stdio.h>
void main()
{
int a, b, c;
a=5, b=4;
c=a&&b;
b=a||b||c;
a=a&&b||c;
printf("%d, %d, %d", a,b,c);
}
#include<stdio.h>
void main()
{
int a, b, c;
a=5, b=4;
c=a&&b;
b=a||b||c;
a=a&&b||c;
printf("%d, %d, %d", a,b,c);
}
Karnataka PUCTextbookLong· 5mImportance★★★★★est
35% · 54/154 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →[!TLDR]
Logical && and || return 1 for true and 0 for false; because a, b and c are all non-zero throughout, each assignment stores 1.
A logical operator regards any non-zero operand as true and zero as false, and produces 1 (true) or 0 (false). Tracing the program with a = 5 and b = 4:
c = a && b;: 5 && 4 — both operands non-zero, so c = 1.b = a || b || c;: 5 || 4 || 1 — at least one operand is non-zero, so b = 1.a = a && b || c;: here && has higher precedence than ||, so it is (5 && 1) || 1 = 1 || 1 = 1, so a = 1.
Finally printf displays a, b, c. A complete standard version is:
#include <stdio.h>
int main(void)
{
int a, b, c;
a = 5, b = 4;
c = a && b; …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.