Operators are the basic components of C programming. They are symbols that represent some kind of operation, such as mathematical, relational, bitwise, conditional, or logical computations, which are to be performed on values or variables. The values and variables used with operators are called operands.
C
#include <stdio.h>
int main() {
// Expression for getting sum
int sum = 10 + 20;
printf("%d", sum);
return 0;
}
Unary, Binary and Ternary Operators
On the basis of the number of operands they work on, operators can be classified into three types :
- Unary Operators: Operators that work on single operand.
Example: Increment( ++) , Decrement(--) - Binary Operators: Operators that work on two operands.
Example: Addition (+), Subtraction( -) , Multiplication (*) - Ternary Operators: Operators that work on three operands.
Example: Conditional Operator( ? : )
Types of Operators in C
C language provides a wide range of built in operators that can be classified into 6 types based on their functionality:
Arithmetic Operators
The arithmetic operators are used to perform arithmetic/mathematical operations on operands. There are 9 arithmetic operators in C language:
C
#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;
}
Outputa + b = 30
a - b = 20
a * b = 125
a / b = 5
a % b = 0
+a = 25
-a = -25
a++ = 25
a-- = 26
Relational Operators
The relational operators in C are used for the comparison of the two operands. All these operators are binary operators that return true or false values as the result of comparison.
C
#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;
}
Outputa < b : 0
a > b : 1
a <= b: 0
a >= b: 1
a == b: 0
a != b : 1
Here, 0 means false and 1 means true.
Logical Operator
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.
Symbol | Operator | Description | Syntax |
---|
&& | Logical AND | Returns true if both the operands are true. | a && b |
|| | Logical OR | Returns true if both or any o f the operand is true. | a || b |
! | Logical NOT | Returns true if the operand is false. | !a |
C
#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;
}
Outputa && b : 1
a || b : 1
!a: 0
Bitwise Operators
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.
Note: Mathematical operations such as addition, subtraction, multiplication, etc. can be performed at the bit level for faster processing.
Symbol | Operator | Description | Syntax |
---|
& | Bitwise AND | Performs bit-by-bit AND operation and returns the result. | a & b |
| | Bitwise OR | Performs bit-by-bit OR operation and returns the result. | a | b |
^ | Bitwise XOR | Performs bit-by-bit XOR operation and returns the result. | a ^ b |
~ | Bitwise First Complement | Flips all the set and unset bits on the number. | ~a |
<< | Bitwise Leftshift | Shifts bits to the left by a given number of positions; multiplies the number by 2 for each shift. | a << b |
>> | Bitwise Rightshilft | Shifts bits to the right by a given number of positions; divides the number by 2 for each shift. | a >> b |
C
#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;
}
Outputa & b: 1
a | b: 29
a ^ b: 28
~a: -26
a >> b: 0
a << b: 800
Assignment Operators
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.
The assignment operators can be combined with some other operators in C to provide multiple operations using single operator. These operators are called compound operators.
Symbol | Operator | Description | Syntax |
---|
= | Simple Assignment | Assign the value of the right operand to the left operand. | a = b |
+= | Plus and assign | Add the right operand and left operand and assign this value to the left operand. | a += b |
-= | Minus and assign | Subtract the right operand and left operand and assign this value to the left operand. | a -= b |
*= | Multiply and assign | Multiply the right operand and left operand and assign this value to the left operand. | a *= b |
/= | Divide and assign | Divide the left operand with the right operand and assign this value to the left operand. | a /= b |
%= | Modulus and assign | Assign the remainder in the division of left operand with the right operand to the left operand. | a %= b |
&= | 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 |
C
#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);
printf("a <<= b: %d\n", a <<= b);
return 0;
}
Outputa = 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: 0
a <<= b: 0
Other Operators
Apart from the above operators, there are some other operators available in C used to perform some specific tasks. Some of them are discussed here:
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
C
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. It can act as both operator and separator.
Syntax
C
Conditional Operator ( ? : )
The conditional operator is the only ternary operator in C++. It is a conditional operator that we can use in place of if..else statements.
Syntax
C
expression1 ? Expression2 : Expression3;
Here, Expression1 is the condition to be evaluated. If the condition(Expression1) is True then we will execute and return the result of Expression2 otherwise if the condition(Expression1) is false then we will execute and return the result of Expression3.
dot (.) and arrow (->) Operators
Member operators are used to reference individual members of classes, structures, and unions.
Syntax
C
structure_variable . member;
structure_pointer -> member;
Cast Operators
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.
Syntax
C
addressof (&) and Dereference (*) Operators
Addressof operator & returns the address of a variable and the dereference operator * is a pointer to a variable. For example *var; will pointer to a variable var.
Example of Other C Operators
C
// 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;
}
Outputsizeof(num) = 4 bytes
&num = 0x7ffdb58c037c
*add_of_num = 10
(10 < 5) ? 10 : 20 = 20
(float)num = 10.000000
It is strongly recommended to learn Operator Precedence and Associativity after reading about operators.
Explore
C Basics
Arrays & Strings
Pointers and Structures
Memory Management
File & Error Handling
Advanced Concepts