Skip to main content

Operators in C

An operator is a symbol that tells the compiler to perform a specific operation on one or more operands (variables or values).
int sum = a + b;
// '+' is the operator; a and b are operands

Arithmetic Operators

Used to perform mathematical operations (assume a = 10, b = 3):
OperatorNameExampleResult
+Additiona + b13
-Subtractiona - b7
*Multiplicationa * b30
/Divisiona / b3
%Modulusa % b1
/ performs integer division when both operands are integers (drops the decimal). % gives the remainder of that division.

Assignment Operators

Assign values to variables. The right-hand side is evaluated first, then assigned to the left.
int a = 10;           // simple assignment
int result = a + b;   // expression assigned to result
Chained assignment:
int a, b;
a = b = 20;   // b gets 20, then a gets b (both become 20)
Compound assignment:
a += 5;   // a = a + 5
a -= 3;   // a = a - 3
a *= 2;   // a = a * 2
a /= 4;   // a = a / 4
a %= 3;   // a = a % 3

Relational Operators

Compare two values. Result is 1 (true) or 0 (false). Assume a = 10, b = 5:
OperatorDescriptionExampleResult
==Equal toa == b0
!=Not equal toa != b1
>Greater thana > b1
<Less thana < b0
>=Greater or equala >= b1
<=Less or equala <= b0

Logical Operators

Combine multiple conditions:
OperatorNameMeaning
&&Logical ANDTrue if both are true
||Logical ORTrue if either is true
!Logical NOTInverts the condition
int age = 20, hasLicense = 1;
if (age >= 18 && hasLicense == 1)
    printf("Eligible to drive");

Bitwise Operators

Operate at the binary (bit) level:
OperatorNameDescription
&AND1 if both bits are 1
|OR1 if either bit is 1
^XOR1 if bits differ
~NOTFlips all bits
<<Left ShiftShifts bits left
>>Right ShiftShifts bits right
Example:
int a = 12;   // binary: 01100
int b = 25;   // binary: 11001

printf("%d", a & b);   // 01000 = 8
printf("%d", a | b);   // 11101 = 29
printf("%d", a ^ b);   // 10101 = 21
printf("%d", a << 1);  // 11000 = 24 (shift left by 1)
printf("%d", a >> 1);  // 00110 = 6  (shift right by 1)

Increment / Decrement Operators

OperatorTypeEffect
++aPre-incrementIncrement first, then use
a++Post-incrementUse first, then increment
--aPre-decrementDecrement first, then use
a--Post-decrementUse first, then decrement
int a = 5;
printf("%d", ++a);  // prints 6 (increments before print)
printf("%d", a++);  // prints 6 (prints then increments to 7)
printf("%d", a);    // prints 7

Practice Programs

Problem 1: Sum, Difference, Product, Quotient, Remainder
#include <stdio.h>
int main() {
    int a, b;
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);
    printf("Sum       = %d\n", a + b);
    printf("Difference= %d\n", a - b);
    printf("Product   = %d\n", a * b);
    printf("Quotient  = %d\n", a / b);
    printf("Remainder = %d\n", a % b);
    return 0;
}
Problem 2: Average of Three Numbers
#include <stdio.h>
int main() {
    float a, b, c;
    printf("Enter three numbers: ");
    scanf("%f %f %f", &a, &b, &c);
    float avg = (a + b + c) / 3;
    printf("Average = %.2f", avg);
    return 0;
}
Problem 3: Convert Days into Years, Weeks, Days
#include <stdio.h>
int main() {
    int days, years, weeks, remaining;
    printf("Enter total days: ");
    scanf("%d", &days);
    years     = days / 365;
    weeks     = (days % 365) / 7;
    remaining = (days % 365) % 7;
    printf("Years: %d, Weeks: %d, Days: %d", years, weeks, remaining);
    return 0;
}
Problem 4: Check Even or Odd
#include <stdio.h>
int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    if (num % 2 == 0)
        printf("Even");
    else
        printf("Odd");
    return 0;
}
Problem 5: Simple Interest Calculator
#include <stdio.h>
int main() {
    float p, r, t, si;
    printf("Enter Principal, Rate, Time: ");
    scanf("%f %f %f", &p, &r, &t);
    si = (p * r * t) / 100;
    printf("Simple Interest = %.2f", si);
    return 0;
}