View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Operators in C Explained: Master Every Operator with Easy Examples!

Updated on 06/05/20252,575 Views

When writing C programs, you often need to perform different operations on data. These operations can involve arithmetic calculations, comparisons, logic checks, or even bit manipulation. To handle these actions effectively, you need a solid understanding of operators in C. Without them, writing even simple programs would become tedious and error-prone. 

Throughout your coding journey, you’ll encounter situations where using the right operator makes your program concise, efficient, and correct. 

This article explores operators in C in depth, covering their syntax, types, working principles, and real-world applications with clear examples.

Pursue our Software Engineering courses to get hands-on experience!

What Are Operators in C?

In C programming, operators are special symbols or keywords used to perform operations on variables and values. Operators form expressions and dictate how data is processed. They work on operands and return results based on the type of operation.

Syntax of Operators in C

The basic syntax of an operator involves placing it between or alongside operands. Here's a simple syntax:

result = operand1 operator operand2;

For example:

int sum = 10 + 5;

Here, + is the operator, 10 and 5 are operands, and sum stores the result.

Elevate your skills with these leading online programs:

How Many Types of Operators Are There in C? 

Mainly there are 7 types of Operators in C, each designed for specific operations:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Increment and Decrement Operators
  • Conditional (Ternary) Operator

For a deeper understanding, let’s discuss these seven C operators in detail.

Arithmetic Operators in C

Arithmetic operators perform basic mathematical operations like addition, subtraction, multiplication, division, and modulus.

Operator

Description

+

Addition

-

Subtraction

*

Multiplication

/

Division

%

Modulus

Example:

