Skip to content
“`html C Language – Operators

C Language Operators

Complete Notes, Examples, Programs and Practice Questions

1. What is an Operator?

Operator: An operator is a symbol that tells the computer to perform an operation.

An expression is a combination of operators and operands that is evaluated to produce a result.

Example

2 + 3
        
  • 2 + 3 → Expression
  • + → Operator
  • 2 and 3 → Operands

Examples of Expressions

Expression Explanation
2 + 3 Two constants are added.
x + 10 A variable and a constant are added.
x + y Two variables are added.
(x + y) * z The result of x + y is multiplied by z.
Remember: An operand can be a constant, variable, expression, or a combination of these.

2. Types of Operators Based on Number of Operands

Type Number of Operands Example Explanation
Unary 1 ++x Works with one operand.
Binary 2 x + y Works with two operands.
Ternary 3 a ? b : c Conditional operator works with three expressions.
Important: The number of operands is different from the number of symbols. For example, ++x has one operand, so it is a unary operator.

3. Types of C Operators Based on Operation

# Operator Category Examples
1 Arithmetic Operators + - * / %
2 Assignment Operators = += -= *= /= %=
3 Increment / Decrement ++ --
4 Relational / Comparison > >= < <= == !=
5 Logical Operators && || !
6 Bitwise Operators & | ~ ^ << >>

4. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations.

Operator Name Example Result
+ Addition 5 + 2 7
Subtraction 5 - 2 3
* Multiplication 5 * 2 10
/ Division 5 / 2 2 for integers
% Modulus 5 % 2 1
Important: When both operands are integers, integer division is performed. Therefore 5 / 2 gives 2, not 2.5.

Example Program

#include <stdio.h>

int main()
{
    int res;

    res = 5 + 2;
    printf("Result = %d\n", res);

    res = 5 - 2;
    printf("Result = %d\n", res);

    res = 5 * 2;
    printf("Result = %d\n", res);

    res = 5 / 2;
    printf("Result = %d\n", res);

    res = 5 % 2;
    printf("Result = %d\n", res);

    return 0;
}
        

Output

Result = 7 Result = 3 Result = 10 Result = 2 Result = 1

5. Assignment Operator

The = symbol is called the assignment operator in C. It assigns the value on the right-hand side to the variable on the left-hand side.

Syntax

variable = value;
variable = expression;
variable = another_variable;
        

Example

#include <stdio.h>

int main()
{
    int a, b;

    a = 12;
    b = 24;

    printf("a = %d\n", a);
    printf("b = %d\n", b);

    a = b;

    printf("After a = b:\n");
    printf("a = %d\n", a);
    printf("b = %d\n", b);

    return 0;
}
        
a = 12 b = 24 After a = b: a = 24 b = 24
Do not confuse: = means assignment, while == means comparison.

6. Swapping Two Variables

Swapping means exchanging the values of two variables.

Using a Temporary Variable

#include <stdio.h>

int main()
{
    int x, y, temp;

    x = 12;
    y = 24;

    printf("Before swapping:\n");
    printf("x = %d\n", x);
    printf("y = %d\n", y);

    temp = x;
    x = y;
    y = temp;

    printf("\nAfter swapping:\n");
    printf("x = %d\n", x);
    printf("y = %d\n", y);

    return 0;
}
        
Logic:
temp = x;
x = y;
y = temp;

7. Short-Hand / Compound Assignment Operators

Compound assignment operators provide a shorter way of writing an expression where the same variable appears on both sides of an assignment.

Short Form Equivalent Form
x += 5 x = x + 5
x -= 5 x = x - 5
x *= 5 x = x * 5
x /= 5 x = x / 5
x %= 5 x = x % 5

Example

#include <stdio.h>

int main()
{
    int x = 10;
    int y = 20;

    printf("x = %d\n", x);

    x += 2;
    printf("After x += 2, x = %d\n", x);

    x += y;
    printf("After x += y, x = %d\n", x);

    y += x;
    printf("After y += x, y = %d\n", y);

    return 0;
}
        

8. Type Conversion and Type Casting

Type conversion occurs when a value of one data type is converted into another data type.

Implicit Type Conversion

When the compiler automatically converts one data type into another, it is called implicit type conversion.

int a = 10;
float b;

b = a;
        

Here, the integer value 10 is automatically converted to a floating-point value.

Explicit Type Conversion / Type Casting

When the programmer explicitly specifies the required data type, it is called type casting.

float x;

x = (float)'a';

printf("%f", x);
        
The cast (float) explicitly converts the character value into a floating-point value.

Example: Integer Division vs Type Casting

#include <stdio.h>

int main()
{
    int a = 5;
    int b = 2;

    printf("Integer division = %d\n", a / b);
    printf("Floating division = %.2f\n", (float)a / b);

    return 0;
}
        
Integer division = 2 Floating division = 2.50

9. Type Conversion Practice

Question 1: Write a C program to accept a character from the user and print its ASCII value.
Hint: ASCII value of ‘A’ is 65.
Question 2: Write a C program to accept an integer and a float from the user and display their product.
Question 3: Write a C program to convert temperature from Fahrenheit to Celsius.
C = (F – 32) × 5 / 9
Question 4: Write a program to convert an integer value into a float using explicit type casting.

10. Increment and Decrement Operators

Operator Name Operation
++ Increment Adds 1 to a variable.
-- Decrement Subtracts 1 from a variable.

Increment

x++ or ++x increases the value of x by 1.

Conceptually:

x = x + 1;
        

Decrement

x-- or --x decreases the value of x by 1.

x = x - 1;
        

Example

#include <stdio.h>

int main()
{
    int x = 10;

    printf("x = %d\n", x);

    x++;
    printf("After increment, x = %d\n", x);

    x--;
    printf("After decrement, x = %d\n", x);

    return 0;
}
        

11. Pre-Increment and Post-Increment

Operator Name Meaning
x++ Post-increment Use the current value first, then increment.
++x Pre-increment Increment first, then use the new value.
x-- Post-decrement Use the current value first, then decrement.
--x Pre-decrement Decrement first, then use the new value.

Post-Increment Example

#include <stdio.h>

int main()
{
    int a = 2;

    printf("a = %d\n", a++);
    printf("a = %d\n", a);

    return 0;
}
        
a = 2 a = 3

In a++, the old value is used first and then a is increased.

Another Example

#include <stdio.h>

int main()
{
    int x = 10;
    int y;

    y = x++ + 5;

    printf("x = %d\n", x);
    printf("y = %d\n", y);

    return 0;
}
        
x = 11 y = 15
Easy way to remember:
Post → Use first, change later.
Pre → Change first, use later.

Pre-Increment Example

int x = 10;
int y;

y = ++x + 5;
        

Here x becomes 11 first, so y becomes 16.

12. Relational / Comparison Operators

Relational operators are used to compare two values. The result of a comparison is either true or false.

In C, a true condition generally evaluates to 1, while a false condition evaluates to 0.

Operator Name Example
> Greater than 5 > 3
>= Greater than or equal to 5 >= 5
< Less than 3 < 5
<= Less than or equal to 5 <= 5
== Equal to 5 == 5
!= Not equal to 5 != 3
Very Important:
= → Assignment
== → Comparison

Example Program

#include <stdio.h>

int main()
{
    int res;

    res = 5 > 7;
    printf("5 > 7 = %d\n", res);

    res = 5 >= 7;
    printf("5 >= 7 = %d\n", res);

    res = 5 < 7;
    printf("5 < 7 = %d\n", res);

    res = 5 <= 7;
    printf("5 <= 7 = %d\n", res);

    res = 5 == 7;
    printf("5 == 7 = %d\n", res);

    res = 5 != 7;
    printf("5 != 7 = %d\n", res);

    return 0;
}
        
5 > 7 = 0 5 >= 7 = 0 5 < 7 = 1 5 <= 7 = 1 5 == 7 = 0 5 != 7 = 1

13. Logical / Boolean Operators

Logical operators are used to combine or modify conditions. Their result is either 1 (true) or 0 (false).

Operator Name Meaning
&& Logical AND True only when both conditions are true.
|| Logical OR True when at least one condition is true.
! Logical NOT Reverses the logical result.

Truth Table

A B A && B A || B
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 1
In C, 0 is false and any non-zero value is true when used as a logical value.

Example Program

#include <stdio.h>

int main()
{
    int res;

    res = (5 > 7) && (12 < 20);
    printf("AND result = %d\n", res);

    res = (5 > 7) || (12 < 20);
    printf("OR result = %d\n", res);

    res = !(5 > 7);
    printf("NOT result = %d\n", res);

    return 0;
}
        
AND result = 0 OR result = 1 NOT result = 1

14. Bitwise Operators

Bitwise operators work directly with the individual bits of integer values.

Operator Name Description
& Bitwise AND Sets a bit to 1 when both corresponding bits are 1.
| Bitwise OR Sets a bit to 1 when at least one corresponding bit is 1.
^ Bitwise XOR Sets a bit to 1 when the corresponding bits are different.
~ Bitwise NOT / Complement Flips each bit.
<< Left Shift Shifts bits toward the left.
>> Right Shift Shifts bits toward the right.

Example: Bitwise AND

4 = 0100
5 = 0101
    ----
4 & 5 = 0100 = 4
        

Example Program

#include <stdio.h>

int main()
{
    int res;

    res = 4 & 5;
    printf("4 & 5 = %d\n", res);

    res = 4 << 2;
    printf("4 << 2 = %d\n", res);

    return 0;
}
        
4 & 5 = 4 4 << 2 = 16

Bitwise AND Example

  4 = 0100
  5 = 0101
      ----
4 & 5 = 0100
        = 4
        

Bitwise OR Example

  4 = 0100
  5 = 0101
      ----
4 | 5 = 0101
        = 5
        

Bitwise XOR Example

  4 = 0100
  5 = 0101
      ----
4 ^ 5 = 0001
        = 1
        

Left Shift

4 = 0100

4 << 1 = 1000 = 8
4 << 2 = 10000 = 16
        
For positive integer values, a left shift by one position is commonly equivalent to multiplying by 2, provided no overflow occurs.

15. Quick Revision Table

Category Operators Main Purpose
Arithmetic + - * / % Mathematical operations
Assignment = Assign a value
Compound Assignment += -= *= /= %= Short form of assignment + operation
Increment / Decrement ++ -- Increase/decrease by 1
Relational > >= < <= == != Compare values
Logical && || ! Combine or reverse conditions
Bitwise & | ^ ~ << >> Operate on individual bits

16. Practice Questions

Level 1 – Basic

1. What is an operator?
2. What is an operand? Give two examples.
3. Identify the operator and operands in x + 10.
4. What are unary, binary and ternary operators?
5. What is the difference between = and ==?

Level 2 – Predict the Output

6. Predict the output:
int x = 10;
printf("%d", x++);
            
7. Predict the output:
int x = 10;
printf("%d", ++x);
            
8. Find the values of x and y:
int x = 5;
int y;

y = x++ + 10;
            
9. Find the result:
int x = 5;
int y = 2;

printf("%d", x / y);
printf("%d", x % y);
            
10. What will be the result?
printf("%d", 4 & 5);
            

Level 3 – Write Programs

11. Write a C program to accept two integers and print their sum, difference, product, quotient and remainder.
12. Write a C program to swap two numbers using a temporary variable.
13. Write a C program to swap two numbers without using a third variable.
14. Write a C program to check whether a number is greater than 100.
15. Write a C program to check whether a person is eligible to vote based on age.
16. Write a C program to check whether a number is divisible by both 3 and 5 using the logical AND operator.
17. Write a C program to accept a character and print its ASCII value.
18. Write a C program to convert Fahrenheit temperature into Celsius.
19. Write a C program to demonstrate the difference between pre-increment and post-increment.
20. Write a C program to demonstrate bitwise AND, OR, XOR, left shift and right shift.

17. Challenge Questions

Challenge 1: What is the output?
int a = 5;
int b = 10;

a += b;
b += a;

printf("a = %d\n", a);
printf("b = %d\n", b);
            
Challenge 2: What are the final values of a and b?
int a = 10;
int b = 20;

a++;
++b;

a += b;
b -= 5;
            
Challenge 3: Predict the result:
int a = 4;
int b = 5;

printf("%d\n", a & b);
printf("%d\n", a | b);
printf("%d\n", a ^ b);
            
Challenge 4: Explain why the following produces different results:
printf("%d\n", 5 / 2);
printf("%.1f\n", (float)5 / 2);
            

18. Important Points to Remember

  • = is assignment; == is comparison.
  • 5 / 2 gives 2 when both operands are integers.
  • 5 % 2 gives the remainder 1.
  • Post-increment uses the old value before incrementing.
  • Pre-increment increments the value before it is used.
  • Zero represents false in a logical context.
  • Any non-zero value represents true in a logical context.
  • Relational operators produce 0 or 1 in C.
  • Bitwise operators work on individual bits of integer values.
  • Type casting can be used to explicitly convert a value to another type.
C Language Operators
Learn → Practice → Predict Output → Write Programs
“`