OPERATORS IN C LANGUAGE
Question Bank with Correct Code Answers | 23 Programs | [Link] CSE
Quick Reference: C Operators Summary
Category Operators Example
Arithmetic +-*/% a+b
Relational == != > < >= <= a>b
Logical && || ! a && b
Bitwise & | ^ ~ << >> a&b
Assignment = += -= *= /= %= a += 5
Increment/Decrement ++ -- a++, --b
Conditional (Ternary) ?: a>b ? a : b
sizeof sizeof() sizeof(int)
Comma , i++, j--
Pointer &* &x, *ptr
PROGRAMS — Questions and Answers
Arithmetic Operators
Q1. Write a C program to perform all arithmetic operations (+, -, *, /, %) on two integers.
■ Code:
#include <stdio.h>
int main() {
int a = 17, b = 5;
printf("Addition: %d\n", a + b);
printf("Subtraction: %d\n", a - b);
printf("Multiplication: %d\n", a * b);
printf("Division: %d\n", a / b);
printf("Modulus: %d\n", a % b);
return 0;
}
Output: Addition: 22 Subtraction: 12 Multiplication: 85 Division: 3 Modulus: 2
Relational Operators
Q2. Write a C program to demonstrate all relational operators (==, !=, >, <, >=, <=).
■ Code:
#include <stdio.h>
int main() {
int a = 10, b = 20;
printf("a == b : %d\n", a == b);
printf("a != b : %d\n", a != b);
printf("a > b : %d\n", a > b);
printf("a < b : %d\n", a < b);
printf("a >= b : %d\n", a >= b);
printf("a <= b : %d\n", a <= b);
return 0;
}
Output: a==b:0 a!=b:1 a>b:0 a<b:1 a>=b:0 a<=b:1
Logical Operators
Q3. Write a C program to illustrate logical operators (&&, ||, !).
■ Code:
#include <stdio.h>
int main() {
int a = 1, 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;
}
Output: a&&b;=0 a||b=1 !a=0 !b=1
Bitwise Operators
Q4. Write a C program to demonstrate bitwise operators (&, |, ^, ~, <<, >>).
■ Code:
#include <stdio.h>
int main() {
int a = 12, b = 10; /* a=1100, b=1010 */
printf("a & b = %d\n", a & b);
printf("a | b = %d\n", a | b);
printf("a ^ b = %d\n", a ^ b);
printf("~a = %d\n", ~a);
printf("a << 1 = %d\n", a << 1);
printf("a >> 1 = %d\n", a >> 1);
return 0;
}
Output: a&b;=8 a|b=14 a^b=6 ~a=-13 a<<1=24 a>>1=6
Assignment Operators
Q5. Write a C program to demonstrate all assignment operators (=, +=, -=, *=, /=, %=).
■ Code:
#include <stdio.h>
int main() {
int a = 20;
printf("a = %d\n", a);
a += 5; printf("a += 5 : %d\n", a);
a -= 3; printf("a -= 3 : %d\n", a);
a *= 2; printf("a *= 2 : %d\n", a);
a /= 4; printf("a /= 4 : %d\n", a);
a %= 3; printf("a %%= 3 : %d\n", a);
return 0;
}
Output: a=20 a+=5:25 a-=3:22 a*=2:44 a/=4:11 a%=3:2
Increment & Decrement Operators
Q6. Write a C program to show the difference between pre-increment and post-increment
operators.
■ Code:
#include <stdio.h>
int main() {
int a = 5, b = 5;
printf("Post-increment: a++ = %d\n", a++);
printf("After post: a = %d\n", a);
printf("Pre-increment: ++b = %d\n", ++b);
printf("After pre: b = %d\n", b);
int x = 10;
printf("Post-decrement: x-- = %d\n", x--);
printf("Pre-decrement: --x = %d\n", --x);
return 0;
}
Output: Post-inc:5 After:6 Pre-inc:6 After:6 Post-dec:10 Pre-dec:8
Ternary (Conditional) Operator
Q7. Write a C program using the ternary operator to find the largest of three numbers.
■ Code:
#include <stdio.h>
int main() {
int a = 15, b = 28, c = 19;
int max;
max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
printf("Largest = %d\n", max);
return 0;
}
Output: Largest = 28
sizeof Operator
Q8. Write a C program to print the size of different data types using sizeof operator.
■ Code:
#include <stdio.h>
int main() {
printf("char : %zu bytes\n", sizeof(char));
printf("int : %zu bytes\n", sizeof(int));
printf("float : %zu bytes\n", sizeof(float));
printf("double : %zu bytes\n", sizeof(double));
printf("long : %zu bytes\n", sizeof(long));
int arr[10];
printf("int[10]: %zu bytes\n", sizeof(arr));
return 0;
}
Output: char:1 int:4 float:4 double:8 long:8 int[10]:40
Comma Operator
Q9. Write a C program to demonstrate the comma operator inside a for loop and in variable
assignment.
■ Code:
#include <stdio.h>
int main() {
int i, j;
/* comma in for loop */
for (i = 0, j = 10; i < 5; i++, j--)
printf("i=%d j=%d\n", i, j);
/* comma in assignment — right-most value assigned */
int x = (5, 10, 20);
printf("x = %d\n", x);
return 0;
}
Output: i=0 j=10 ... i=4 j=6 x=20
Compound Bitwise Assignment Operators
Q10. Write a C program to demonstrate &=, |=, ^=, <<=, >>= operators.
■ Code:
#include <stdio.h>
int main() {
int a = 15; /* binary: 1111 */
printf("Initial a = %d\n", a);
a &= 9; printf("a &= 9 : %d\n", a); /* 1111 & 1001 = 1001 = 9 */
a |= 6; printf("a |= 6 : %d\n", a); /* 1001 | 0110 = 1111 = 15 */
a ^= 5; printf("a ^= 5 : %d\n", a); /* 1111 ^ 0101 = 1010 = 10 */
a <<= 2; printf("a <<= 2 : %d\n", a); /* 10 << 2 = 40 */
a >>= 1; printf("a >>= 1 : %d\n", a); /* 40 >> 1 = 20 */
return 0;
}
Output: a=15 &=9:9 |=6:15 ^=5:10 <<=2:40 >>=1:20
Operator Precedence
Q11. Write a C program to show how operator precedence affects the result of an
expression.
■ Code:
#include <stdio.h>
int main() {
int result;
result = 2 + 3 * 4;
printf("2 + 3 * 4 = %d\n", result); /* 14 */
result = (2 + 3) * 4;
printf("(2 + 3) * 4 = %d\n", result); /* 20 */
result = 10 - 4 / 2 + 1;
printf("10 - 4/2 + 1 = %d\n", result); /* 9 */
result = 8 % 3 + 2 * 5;
printf("8%%3 + 2*5 = %d\n", result); /* 12 */
return 0;
}
Output: 2+3*4=14 (2+3)*4=20 10-4/2+1=9 8%3+2*5=12
Type Casting with Operators
Q12. Write a C program to demonstrate explicit type casting and its effect on division.
■ Code:
#include <stdio.h>
int main() {
int a = 7, b = 2;
printf("Integer division : %d\n", a / b);
printf("Float cast : %.2f\n", (float)a / b);
printf("Double cast : %.6f\n", (double)a / b);
double pi = 3.14159;
printf("double->int cast : %d\n", (int)pi);
return 0;
}
Output: Integer:3 Float:3.50 Double:3.500000 cast:3
Unary Operators
Q13. Write a C program to demonstrate unary plus (+) and unary minus (-) operators.
■ Code:
#include <stdio.h>
int main() {
int a = 10;
printf("a = %d\n", a);
printf("+a = %d\n", +a);
printf("-a = %d\n", -a);
float b = -3.14;
printf("b = %.2f\n", b);
printf("-b = %.2f\n", -b);
return 0;
}
Output: a=10 +a=10 -a=-10 b=-3.14 -b=3.14
Address-of and Dereference Operators
Q14. Write a C program to demonstrate & (address-of) and * (dereference) operators with
pointers.
■ Code:
#include <stdio.h>
int main() {
int x = 42;
int *ptr = &x; /* & gives address of x */
printf("Value of x : %d\n", x);
printf("Address of x : %p\n", &x;);
printf("ptr holds : %p\n", ptr);
printf("*ptr (deref) : %d\n", *ptr);
*ptr = 100; /* modify x via pointer */
printf("x after *ptr=100: %d\n", x);
return 0;
}
Output: Value:42 Address:0x... *ptr:42 x after:100
Bitwise NOT / One's Complement
Q15. Write a C program to find the one's complement and two's complement using bitwise
operators.
■ Code:
#include <stdio.h>
int main() {
unsigned int n = 9; /* binary: 00...1001 */
printf("n = %u\n", n);
printf("Ones complement (~n) = %u\n", ~n);
printf("Twos complement (-n) = %d\n", -((int)n));
/* verify: n + (~n) = all 1s (UINT_MAX) */
printf("n + ~n = %u\n", n + ~n);
return 0;
}
Output: n=9 ~n=4294967286 -n=-9 n+~n=4294967295
Left Shift & Right Shift (Power of 2)
Q16. Write a C program to multiply and divide a number by powers of 2 using shift
operators.
■ Code:
#include <stdio.h>
int main() {
int n = 3;
printf("n = %d\n", n);
printf("n << 1 (x2)= %d\n", n << 1);
printf("n << 2 (x4)= %d\n", n << 2);
printf("n << 3 (x8)= %d\n", n << 3);
int m = 40;
printf("m >> 1 (/2)= %d\n", m >> 1);
printf("m >> 2 (/4)= %d\n", m >> 2);
printf("m >> 3 (/8)= %d\n", m >> 3);
return 0;
}
Output: 3<<1=6 3<<2=12 3<<3=24 40>>1=20 40>>2=10 40>>3=5
Check Even/Odd using Bitwise &
Q17. Write a C program to check if a number is even or odd using the bitwise AND operator.
■ Code:
#include <stdio.h>
int main() {
int nums[] = {4, 7, 12, 19, 0, -3};
int i, n = sizeof(nums)/sizeof(nums[0]);
for (i = 0; i < n; i++) {
if (nums[i] & 1)
printf("%d is Odd\n", nums[i]);
else
printf("%d is Even\n", nums[i]);
}
return 0;
}
Output: 4 Even 7 Odd 12 Even 19 Odd 0 Even -3 Odd
Swap Two Numbers without Temp (XOR)
Q18. Write a C program to swap two integers without a temporary variable using XOR
operator.
■ Code:
#include <stdio.h>
int main() {
int a = 25, b = 40;
printf("Before: a = %d, b = %d\n", a, b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("After: a = %d, b = %d\n", a, b);
return 0;
}
Output: Before: a=25 b=40 After: a=40 b=25
Evaluate Complex Expression
Q19. Write a C program to evaluate a complex expression and explain step by step: result =
(a++ + --b) * (c / d) - e % f
■ Code:
#include <stdio.h>
int main() {
int a=5, b=8, c=20, d=4, e=15, f=6;
int result;
printf("Before: a=%d b=%d c=%d d=%d e=%d f=%d\n",a,b,c,d,e,f);
result = (a++ + --b) * (c / d) - e % f;
/* Step: (5 + 7) * (5) - 3 = 12*5 - 3 = 57 */
printf("result = %d\n", result);
printf("After: a=%d b=%d\n", a, b);
return 0;
}
Output: result = 57 After: a=6 b=7
Conditional Operator — Grade Calculator
Q20. Write a C program using nested ternary operators to assign a grade based on marks.
■ Code:
#include <stdio.h>
int main() {
int marks;
printf("Enter marks (0-100): ");
scanf("%d", &marks;);
char *grade = (marks >= 90) ? "A+" :
(marks >= 80) ? "A" :
(marks >= 70) ? "B" :
(marks >= 60) ? "C" :
(marks >= 50) ? "D" : "F";
printf("Grade = %s\n", grade);
return 0;
}
Output: Enter marks: 85 Grade = A
Bit Masking with Bitwise Operators
Q21. Write a C program to set, clear, and toggle a specific bit using bitwise operators.
■ n=0101; setting bit1 -> 0111=7; clearing bit1 -> 0101=5; toggling bit1 -> 0111=7
■ Code:
#include <stdio.h>
int main() {
int n = 5; /* binary: 0101 */
int pos = 1; /* bit position to operate on */
/* Set bit */
int set = n | (1 << pos);
/* Clear bit */
int clear = n & ~(1 << pos);
/* Toggle bit */
int tog = n ^ (1 << pos);
printf("Original : %d\n", n);
printf("Set bit1: %d\n", set);
printf("Clr bit1: %d\n", clear);
printf("Tog bit1: %d\n", tog);
return 0;
}
Output: Original:5 Set bit1:7 Clear bit1:5 Toggle bit1:7
Short-Circuit Evaluation
Q22. Write a C program to demonstrate short-circuit evaluation in logical && and ||
operators.
■ Code:
#include <stdio.h>
int check(int x) {
printf(" check(%d) called\n", x);
return x;
}
int main() {
printf("--- Short-circuit AND ---\n");
if (check(0) && check(1)) /* check(1) NOT called */
printf("Both true\n");
else
printf("Short-circuited\n");
printf("--- Short-circuit OR ---\n");
if (check(1) || check(0)) /* check(0) NOT called */
printf("Short-circuited\n");
return 0;
}
Output: check(0) called -> Short-circuited check(1) called -> Short-circuited
Count Set Bits using Bitwise Operator
Q23. Write a C program to count the number of 1-bits (set bits) in an integer using &
operator.
■ Code:
#include <stdio.h>
int main() {
int n, count = 0;
printf("Enter a number: ");
scanf("%d", &n;);
int temp = n;
while (temp) {
count += temp & 1; /* check LSB */
temp >>= 1; /* right shift by 1 */
}
printf("Set bits in %d = %d\n", n, count);
return 0;
}
Output: Enter: 29 (11101) Set bits = 4
All programs compiled with GCC | Outputs may vary slightly based on system architecture | Unicode sub/superscripts avoided for
compatibility