#include <stdio.h>
int main() {
    int a = 20, b = 6;
    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 = 26
a - b = 14
a * b = 120
a / b = 3
a % b = 2

Explanation: This example shows basic arithmetic operations between two integers a and b.

Relational Operators in C

Relational operators compare two values and return a boolean result (true or false).

Operator

Meaning

==

Equal to

!=

Not equal to

>

Greater than

<

Less than

>=

Greater or equal

<=

Less or equal

Example:

#include <stdio.h>
int main() {
    int x = 5, y = 10;
    printf("%d\n", x > y);
    printf("%d\n", x < y);
    printf("%d\n", x == y);
    return 0;
}

Output:

0
1
0

Explanation: x > y returns false (0), x < y returns true (1), x == y returns false (0).

Logical Operators in C

Logical operators combine or invert boolean expressions.

Operator

Meaning

&&

Logical AND

||

Logical OR

!

Logical NOT

Example:

#include <stdio.h>
int main() {
    int p = 1, q = 0;
    printf("%d\n", p && q);
    printf("%d\n", p || q);
    printf("%d\n", !p);
    return 0;
}

Output:

0
1
0

Explanation: AND returns false, OR returns true, NOT negates p (1 → 0).

Bitwise Operators in C

Bitwise operators perform operations on bits of integers.

Operator

Meaning

&

AND

|

OR

^

XOR

~

NOT

<<

Left shift

>>

Right shift

Example:

#include <stdio.h>
int main() {
    int a = 5, b = 9;
    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("b << 1 = %d\n", b << 1);
    printf("b >> 1 = %d\n", b >> 1);
    return 0;
}

Output:

a & b = 1
a | b = 13
a ^ b = 12
~a = -6
b << 1 = 18
b >> 1 = 4

Explanation: This example shows bitwise operations on a and b.

Assignment Operators in C

Assignment operators assign values to variables.

Operator

Meaning

=

Assign

+=

Add and assign

-=

Subtract and assign

*=

Multiply and assign

/=

Divide and assign

%=

Modulus and assign

Example:

#include <stdio.h>
int main() {
    int num = 10;
    num += 5;
    printf("%d\n", num);
    num *= 2;
    printf("%d\n", num);
    return 0;
}

Output:

15
30

Explanation: num increases by 5, then doubles.

Increment and Decrement Operators in C

These operators increase or decrease a variable’s value by one.

Operator

Meaning

++

Increment

--

Decrement

Example:

#include <stdio.h>
int main() {
    int count = 5;
    printf("%d\n", count++);
    printf("%d\n", ++count);
    printf("%d\n", count--);
    printf("%d\n", --count);
    return 0;
}

Output:

5
7
7
5

Explanation: Shows pre and post increment/decrement behavior.

Conditional (Ternary) Operator in C

The ternary operator can simplify conditional expressions. Discover how to use it effectively in our guide on the Ternary Operator in C. A shorthand for if-else condition.

Syntax:

(condition) ? expression1 : expression2;

Example:

#include <stdio.h>
int main() {
    int a = 10, b = 20;
    int max = (a > b) ? a : b;
    printf("Max: %d\n", max);
    return 0;
}

Output:

Max: 20

Explanation: Returns the greater value between a and b.

Other Operators in C

Other operators include:

Operator

Use

sizeof

Get size

&

Address

*

Pointer dereference

Example:

#include <stdio.h>
int main() {
    int num = 10;
    printf("Size of int: %lu\n", sizeof(num));
    printf("Address of num: %p\n", &num);
    printf("Value using pointer: %d\n", *(&num));
    return 0;
}

Output:

Size of int: 4
Address of num: 0x7ffd45f8c7ec
Value using pointer: 10

Explanation: Shows use of sizeof, &, and *.

How Do Operators Work in C?

Operators follow rules to determine evaluation order.

Operator Precedence in C

Higher precedence operators are evaluated first. For example, * and / have higher precedence than + and -.

Example:

#include <stdio.h>
int main() {
    int result = 10 + 5 * 2;
    printf("%d\n", result);
    return 0;
}

Output:

20

Explanation: 5 * 2 is evaluated first.

Operator Associativity in C

When operators have the same precedence, associativity determines evaluation direction. Understanding operator precedence and associativity is crucial when dealing with complex expressions in C. Learn more about this in our section on Operator Precedence and Associativity in C.

Example:

#include <stdio.h>
int main() {
    int x = 5, y = 10, z = 15;
    int result = x + y + z;
    printf("%d\n", result);
    return 0;
}

Output:

30

Explanation: + is left-associative, evaluated left to right.

What Are the Real-World Applications of Operators in C?

Operators are essential in calculations, decision-making, loops, data manipulation, and more. Without them, programming logic cannot be expressed.

Example of Operators in C Applications

Example:

#include <stdio.h>
int main() {
    int marks = 85;
    char grade = (marks >= 90) ? 'A' : (marks >= 75) ? 'B' : 'C';
    printf("Grade: %c\n", grade);
    return 0;
}

Output:

Grade: B

Explanation: Uses conditional operator for grading logic.

Operators can often be combined with functions. Learn more about functions in C programming by visiting our Function in C Programming guide.

What Are the Advantages of Using Operators in C?

Operators simplify code by reducing the need for verbose expressions. They improve readability and make complex logic more manageable with concise syntax.

What Are the Common Mistakes with Operators in C?

  • Misunderstanding operator precedence
  • Confusing = with ==
  • Incorrect use of increment/decrement in expressions
  • Using bitwise instead of logical operators (& vs &&)

Avoiding these mistakes improves code accuracy.

Conclusion

Operators are an essential part of C programming. They form the core tools for performing operations on data. Whether you're adding numbers, comparing values, checking conditions, or manipulating bits, you rely on operators to get the job done.

A strong grasp of operators in C makes it easier to write logical, efficient, and error-free code. It also helps you debug issues related to precedence, associativity, and expression evaluation.

By learning the types of operators, their syntax, precedence rules, and practical uses, you can write cleaner, more optimized programs that work as intended. Mastering operators early in your programming journey lays a solid foundation for tackling more advanced topics in C and other programming languages.

FAQs

1. What is the main purpose of operators in C?

Operators in C help perform mathematical, logical, relational, and bit-level operations on data values or variables, making expressions easier to write and evaluate efficiently within programs.

2. How do arithmetic operators in C work?

Arithmetic operators in C perform basic mathematical operations like addition, subtraction, multiplication, division, and modulus directly on variables or constants within expressions.

3. What is operator precedence in C?

Operator precedence in C defines the order in which different operators are evaluated in an expression. Operators with higher precedence are evaluated before operators with lower precedence.

4. Why is operator associativity important in C?

Operator associativity determines the direction (left-to-right or right-to-left) in which operators of the same precedence level are evaluated in a C expression, ensuring correct evaluation.

5. Can we overload operators in C?

No, operator overloading is not supported in C. Operator overloading is a feature available in C++ but not in the C programming language.

6. What is a compound assignment operator in C?

A compound assignment operator combines an arithmetic operator with the assignment operator (like +=, -=) to perform an operation and assign the result in a single step.

7. What does the sizeof operator do in C?

The sizeof operator in C returns the size, in bytes, of a data type or variable. It helps determine memory allocation and usage at compile time.

8. How does the comma operator work in C?

The comma operator evaluates two expressions but returns the value of the second expression. It allows multiple expressions within a statement, mainly inside loops.

9. What are bitwise shift operators in C?

Bitwise shift operators in C include left shift (<<) and right shift (>>). They shift bits of an integer value left or right, useful in low-level programming and optimizations.

10. What is the difference between == and = in C?

In C, == is a relational operator used to compare two values for equality, while = is an assignment operator used to assign a value to a variable.

11. Can operators be used with pointers in C?

Yes, operators like *, &, ++, and -- can be used with pointers in C for dereferencing, getting addresses, and pointer arithmetic operations.

12. Why are logical operators important in C?

Logical operators in C (&&, ||, !) are essential for building complex conditional expressions that control program flow in decision-making and loop statements.

13. What is the ternary operator in C used for?

The ternary operator (? :) in C is a shorthand for if-else conditions. It evaluates a condition and returns one of two values based on whether the condition is true or false.

14. How are operators used in conditional statements in C?

Operators like relational, logical, and ternary operators are commonly used inside if, else, while, and for statements to build conditions for decision-making.

15. What are the common errors when using operators in C?

Common errors include confusion between = and ==, misunderstanding precedence, forgetting parentheses in expressions, and incorrect use of bitwise versus logical operators.

image

Take a Free C Programming Quiz

Answer quick questions and assess your C programming knowledge

right-top-arrow
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
advertise-arrow

Free Courses

Start Learning For Free

Explore Our Free Software Tutorials and Elevate your Career.

upGrad Learner Support

Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918068792934

Disclaimer

1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.

2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.