Back to C Programming
2026-07-126 min read

operators (C Programming)

Learn operators (C Programming) step by step with clear examples and exercises.

Why This Matters

In this full guide, we'll delve into the world of C operators, a fundamental aspect of the C programming language that every programmer should master to write efficient and effective code. We will cover the core concept, provide a worked example, discuss common mistakes, offer practice questions, and answer frequently asked questions.

Why This Matters

Understanding C operators is essential for any C programmer because they enable you to perform various operations on data, such as arithmetic calculations, comparisons, bitwise manipulations, and more. Mastering these operators will help you write cleaner, more efficient code and solve real-world programming problems effectively. Additionally, a solid grasp of C operators is crucial for acing coding interviews, debugging complex programs, and understanding low-level programming concepts.

Prerequisites

Before diving into the core concept, it's important to have a basic understanding of:

  1. C syntax and variables
  2. Basic data types in C (int, float, char)
  3. Control structures like if-else statements and loops

If you're not familiar with these concepts, we recommend reviewing them before proceeding.

Core Concept

C operators can be categorized into several groups: arithmetic, unary, relational, logical, bitwise, assignment, increment, decrement, ternary, and sizeof. Let's explore each category in detail.

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations on numerical values. The following arithmetic operators are available in C:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%, returns the remainder of the division)
  • Increment (++, pre-increment and post-increment)
  • Decrement (--, pre-decrement and post-decrement)
int a = 5;
int b = 3;

// Pre-increment example
++a; // a becomes 6
printf("%d\n", a); // Output: 6

// Post-increment example
int c = a++; // c becomes 5, a becomes 6
printf("%d %d\n", c, a); // Output: 5 6

Unary Operators

Unary operators operate on a single operand. C provides the following unary operators:

  • Negation (-)
  • Bitwise NOT (~)
  • Address-of (&)
  • Indirect-dereference (*)
  • Typecasting (static_cast, dynamic_cast, const_cast, reinterpret_cast)
int a = 5;
int b = -a; // Negation example
printf("%d\n", b); // Output: -5

char c = 'A';
printf("%d\n", (int)c); // Typecasting example
// Output: 65, the ASCII value of 'A'

Relational Operators

Relational operators are used to compare two operands and return a boolean result. The following relational operators are available in C:

  • Equal (==)
  • Not equal (!=)
  • Greater than (>)
  • Less than (<)
  • Greater than or equal to (>=)
  • Less than or equal to (<=)
int a = 5;
int b = 3;

if(a > b) {
printf("a is greater than b\n");
} else if(a < b) {
printf("a is less than b\n");
} else {
printf("a and b are equal\n");
}

Logical Operators

Logical operators allow you to combine relational expressions or boolean values. C provides the following logical operators:

  • Logical AND (&&)
  • Logical OR (||)
  • Logical NOT (!)
int a = 5;
int b = 3;

if(a > 4 && b < 6) {
printf("Both conditions are true\n");
}

Bitwise Operators

Bitwise operators perform operations on the individual bits of binary representations. C provides the following bitwise operators:

  • Bitwise AND (&)
  • Bitwise OR (|)
  • Bitwise XOR (\^)
  • Bitwise NOT (~)
  • Bitwise left shift (<<)
  • Bitwise right shift (>>)
int a = 60; // binary: 0011 1100
int b = 13; // binary: 0000 1101

// Bitwise AND example
int c = a & b; // binary: 0000 1100 (both bits are set to 1 in the same position)
printf("%d\n", c); // Output: 12

Assignment Operators

Assignment operators are used to assign values to variables. C provides several shorthand assignment operators, such as:

  • Simple assignment (=)
  • Addition assignment (+=)
  • Subtraction assignment (-=)
  • Multiplication assignment (\*=)
  • Division assignment (/=)
  • Modulus assignment (%=)
  • Bitwise AND assignment (&=)
  • Bitwise OR assignment (|=)
  • Bitwise XOR assignment (\^=)
  • Bitwise left shift assignment (<<=)
  • Bitwise right shift assignment (>>=)
