There are several types of operators in C. These operators are as follows:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Miscellaneous Operators
- Shift Operators
Now, we will discuss these operator one by one in C.
Arithmetic Operators
In C programming language , arithmetic operators carry out fundamental mathematical operations, such as addition (+), subtraction (-), multiplication (*), division (/), and modulus (%) on the operands. Several arithmetic operators in C are as follows:
| Operator Name | Symbol | Description |
|---|---|---|
| Addition | + | The addition operator is used toaddtwo operands together. |
| Subtraction | - | The subtraction operator is used to subtract the second operand from the first operand. |
| Multiplication | * | This operator is used tomultiplythe two operands. |
| Division | / | The first operand and the second operand aredividedusing the division operator. |
| Modulus | % | Themodulus operatordetermines the remainder of the division between two operands. |
| Increment | ++ | It increments value by one x++ or ++x. |
| Decrement | -- | It decrements value by one x-- or --x |
The modulo operator (%) should only be applied to integer values because it returns the remainder of an integer division. It is not appropriate for floating-point numbers. The increment (++) and decrement (--) operators are available in two forms: prefix and postfix. Both increase or reduce the value by one, but their execution differs:
- ++a: Prefix increment; the value is increased before being used in the expression.
- a++: Postfix increment; first, the original value is used, followed by the increment.
The same logic applies to the decrement operator.
- --a: It reduces the value before using it.
- a--: It takes the current value and then decreases it.
C Arithmetic Operator Example
Let us take an example to illustrate the Arithmetic operators in C.
Example
#include <stdio.h>
int main() //Main function
{
int x = 10, y = 20;
printf("The addition of given numbers x + y is = %d\n", x + y); // Addition of two numbers
printf("The subtraction of given numbers x - y is = %d\n", x - y); // Subtraction of two numbers
printf("The multiplication of given numbers x * y is = %d\n", x * y); // Multiplication of two numbers
printf("The division of given numbers y / x is = %d\n", y / x); // Division of numbers
printf("The modulus of given numbers y %% x is = %d\n", y % x); // Modulus of numbers
printf("The given number is x++ = %d\n", x++); // Increment of a number
printf("After increment, the value of x is = %d\n", x);
printf("Now, the value of x-- is = %d\n", x--); // Decrement of a number
printf("After decrement, the value of x is = %d\n", x);
return 0;
}
Output:
The addition of given numbers x + y is = 30
The subtraction of given numbers x - y is = -10
The multiplication of given numbers x * y is = 200
The division of given numbers y / x is = 2
The modulus of given numbers y % x is = 0
The given number is x++ = 10
After increment, the value of x is = 11
Now, the value of x-- is = 11
After decrement, the value of x is = 10
Explanation:
In this example, we demonstrate how to use arithmetic operators such as addition, subtraction, multiplication, division, modulus, increment, and decrement with two numbers, x and y. It shows the results of each operation step by step, which demonstrates how values change, particularly when incrementing and decrementing x.
Relational Operators
In C, relational operators are mainly used to compare two values or expressions and establish their relationship. These comparisons return a boolean value true or false (0 and 1). If the comparison is true, it returns 1 value. On the other hand, if the comparison is false, it returns 0 value. There are several operators in relational operators. Here is the following table that helps to understand the working of these operators in C:
| Operator Name | Symbol | Description | Example |
|---|---|---|---|
| Equal to | == | If two operands are equal, theequality operatorverifies this. | x==y |
| Not Equal to | != | The inequality operator determines whether two operands are equal or not. | x!=y |
| Greater than | > | The greater than operator determines if the first operand exceeds the second operand. | x>y |
| Less than | < | Theless-than operatordetermines if the first operand is less than the second operand. | x<y |
| Greater than or equal to | >= | Thegreater than or equal tothe operator determines if the first operand is more than or equal to the second operand. | x>=y |
| Less than or equal to | <= | Theless than or equal tothe operator determines if the first operand must be less than or equal to the second operand. | x<=y |
C Relational Operator Example
Let us take an example to illustrate the Relational operators in C.
Example
#include <stdio.h>
int main() //main function
{
int x = 10;
int y = 20;
printf("The value of x is = %d\nThe value of y is = %d\n", x, y);
printf("The equality of two numbers (x == y) is: %d\n", x == y); // Checking Equal to
printf("The not equality of two numbers (x != y) is: %d\n", x != y); // Checking Not equal to
printf("The Greater than of numbers (x > y) is: %d\n", x > y); // Verifying Greater than
printf("The Less than of numbers (x < y) is: %d\n", x < y); //Verifying Less than
printf("The Greater than or equal to of numbers (x >= y) is: %d\n", x >= y); // Checking Greater than or equal to
printf("The Less than or equal to of numbers (x <= y) is: %d\n", x <= y); // Checking Less than or equal to
return 0;
}
Output:
The value of x is = 10
The value of y is = 20
The equality of two numbers (x == y) is: 0
The not equality of two numbers (x != y) is: 1
The Greater than of numbers (x > y) is: 0
The Less than of numbers (x < y) is: 1
The Greater than or equal to of numbers (x >= y) is: 0
The Less than or equal to of numbers (x <= y) is: 1
Explanation:
In this example, we demonstrate how each relational operator analyses a condition and returns either 1 (true) or 0 (false), which is important for making decisions and controlling program flow.
Logical Operators
In C, logical operators are mainly used to perform logical operations on boolean values (true and false) and return either true (1) or false (0). These operators are essential for controlling the program flow based on conditions. If the comparison is true, it returns 1 value. If the comparison is false, it returns 0 value. Here is the following table that helps to understand the working of these operators in C.
| Operator Name | Symbol | Description | ||
|---|---|---|---|---|
| Logical AND | && | Called Logical AND operator. Thelogical AND operatorreturnstrueif both operands aretrue. | ||
| (Logical OR) | Called Logical OR Operator. Thelogical OR operatorreturnstrueif at least one of the operands is true. | |||
| (Logical NOT) | ! | Called Logical NOT Operator. Thelogical NOT operatornegates the value of the operand. |
C Logical Operators Example
Let us take an example to illustrate the Logical operators in C.
Example
#include <stdio.h>
int main() //main function
{
int a = 5, b = 10, c = 0;
// Logical AND - "true" if both conditions are true.
if (a > 0 && b > 5)
{
printf("Logical AND: Both a > 0 and b > 5 are true.\n");
}
else
{
printf("Logical AND: Condition is false.\n");
}
// Logical OR - "true" if at least one condition is true.
if (a < 0 || c == 0)
{
printf("Logical OR: Either a < 0 or c == 0 is true.\n");
}
else
{
printf("Logical OR: Condition is false.\n");
}
// Logical NOT - reverses the given condition.
if (!(b < 5))
{
printf("Logical NOT: b < 5 is false, so !(b < 5) is true.\n");
}
else
{
printf("Logical NOT: Condition is false.\n");
}
return 0;
}
Output:
Logical AND: Both a > 0 and b > 5 are true.
Logical OR: Either a < 0 or c == 0 is true.
Logical NOT: b < 5 is false, so !(b < 5) is true.
Explanation:
In this example, we evaluate several conditions with variables a, b, and c. After that, it displays results according on whether both, either, or none of the conditions are true, showing how &&, ||, and! behave logically.
Bitwise Operators
In C, bitwise operators are used to perform operations at the bit level on integer data types. These operations directly manipulate the individual bits of data, which makes them useful in low-level programming, graphics, encryption, and system programming. Here is the following table that helps to understand the working of these operators in C:
| Operator Name | Symbol | Description | |
|---|---|---|---|
| bitwise AND | a&b | Thebitwise AND operatorperforms abitwise AND operationon the corresponding bits of the operands. | |
| bitwise OR | a | b | Thebitwise OR operatorperforms a bitwise OR operation on the corresponding bits of the operands. |
| bitwise XOR | a^b | Thebitwise XOR operatorperforms a bitwise exclusive OR operation on the corresponding bits of the operands. | |
| bitwise NOT | ~a | Thebitwise NOT operatorflips each bit of the operand. | |
| Binary left shift | a<<2 | Shifts bits from the left operand to the left by the number of positions provided by the right operand. Empty bits are filled with zeros. | |
| Binary right shift | a>>2 | Shifts the left operand's bits to the right by the number of positions given by the right operand. The sign bit (in the case of signed types) or zero is used to fill empty bits. |
C Bitwise Operator Example
Let us take an example to illustrate the Bitwise operators in C.
Example
#include <stdio.h>
int main() //main function
{
int a = 45; // 0010 1101
int b = 25; // 0001 1001
printf("The value of bitwise AND - a&b is = %d\n", a & b);
printf("The value of bitwise OR - a|b is = %d\n", a | b);
printf("The value of bitwise XOR - a^b is = %d\n", a ^ b);
printf("The value of bitwise NOT - ~a is = %d\n", ~a);
printf("The value of binary left shift - a<<2 is = %d\n", a<<2);
printf("The value of binary right shift - a>>2 is = %d\n", a>>2);
return 0;
}
Output:
The value of bitwise AND - a&b is = 9
The value of bitwise OR - a|b is = 61
The value of bitwise XOR - a^b is = 52
The value of bitwise NOT - ~a is = -46
The value of binary left shift - a<<2 is = 180
The value of binary right shift - a>>2 is = 11
Explanation:
In this example, we demonstrate how to use bitwise operators on two integers (a and b). It performs bitwise AND, OR, XOR, NOT, binary left shift, and binary right shift operations and prints the results depending on the binary representation of the values.
Assignment Operators
In C, assignment operators are used to assign and update variable values. The basic assignment operator (=) stores the value of the right-hand operand in the left-hand variable, which makes it essential to variable initialization and updating. Here is the following table that helps to understand the working of these operators in C:
| Operator Name | Symbol | Description | |
|---|---|---|---|
| Assignment | = | It assigns the right operand to the left operand. | |
| Addition Assignment | += | It adds the right operand to the left and assigns the result there. | |
| Subtraction Assignment | -= | It subtracts right from left and assigns the result to the left. | |
| Multiplication Assignment | *= | Multiply left by right and allocate the result to the left. | |
| Division Assignment | /= | It divides left by right and assigns the result to the left. | |
| Modulus Assignment | %= | It computes the remainder and assigns it to the left. | |
| Bitwise left Shift Assignment | <<= | It shifts left and assigns result to the left. | |
| Bitwise Right Shift Assignment | >>= | It shifts right and assigns the result to the left. | |
| Bitwise AND Assignment | &= | It executes bitwise AND and assigns the result to the left operand. | |
| Bitwise OR Assignment | = | It executes bitwise OR and assigns the result to the left operand. | |
| Bitwise XOR Assignment | ^= | It executes bitwise XOR and assigns the result to the left operand. |
C Assignment Operator Example
Let us take an example to illustrate the Assignment operators in C.
Example
#include <stdio.h>
int main(void) //main function
{
int c;
int a = 5;
c = a;
printf("The value of assignment operator c = a is c = %d\n", c);
c = 10; //using assignment operator
c += a; //using addition assignment
printf("The value of a addition assignment operator c += a is c = %d\n", c); c = 10;
c -= a; //using subtraction assignment
printf("The value of a subtraction assignment operator c -= a is c = %d\n", c); c = 10;
c *= a; //using multiplication assignment
printf("The value of a multiplication assignment operator c *= a is c = %d\n", c);
c = 10;
c /= a; //using division assignment
printf("The value of a division assignment operator c /= a is c = %d\n", c);
c = 10;
c %= a; //using modulus assignment
printf("The value of a modulus assignment operator c %%= a is c = %d\n", c);
c = 10;
c <<= 2; //using bitwise left shift assignment
printf("The value of a bitwise left shift operator c <<= 2 is c = %d\n", c);
c = 10;
c >>= 2; //using bitwise right shift assignment
printf("The value of a bitwise right shift operator c >>= 2 is c = %d\n", c);
c = 10;
c &= a; //using bitwise AND assignment
printf("The value of a Bitwise AND Assignment operator c &= a is c = %d\n", c);
c = 10;
c |= a; //using bitwise OR assignment
printf("The value of a Bitwise OR Assignment c |= a is c = %d\n", c);
c = 10;
c ^= a; //using bitwise XOR assignment
printf("The value of a Bitwise XOR Assignment c ^= a is c = %d\n", c);
return 0;
}
Output:
The value of assignment operator c = a is c = 5
The value of a addition assignment operator c += a is c = 15
The value of a subtraction assignment operator c -= a is c = 5
The value of a multiplication assignment operator c *= a is c = 50
The value of a division assignment operator c /= a is c = 2
The value of a modulus assignment operator c %= a is c = 0
The value of a bitwise left shift operator c <<= 2 is c = 40
The value of a bitwise right shift operator c >>= 2 is c = 2
The value of a Bitwise AND Assignment operator c &= a is c = 0
The value of a Bitwise OR Assignment c |= a is c = 15
The value of a Bitwise XOR Assignment c ^= a is c = 15
Explanation:
In this example, we demonstrate how to use several assignment operators with two integers, a = 5 and c. It performs simple arithmetic, bitwise, and shift assignment operations on c with respect to a, which prints the results of each operation to demonstrate how c is updated.
Miscellaneous Operators
In C, miscellaneous operators refer to operators that do not fall under the arithmetic, relational, or logical category. They perform special operations, including memory access, type conversion, and conditional evaluation. Several miscellaneous operators in C as mentioned below:
| Operator Name | Symbol | Description |
|---|---|---|
| sizeof Operator | sizeof | It returns the variable or data type's size (in bytes). |
| Conditional Operator (? :) | ? : | Based on a criteria, one of two values is returned. |
| Comma Operator (,) | , | The comma operator analyses multiple expressions and returns the last one. |
| dot (.) and arrow (->) Operators | . and -> | Access structure members using. for direct objects and -> for objecLogic Practiceers. |
| cast Operators | Type cast | It converts a data type to another explicitly. |
| addressof (&) Operators | & | Address-of operator. Returns the memory address of a variable. |
| Dereference (*) Operators | * | Pointer dereference operator. Accesses the value at the address specified by a pointer. |
C Miscellaneous Operator Example
Let us take an example to illustrate the Miscellaneous operators in C.
Example
#include <stdio.h>
struct Employee
{
int age;
};
int main() //main function
{
int a = 10, b = 20, result;
int *p = &a;
// sizeof operator
printf("Size of int is: %lu\n", sizeof(int));
// Ternary operator
result = (a > b) ? a : b;
printf("Greater value using ? : is: %d\n", result);
// Comma operator
result = (a = 5, b = 15, a + b);
printf("Result using comma operator is: %d\n", result);
// Structure access using . and -> operators
struct Employee emp = {25};
struct Employee *ptr = &emp;
printf("Employee age using . is : %d\n", emp.age);
printf("Employee age using -> is : %d\n", ptr->age);
// Type casting operator
float x = 5.9;
int y = (int)x;
printf("After casting float to int: %d\n", y);
// Address-of operator
printf("The address of a is: %p\n", (void*)&a);
// Pointer dereference operator
printf("Value pointed by p is: %d\n", *p);
return 0;
}
Output:
Size of int is: 4
Greater value using ? : is: 20
Result using comma operator is: 20
Employee age using . is : 25
Employee age using -> is : 25
After casting float to int: 5
The address of a is: 0x7ffdea010984
Value pointed by p is: 5
Explanation:
In this example, we demonstrate how to use sizeof, ternary ?:, comma,, structure access (. and ->), type casting, address-of &, and pointer dereferencing * with integers, floats, and structures.
Shift Operators
A binary number's bits can be moved to the left or right using shift operators . In C, two shift operators are supported:
| Operator Name | Symbol | Description |
|---|---|---|
| Left shift operator | << | Theleft shift operatormoves the bits of the first operand to the left by the number of places indicated by the second argument. |
| Right shift operator | >> | Theright shift operatorshifts the bits of the first operand to therightby the number of positions specified by the second operand. |
C Shift Operator Example
Let us take an example to illustrate the Shift operator in C.
Example
#include <stdio.h>
int main() //main function
{
unsigned int a = 8;
unsigned int b = 10;
unsigned int l = a << 2;
printf("The value of left shift operator is: %u << 2 = %u\n", a, l);
unsigned int r = b >> 2;
printf("The value of right shift operator is: %u >> 2 = %u\n", b, r);
return 0;
}
Output:
The value of left shift operator is: 8 << 2 = 32
The value of right shift operator is: 10 >> 2 = 2
Explanation:
In this example, we demonstrate the left (<<) and right (>>) bitwise shift operators. It shifts the bits of two unsigned integers and prints the results.
Precedence of Operators in C
The precedence of operator species that which operator will be evaluated first and next. The associativity specifies the operator direction to be evaluated; it may be left to right or right to left.
Let's understand the precedence by the example given below:
int value=10+20*10;
The value variable will contain 210 because * (multiplicative operator) is evaluated before + (additive operator).
The precedence and associativity of C operators is given below:
| Category | Symbols | Associativity | ||
|---|---|---|---|---|
| Postfix | () [] ->. ++ - - | Left to right | ||
| Unary | + - ! ~ ++ - - (type)* & sizeof | Right to left | ||
| Multiplicative | * / % | Left to right | ||
| Additive | + - | Left to right | ||
| Shift | << >> | Left to right | ||
| Relational | < <= > >= | Left to right | ||
| Equality | == != | Left to right | ||
| Bitwise AND | & | Left to right | ||
| Bitwise XOR | ^ | Left to right | ||
| Bitwise OR | Left to right | |||
| Logical AND | && | Left to right | ||
| Logical OR | Left to right | |||
| Conditional | ?: | Right to left | ||
| Assignment | = += -= *= /= %=>>= <<= &= ^= | = | Right to left | |
| Comma | , | Left to right |
Conclusion
In this article, we have covered several types of C operators, such as arithmetic, relational, shift, logical, bitwise, ternary, assignment, and other operators. We also covered their features, syntax, samples, and what results to expect. By becoming proficient with these operators, we may effectively handle data and write reliable C programs .