For working professionals
For fresh graduates
More
5. Array in C
13. Boolean in C
18. Operators in C
33. Comments in C
38. Constants in C
41. Data Types in C
49. Double In C
58. For Loop in C
60. Functions in C
70. Identifiers in C
81. Linked list in C
83. Macros in C
86. Nested Loop in C
97. Pseudo-Code In C
100. Recursion in C
103. Square Root in C
104. Stack in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
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!
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.
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:
Mainly there are 7 types of Operators in C, each designed for specific operations:
For a deeper understanding, let’s discuss these seven C operators in detail.
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 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 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 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 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.
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.
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 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 *.
Operators follow rules to determine evaluation order.
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.
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.
Operators are essential in calculations, decision-making, loops, data manipulation, and more. Without them, programming logic cannot be expressed.
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.
Operators simplify code by reducing the need for verbose expressions. They improve readability and make complex logic more manageable with concise syntax.
Avoiding these mistakes improves code accuracy.
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.
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.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
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.