int a = 5;
a += 3; // Equivalent to: a = a + 3
printf("%d\n", a); // Output: 8

Increment and Decrement Operators

Increment (++) and decrement (--) operators are shorthand for adding or subtracting one from a variable. C provides both pre-increment/decrement and post-increment/decrement versions.

Ternary Operator

The ternary operator is a shorthand for an if-else statement that assigns the result of an expression to a variable based on a condition. The syntax is:

condition ? expression1 : expression2;

int a = 5;
int b = (a > 4) ? 10 : 20; // If a > 4, b becomes 10; otherwise, b becomes 20
printf("%d\n", b); // Output: 10

sizeof Operator

The sizeof operator returns the size of a data type or variable in bytes.

int a = 5;
printf("%zu\n", sizeof(a)); // Output: 4 (assuming a 32-bit integer)

Worked Example

Let's create a simple program that demonstrates the use of various C operators.

#include <stdio.h>

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

// Arithmetic operations
printf("a + b: %d\n", a + b); // Output: 8
printf("a - b: %d\n", a - b); // Output: 2
printf("a * b: %d\n", a * b); // Output: 15
printf("a / b: %d\n", a / b); // Output: 1
printf("a %% b: %d\n", a % b); // Output: 2

// Unary operators
int c = -a;
printf("Negation of a: %d\n", c); // Output: -5

char d = 'A';
printf("%d\n", (int)d); // Typecasting example
// Output: 65, the ASCII value of 'A'

// Relational operators
if(a > b) {
printf("a is greater than b\n");
} else if(a < b) {
printf("a is less than b\n");
} else {
printf("a and b are equal\n");
}

// Logical operators
int e = 4;
int f = 6;

if(e > 4 && f < 8) {
printf("Both conditions are true\n");
}

// Bitwise operators
int g = 60;
int h = 13;

// Bitwise AND example
int i = g & h;
printf("%d\n", i); // Output: 12

// Assignment operators
int j = 5;
j += 3;
printf("%d\n", j); // Output: 8

return 0;
}

Common Mistakes

  1. Forgetting semicolons: Semicolons are required at the end of every statement in C, except for control structures like if, while, and for.
  2. Mixing arithmetic and bitwise operators: Be careful when mixing arithmetic and bitwise operators in the same expression, as they have different precedence levels.
  3. Incorrect use of relational operators: Ensure that you're using the correct relational operator (== vs =) for comparison and assignment.
  4. Misunderstanding unary operators: Be aware of the difference between pre-increment/decrement and post-increment/decrement, as they have different effects on the order of operations.
  5. Ignoring operator precedence: Understand the precedence of C operators to avoid unexpected results in complex expressions.

Practice Questions

  1. Write a program that calculates the sum of two integers using addition and subtraction operators.
  2. Create a program that finds the larger of two numbers using relational operators.
  3. Write a program that swaps the values of two variables using bitwise AND, OR, and XOR operators.
  4. Implement a program that calculates the factorial of a number using multiplication assignment operator.
  5. Create a program that checks if a number is even or odd using modulus operator.

FAQ

What is the difference between pre-increment/decrement and post-increment/decrement operators?

Pre-increment/decrement operators increment/decrement the variable before the operation, while post-increment/decrement operators perform the operation after the operation.

Why should I be careful when mixing arithmetic and bitwise operators in expressions?

Arithmetic and bitwise operators have different precedence levels, which can lead to unexpected results if not handled correctly.

What is operator precedence, and why is it important in C programming?

Operator precedence defines the order in which operations are performed when multiple operators appear in the same expression. Understanding operator precedence is essential for writing correct and efficient code in C.

How can I find the maximum of two numbers using bitwise operators in C?

You can use the bitwise OR operator (|) to find the maximum of two numbers, as it sets the result to the higher value of the corresponding bits.

What is the difference between == and = operators in C?

The == operator compares two values for equality, while the = operator assigns a value to a variable.