C Operators

By | September 23, 2023

C operators are symbols or keywords that are used to perform different operations on one or more values. In C programming language, operators are classified into several categories, including:

1. Arithmetic Operators:
These operators are used to perform basic arithmetic operations such as addition, subtraction, multiplication, division, and modulus (%). Here is a list of all arithmetic operators in C:

+ (addition)
– (subtraction)
* (multiplication)
/ (division)
% (modulus or remainder)

Example –

int a = 10;
int b = 20;

int sum = a + b; // sum = 30
int difference = b - a; // difference = 10
int product = a * b; // product = 200
int quotient = b / a; // quotient = 2
int remainder = b % a; // remainder = 0

2. Assignment Operators:
These operators are used to assign values to variables. In C, the basic assignment operator is =, but there are also shorthand versions of this operator for performing arithmetic and bit-wise operations, such as:

+= (addition assignment)
-= (subtraction assignment)
*= (multiplication assignment)
/= (division assignment)
%= (modulus assignment)
&= (bit-wise AND assignment)
|= (bit-wise OR assignment)
^= (bit-wise XOR assignment)

Example –

int a = 10;
a += 5; // equivalent to a = a + 5;
a -= 3; // equivalent to a = a - 3;
a *= 2; // equivalent to a = a * 2;
a /= 4; // equivalent to a = a / 4;
a %= 3; // equivalent to a = a % 3;

3. Relational Operators:
These operators are used to compare two values and return a boolean result (true or false). The relational operators in C are:

== (equal to)
!= (not equal to)
< (less than) > (greater than)
<= (less than or equal to)
= (greater than or equal to)

Example –

int a = 10;
int b = 20;

bool isEqual = (a == b); // isEqual = false
bool isNotEqual = (a != b); // isNotEqual = true
bool isLessThan = (a < b); // isLessThan = true
bool isGreaterThan = (a > b); // isGreaterThan = false
bool isLessThanOrEqual = (a <= b); // isLessThanOrEqual = true
bool isGreaterThanOrEqual = (a >= b); // isGreaterThanOrEqual = false

4. Logical Operators:
These operators are used to perform logical operations on boolean values (true or false). The logical operators in C are:

&& (logical AND)
|| (logical OR)
! (logical NOT)

Example –

bool a = true;
bool b = false;

bool andResult = (a && b); // andResult = false
bool orResult = (a || b); // orResult = true
bool notResult = !a; // notResult = false

5. Bit-wise Operators:
These operators are used to perform bit-wise operations on integers. The bit-wise operators in C are:

& (bit-wise AND)
| (bit-wise OR)
^ (bit-wise XOR)
~ (bit-wise NOT)
<< (left shift) >>(right shift)

Example –

int a = 5; // 0101 in binary
int b = 3; // 0011 in binary

int andResult = a & b; // andResult = 1 (0001 in binary)
int orResult = a | b; // orResult = 7 (0111 in binary)
int xorResult = a ^ b; // xorResult = 6 (0110 in binary)
int notResult = ~a; // notResult = -6 (1111 1011 in binary)
int leftShiftResult = a << 2; // leftShiftResult = 20 (10100 in binary)
int rightShiftResult = a >> 1; // rightShiftResult = 2 (0010 in binary)

6. Conditional Operator:
This operator is a ternary operator (i.e., it takes three operands) that is used to assign a value based on a condition. The syntax for the conditional operator is:

condition ? true_value : false_value

Example –

int a = 10;
int b = 20;

int max = (a > b) ? a : b; // max = 20

7. Comma Operator:
This operator is used to separate multiple expressions. The comma operator evaluates each expression from left to right and returns the value of the last expression. The syntax for the comma operator is:

expression1, expression2, expression3, …, expressionn

Example –

int a = 10, b = 20, c = 30;

int sum = (a + b, b + c, c + a); // sum = 40

These are the basic operators in C programming language, and they are used extensively in writing C programs.

C Operators Precedence Chart:

Here is the precedence chart of C operators, arranged in descending order of precedence (operators with higher precedence are evaluated first):

Precedence Operator Description
1 () [] -> . Parentheses, Array subscripting, Structure/Union member access
2 ++ — Postfix increment and decrement
3 ++ — Prefix increment and decrement
4 + – ! ~ Unary plus, Unary minus, Logical NOT, Bitwise NOT
5 * / % Multiplication, Division, Modulo
6 + – Addition, Subtraction
7 << >> Bitwise Left shift, Right shift
8 < <= > >= Relational Operators (less than, less than or equal to, greater than, greater than or equal to)
9 == != Equality and Inequality
10 & Bitwise AND
11 ^ Bitwise XOR
12 | Bitwise OR
13 && Logical AND
14 || Logical OR
15 ?: Conditional
16 = += -= *= /= %= <<= >>= &= ^= |= Assignment and compound assignment


Note that parentheses can be used to override the operator precedence and force a certain order of evaluation.

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Author: Mithlesh Upadhyay

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.