An operator is a symbol that tells the compiler to perform a certain operation (arithmetic, comparison, etc.) using the values provided along with the operator. The values and variables used with operators are called operands. So we can say that the operators are the symbols that perform operations on operands.
For example :
c = a + b;
/*
Here, ‘+’ is the operator known as the addition operator, and ‘a’ and ‘b’ are operands. The addition operator tells the compiler to add both of the operands ‘a’ and ‘b’.
*/
Type of Operators
- Unary Operators : Operators that work on single operand.
- Binary Operators : Operators that work on two operands.
- Ternary Operators : Operators that work on three operands.
C operators can be classified into the following types:
- Arithmetic operators
- Relational operators
- Logical operators
- Bitwise operators
- Assignment operators
- Conditional operators
- Special operators
1. Arithmetic Operators in C
The C language supports all the basic arithmetic operators such as addition, subtraction, multiplication, division, etc.
The following table shows all the basic arithmetic operators along with their descriptions.
Operator | Description | Example(where a and b are variables with some integer value) |
---|---|---|
+ | adds two operands (values) | a+b |
- | subtract second operands from first | a-b |
* | multiply two operands | a*b |
/ | divide the numerator by the denominator, i.e. divide the operand on the left side with the operand on the right side | a/b |
% | This is the modulus operator, it returns the remainder of the division of two operands as the result | a%b |
++ | This is the Increment operator – increases the integer value by one. This operator needs only a single operand. | a++ or ++a |
-- | This is the Decrement operator – decreases integer value by one. This operator needs only a single operand. | --b or b-- |
+ | Unary Plus : Used to specify the positive values. +a | +a |
– | Unary Minus : Flips the sign of the value. -a | -a |
To learn in what order the arithmetic operators are executed, Please check the list of Operator Precedence and Associativity in C
Example of C Arithmetic Operators
// C program to illustrate the arithmatic operators
#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing results
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);
printf("+a = %d\n", +a);
printf("-a = %d\n", -a);
printf("a++ = %d\n", a++);
printf("a-- = %d\n", a--);
return 0;
}
Output :
a + b = 30
a - b = 20
a * b = 125
a / b = 5
a % b = 0
+a = 25
-a = -25
a++ = 25
a-- = 26
2. Relational Operators
The relational operators (or comparison operators) are used to check the relationship between two operands whether two operands are equal or not equal or less than or greater than, etc. It returns 1(true) if the relationship checks pass, otherwise, it returns 0(false).
For example, if we have two numbers 14 and 7, if we say 14 is greater than 7, this is true, hence this check will return 1 as the result with relationship operators. if we say 14 is less than 7, this is false, hence it will return 0.
These are total relational operators supported in the C language.
Operator | Description | Example(a and b , where a = 10 and b = 11) |
---|---|---|
== | Check if the two operands are equal | a == b , returns 0 |
!= | Check if the two operands are not equal. | a != b , returns 1 because a is not equal to b |
> | Check if the operand on the left is greater than the operand on the right | a > b , returns 0 |
< | Check operand on the left is smaller than the right operand | a < b , returns 1 |
>= | check left operand is greater than or equal to the right operand | a >= b , returns 0 |
<= | Check if the operand on the left is smaller than or equal to the right operand | a <= b , returns 1 |
To learn in what order the relational operators are executed, Please check the list of Operator Precedence and Associativity in C
Example of C Relational Operators
// C program to illustrate the relational operators
#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing results
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);
printf("a != b : %d\n", a != b);
return 0;
}
Output :
a < b : 0
a > b : 1
a <= b: 0
a >= b: 1
a == b: 0
a != b : 1
3. Logical Operator in C
Logical Operators are used to combine two or more conditions/constraints or to complement the evaluation of the original condition in consideration. The result of the operation of a logical operator is a Boolean value either true or false.
S. No. | Symbol | Operator | Description | Syntax |
---|---|---|---|---|
1 | && | Logical AND | Returns true if both the operands are true. | a && b |
2 | || | Logical OR | Returns true if both or any of the operand is true. | a || b |
3 | ! | Logical NOT | Returns true if the operand is false. | !a |
Example of Logical Operators in C :
// C program to illustrate the logical operators
#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing results
printf("a && b : %d\n", a && b);
printf("a || b : %d\n", a || b);
printf("!a: %d\n", !a);
return 0;
}
Output :
a && b : 1
a || b : 1
!a: 0
4. Bitwise Operators in C
The Bitwise operators are used to perform bit-level operations on the operands. The operators are first converted to bit-level and then the calculation is performed on the operands. These operators also perform the shifting of bits from right to left.
There are 6 bitwise operators in C programming.
S. No. | Symbol | Operator | Description | Syntax |
---|---|---|---|---|
1 | & | Bitwise AND | Performs bit-by-bit AND operation and returns the result. | a && b |
2 | | | Bitwise OR | Performs bit-by-bit OR operation and returns the result. | a || b |
3 | ^ | Bitwise XOR | Performs bit-by-bit XOR operation and returns the result. | a ^ b |
4 | ~ | Bitwise First Complement | Flips all the set and unset bits on the number. | ~a |
5 | << | Bitwise Leftshift | Shifts the number in binary form by one place in the operation and returns the result. | a << b |
6 | >> | Bitwise Rightshilft | Shifts the number in binary form by one place in the operation and returns the result. | a >> b |
Below truth table for showing how these operators work with different values.
a | b | a & b | a | b | a ^ b |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
Bitwise operators can produce any arbitrary value as a result. It is not mandatory that the result will either be 0 or 1.
Example of Bitwise Operators :
// C program to illustrate the bitwise operators
#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing results
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("a >> b: %d\n", a >> b);
printf("a << b: %d\n", a << b);
return 0;
}
Output :
a & b: 1 a | b: 29 a ^ b: 28 ~a: -26 a >> b: 0 a << b: 800
Bitwise >>
and <<
operators:
The bitwise shift operator shifts the bit value, either to the left or right. The left operand specifies the value to be shifted and the right operand specifies the number of positions that the bits in the value have to be shifted. Both operands have the same precedence.
Example :
a = 00010000
b = 2
a << b = 01000000
a >> b = 00000100
Here, a << b
, 2 bits are shifted to left in 00010000 and additional zeros are added to the opposite end, that is right, hence the value becomes 01000000a >> b
, 2 bits are shifted from the right, hence two zeros are removed from the right and two are added on the left, hence the value becomes 00000100
Example: Bitwise Left & Right shift Operators :
#include <stdio.h>
int main() {
int a = 0001000, b = 2, result;
// <<
result = a<<b;
printf("a << b = %d \n",result);
// >>
result = a>>b;
printf("a >> b = %d \n",result);
return 0;
}
Output :
a << b = 2048
a >> b = 128
Bitwise One’s Complement (~
) Operator :
The one’s complement operator will change all the 1’s in the operand to 0, and all the 0’s are set to 1.
For example, if the original byte is 00101100, then after one’s complement it will become 11010011.
5. Assignment Operators in C
Assignment operators are used to assign value to a variable. The left side operand of the assignment operator is a variable and the right side operand of the assignment operator is a value. The value on the right side must be of the same data type as the variable on the left side otherwise the compiler will raise an error.
For example, if we want to assign a value 10 to a variable x
then we can do this by using the assignment operator like: x = 10;
Here, =
(equal to) operator is used to assign the value. In the C language, the =
(equal to) operator is used for assignment however it has several other variants such as +=
, -=
to combine two operations in a single statement.
The total assignment operators in the table given below.
Operator | Description | Example(a and b are two variables, with where a=10 and b=5) |
---|---|---|
= | assigns values from right side operand to left side operand | a=b , a gets value 5 |
+= | adds right operand to the left operand and assign the result to left operand | a+=b , is same as a=a+b , value of a becomes 15 |
-= | subtracts right operand from the left operand and assign the result to left operand | a-=b , is same as a=a-b , value of a becomes 5 |
*= | mutiply left operand with the right operand and assign the result to left operand | a*=b , is same as a=a*b , value of a becomes 50 |
/= | divides left operand with the right operand and assign the result to left operand | a/=b , is same as a=a/b , value of a becomes 2 |
%= | calculate modulus using two operands and assign the result to left operand | a%=b , is same as a=a%b , value of a becomes 0 |
&= | AND and assign : Performs bitwise AND and assigns this value to the left operand. | a &= b |
|= | OR and assign : Performs bitwise OR and assigns this value to the left operand. | a |= b |
^= | XOR and assign : Performs bitwise XOR and assigns this value to the left operand. | a ^= b |
>>= | Rightshift and assign : Performs bitwise Rightshift and assign this value to the left operand. | a >>= b |
<<= | Leftshift and assign : Performs bitwise Leftshift and assign this value to the left operand. | a <<= b |
Example :
// C program to illustrate the arithmatic operators
#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing results
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);
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: 5
a += b: 10
a -= b: 5
a *= b: 25
a /= b: 5
a %= b: 0
a &= b: 0
a |= b: 5
)a >>= b: 0
a <<= b: 160
5. Conditional Operator or Ternary Operator (?
) in C
The ternary operator, also known as the conditional operator in the C language can be used for statements of the form if-then-else.
Syntax : (Expression1)? Expression2 : Expression3;
Here is how it works:
- The question mark
?
in the syntax represents the if part. - The first expression (expression 1) returns either true or false, based on which it is decided whether (expression 2) will be executed or (expression 3)
- If (expression 1) returns true then the (expression 2) is executed.
- If (expression 1) returns false then the expression on the right side of
:
i.e (expression 3) is executed.
Example of Ternary Operator
#include <stdio.h>
int main() {
int a = 20, b = 20, result;
/* Using ternary operator
- If a == b then store a+b in result
- otherwise store a-b in result
*/
result = (a==b)?(a+b):(a-b);
printf("result = %d",result);
return 0;
}
Output : result = 40
7. Special Operator
Apart from the above operators, there are some other operators available in C used to perform some specific tasks. Some of them are:
sizeof
operatorComma Operator ( , )
dot (.) and arrow (->) Operators
- Cast Operator
addressof (&) and Dereference (*) Operators
sizeof Operator
- sizeof is much used in the C programming language.
- It is a compile-time unary operator which can be used to compute the size of its operand.
- The result of sizeof is of the unsigned integral type which is usually denoted by size_t.
- Basically, the sizeof the operator is used to compute the size of the variable or datatype.
Syntax:sizeof(operand)
Comma Operator ( , )
- The comma operator (represented by the token) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type).
- The comma operator has the lowest precedence of any C operator.
- Comma acts as both operator and separator.
Syntax:operand1, operand2
dot (.) and arrow (->) Operators
- Member operators are used to reference individual members of classes, structures, and unions.
- The dot operator is applied to the actual object.
- The arrow operator is used with a pointer to an object.
Syntax:structure_variable . member;
andstructure_pointer -> member;
Cast Operator
- Casting operators convert one data type to another. For example, int(2.2000) would return 2.
- A cast is a special operator that forces one data type to be converted into another.
- The most general cast supported by most of the C compilers is as follows − [ (type) expression ].
Syntax:(new_type) operand;
addressof (&) and Dereference (*) Operators
- Pointer operator & returns the address of a variable. For example &a; will give the actual address of the variable.
- The pointer operator * is a pointer to a variable. For example *var; will pointer to a variable var.
Example of these C Operators :
// C Program to demonstrate the use of Misc operators
#include <stdio.h>
int main()
{
// integer variable
int num = 10;
int* add_of_num = #
printf("sizeof(num) = %d bytes\n", sizeof(num));
printf("&num = %p\n", &num);
printf("*add_of_num = %d\n", *add_of_num);
printf("(10 < 5) ? 10 : 20 = %d\n", (10 < 5) ? 10 : 20);
printf("(float)num = %f\n", (float)num);
return 0;
}
Output :
sizeof(num) = 4 bytes
&num = 0x7ffe2b7bdf8c
*add_of_num = 10
(10 < 5) ? 10 : 20 = 20
(float)num = 10.000000
Operator Precedence and Associativity in C
In C programming, it is very common for an expression or statement to have multiple operators and in these expression, there should be a fixed order or priority of operator evaluation to avoid ambiguity.Operator Precedence and Associativity
is the concept that decides which operator will be evaluated first in the case when there are multiple operators present in an expression.
For example, if we have three variables a
, b
and c
, then for the expression a+b*c
, the compiler will first multiply b
and c
, and then add the result of the multiplication with a
, because the *
operator has higher precedence than the +
operator.
The below table describes the precedence order and associativity of operators in C. The precedence of the operator decreases from top to bottom.
Precedence | Operator | Description | Associativity |
---|---|---|---|
1 | () | Parentheses (function call) | left-to-right |
[] | Brackets (array subscript) | left-to-right | |
. | Member selection via object name | left-to-right | |
-> | Member selection via a pointer | left-to-right | |
a++ , a– | Postfix increment/decrement (a is a variable) | left-to-right | |
2 | ++a , –a | Prefix increment/decrement (a is a variable) | right-to-left |
+ , – | Unary plus/minus | right-to-left | |
! , ~ | Logical negation/bitwise complement | right-to-left | |
(type) | Cast (convert value to temporary value of type) | right-to-left | |
* | Dereference | right-to-left | |
& | Address (of operand) | right-to-left | |
sizeof | Determine size in bytes on this implementation | right-to-left | |
3 | * , / , % | Multiplication/division/modulus | left-to-right |
4 | + , – | Addition/subtraction | left-to-right |
5 | << , >> | Bitwise shift left, Bitwise shift right | left-to-right |
6 | < , <= | Relational less than/less than or equal to | left-to-right |
> , >= | Relational greater than/greater than or equal to | left-to-right | |
7 | == , != | Relational is equal to/is not equal to | left-to-right |
8 | & | Bitwise AND | left-to-right |
9 | ^ | Bitwise exclusive OR | left-to-right |
10 | | | Bitwise inclusive OR | left-to-right |
11 | && | Logical AND | left-to-right |
12 | || | Logical OR | left-to-right |
13 | ?: | Ternary conditional | right-to-left |
14 | = | Assignment | right-to-left |
+= , -= | Addition/subtraction assignment | right-to-left | |
*= , /= | Multiplication/division assignment | right-to-left | |
%= , &= | Modulus/bitwise AND assignment | right-to-left | |
^= , |= | Bitwise exclusive/inclusive OR assignment | right-to-left | |
<<=, >>= | Bitwise shift left/right assignment | right-to-left | |
15 | , | expression separator | left-to-right |
Some basic rules around Operator Precedence
While the table above, holds every operator, but in general usage we mostly use arithmetic, logical and relational operators.
The arithmetic operators hold higher precedence than the logical and relational operators.
For example, if we have the following expression,
10 > 1 + 9;
This will return false because first the arithmetic operator +
will be evaluated and then the comparison will be done. The above expression is treated as 10 > (1+9)
.
Hence, because 10 is not greater than 10, but it is equal, so the expression will return false.
Example :
#include <stdio.h>
int main() {
// arithmetic operator precedence
int a = 10, b = 20, c = 30, result;
result = a * b + ++c;
printf("The result is: %d", result);
return 0;
}
Output :
The result is: 231
In the above code, first, ++c
is evaluated because the increment operator has the highest precedence (value becomes 31), then a*b
is evaluated because next in order of precedence is the multiplication operator (value becomes 200), then the +
operator is evaluated (200 + 31), hence the result is 231.
Conclusion
In this article, the points we learned about the operator are as follows:
- Operators are symbols used for performing some kind of operation in C.
- There are six types of operators, Arithmetic Operators, Relational Operators, Logical Operators, Bitwise Operators, Assignment Operators, and Miscellaneous Operators.
- Operators can also be of type unary, binary, and ternary according to the number of operators they are using.
- Every operator returns a numerical value except logical, relational, and conditional operator which returns a boolean value (true or false).
- There is a Precedence in the operators means the priority of using one operator is greater than another operator.
FAQs on C Operators
Q1. What are operators in C?
Answer:
Operators in C are certain symbols in C used for performing certain mathematical, relational, bitwise, conditional, or logical operations for the user.
Q2. What are the 7 types of operators in C?
Answer:
There are 7 types of operators in C as mentioned below:
- Unary operator
- Arithmetic operator
- Relational operator
- Logical operator
- Bitwise operator
- Assignment operator
- Conditional operator
Q3. What is the difference between the ‘=’ and ‘==’ operators?
Answer:
‘=’ is a type of assignment operator that places the value in right to the variable on left, Whereas ‘==’ is a type of relational operator that is used to compare two elements if the elements are equal or not.
Q4. What is the difference between prefix and postfix operators in C?
Answer:
Prefix operations are the operations in which the value is returned prior to the operation whereas in postfix operations value is returned after updating the value in the variable.
Example:
b=c=10;
a=b++; // a==10
a=++c; // a==11
Q5. What is the Modulo operator?
Answer:
The Modulo operator(%) is used to find the remainder if one element is divided by another.
Example:
a % b (a divided by b)
5 % 2 == 1
Q6. What does *
operator do in C?
Answer:
The * operator in the C language is a unary operator that returns the value of the object located at the address, specified after the *
operator. For example q = *m
will store the value stored at the memory address m
in the q
variable, if m
contains a memory address.
The *
operator is also used to perform the multiplication of two values, where it acts as an arithmetic operator.
Q7. What does !=
mean in C?
Answer:
It is a symbol of not equal to(!=
) operator and used to check whether two values are not equal to each other or not. It is a relational operator and its opposite operator is an equal(==
) operator which is used to check equality between two values or variables.
If two values are not equal, then we will get 1 as the result of the comparison.
Q8. What is &
and *
operators in C?
Answer:
Both are special types of operators and are used to perform memory-related operations. The &
operator is used to get the address of a variable and the *
operator is the complement of the &
operator and is used to get the value of the object for located at a memory address.
Q9. What does %d do in C?
Answer:
It is a format specifier that is used to print formatted output to the console. In the C language, it is used with the printf()
function(C Input Output) to display integer value to the console. To print float
, C provides %f
, for char
we use %c
, for double
we use %lf
, etc.