C - Operators
C - Operators
C - Operators
An operator is a symbol that tells the compiler to perform specific mathematical or
logical functions. By definition, an operator performs a certain operation on
operands. An operator needs one or more operands for the operation to be
performed.
Depending on how many operands are required to perform the operation, operands
are called as unary, binary or ternary operators. They need one, two or three
operands respectively.
C language is rich in built-in operators and provides the following types of operators
−
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
We will, in this chapter, look into the way each operator works. Here, you will get an
overview of all these chapters. Thereafter, we have provided independent chapters
on each of these operators that contain plenty of examples to show how these
operators work in C Programming.
Arithmetic Operators
We are most familiar with the arithmetic operators. These operators are used to
perform arithmetic operations on operands. The most common arithmetic operators
are addition (+), subtraction (-), multiplication (*), and division (/).
https://www.tutorialspoint.com/cprogramming/c_operators.htm 1/9
6/16/24, 11:04 AM C - Operators
In addition, the modulo (%) is an important arithmetic operator that computes the
remainder of a division operation. Arithmetic operators are used in forming an
arithmetic expression. These operators are binary in nature in the sense they need
two operands, and they operate on numeric operands, which may be numeric
literals, variables or expressions.
a+b
Here "+" is an arithmetic operator. We shall learn more about arithmetic operators in
C in a subsequent chapter.
The following table shows all the arithmetic operators supported by the C language.
Assume variable A holds 10 and variable B holds 20 then −
Show Examples
Relational Operators
We are also acquainted with relational operators while learning secondary
mathematics. These operators are used to compare two operands and return a
boolean value (true or false). They are used in a boolean expression.
The most common relational operators are less than (<), greater than (>), less than
or equal to (<=), greater than or equal to (>=), equal to (==), and not equal to
https://www.tutorialspoint.com/cprogramming/c_operators.htm 2/9
6/16/24, 11:04 AM C - Operators
(!=). Relational operators are also binary operators, needing two numeric operands.
a>b
We shall learn more about with relational operators and their usage in one of the
following chapters.
Show Examples
Logical Operators
These operators are used to combine two or more boolean expressions. We can form
a compound Boolean expression by combining Boolean expression with these
operators. An example of logical operator is as follows −
https://www.tutorialspoint.com/cprogramming/c_operators.htm 3/9
6/16/24, 11:04 AM C - Operators
The most common logical operators are AND (&&), OR(||), and NOT (!). Logical
operators are also binary operators.
Show Examples
Bitwise Operators
Bitwise operators let you manipulate data stored in computer’s memory. These
operators are used to perform bit-level operations on operands.
The most common bitwise operators are AND (&), OR (|), XOR (^), NOT (~), left
shift (<<), and right shift (>>). Here the "~" operator is a unary operator, while
most of the other bitwise operators are binary in narure.
Bitwise operator works on bits and perform bit−by−bit operation. The truth tables
for &, "|", and "^" are as follows −
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
https://www.tutorialspoint.com/cprogramming/c_operators.htm 4/9
6/16/24, 11:04 AM C - Operators
1 0 0 1 1
A = 0011 1100
B = 0000 1101
------------------------
~A = 1100 0011
The following table lists the bitwise operators supported by C. Assume variable 'A'
holds 60 and variable 'B' holds 13, then −
Show Examples
Binary AND Operator copies a bit to the (A & B) = 12, i.e., 0000
&
result if it exists in both operands. 1100
https://www.tutorialspoint.com/cprogramming/c_operators.htm 5/9
6/16/24, 11:04 AM C - Operators
Assignment Operators
As the name suggests, an assignment operator "assigns" or sets a value to a named
variable in C. These operators are used to assign values to variables. The "=" symbol
is defined as assignment operator in C, however it is not to be confused with its
usage in mathematics.
The following table lists the assignment operators supported by the C language −
Show Examples
C <<= 2 is same as C
<<= Left shift AND assignment operator.
= C << 2
C >>= 2 is same as C
>>= Right shift AND assignment operator.
= C >> 2
https://www.tutorialspoint.com/cprogramming/c_operators.htm 6/9
6/16/24, 11:04 AM C - Operators
C &= 2 is same as C =
&= Bitwise AND assignment operator.
C&2
Hence, the expression "a = 5" assigns 5 to the variable "a", but "5 = a" is an invalid
expression in C.
The "=" operator, combined with the other arithmetic, relational and bitwise
operators form augmented assignment operators. For example, the += operator is
used as add and assign operator. The most common assignment operators are =,
+=, -=, *=, /=, %=, &=, |=, and ^=.
Show Examples
Operators Precedence in C
Operator precedence determines the grouping of terms in an expression and decides
how an expression is evaluated. Certain operators have higher precedence than
others; for example, the multiplication operator has a higher precedence than the
addition operator.
https://www.tutorialspoint.com/cprogramming/c_operators.htm 7/9
6/16/24, 11:04 AM C - Operators
Here, operators with the highest precedence appear at the top of the table, those
with the lowest appear at the bottom. Within an expression, higher precedence
operators will be evaluated first.
Show Examples
Other Operators in C
Apart from the above, there are a few other operators in C that are not classified
into any of the above categories. For example, the increment and decrement
operators (++ and --) are unary in nature and can appear as a prefix or postfix to
the operand.
https://www.tutorialspoint.com/cprogramming/c_operators.htm 8/9
6/16/24, 11:04 AM C - Operators
The operators that work with the address of memory location such as the address-of
operator (&) and the dereference operator (*). The sizeof operator (sizeof) appears
to be a keyword but really an operator.
C also has the type cast operator (()) that forces the type of an operand to be
changed. C also uses the dot (.) and the arrow (->) symbols as operators when
dealing with derived data types such as struct and union.
The C99 version of C introduced a few additional operators such as auto, decltype.
a+b*c
Many other programming languages, which are called C-family languages (such as
C++, C#, Java, Perl and PHP) have an operator nomenclature that is similar to C.
https://www.tutorialspoint.com/cprogramming/c_operators.htm 9/9