Operators in C: Symbols which are used to perform both logical and mathematical operations in a C program are called Operators in C.
C language offers a different type of operators. They are as follows:
- Arithmetic Operators
- Assignment Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Conditional Operators (ternary operators)
- Increment/decrement operators
- Special Operators
Operator Name | Symbol | Example |
---|---|---|
Addition | + | A+B=20 |
Subtraction | – | A – B=10 |
Multiplication | * | A* B=200 |
Division | / | B/A=2 |
Modulus | % | B%A=0 |
Example: C program to add two numbers.
#include <stdio.h> void main() { int i=3, j=7, k; /* Variables Defining and Assign values */ k=i+j; printf("sum of two numbers is %d\n", k); }
Output: sum of two numbers is 10
C Assignment Operators
These operators in C are used to assign the values for variables in the C program.
Operator Name | Description | Example |
---|---|---|
= | Simple assignment operator, which assigns value form the right-side operands to left side operands | C = A + B which will assign a value of A + B to C |
+= | And assignment adds the right operand to the left operand and assign the result to left | C += A is equivalent to C = C + A |
– = | Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. | C -= A is equivalent to C = C – A |
*= | Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand. | C *= A is equivalent to C = C * A |
/= | Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand | C /= A is equivalent to C = C / A |
%= | Modulus AND assignment operator. And It takes modulus using two operands and assigns the result to the left operand | C %= A is equivalent to C = C % A |
<<= | Left shift AND assignment operator | C <<= 2 is same as C = C << 2 |
>>= | Right shift AND assignment operator. | C >>= 2 is same as C = C >> 2 |
&= | Bitwise AND assignment operator | C &= 2 is same as C = C & 2 |
^= | Bitwise exclusive OR and assignment operator. | C ^= 2 is same as C = C ^ 2 |
|= | Bitwise inclusive OR and assignment operator | C |= 2 is same as C = C | 2 |
Example
# include <stdio.h> int main() { int Total=0, i; for(i=0;i<10;i++) { Total+=i; // This is same as Total = Total+i } printf("Total = %d", Total);
Output: 45
Relational Operators in C
These operators in C are used to compare the value of two variables.
Operator | Description | Example |
---|---|---|
= = | Checks if the values of two operands are equal or not. If yes, then the condition becomes true. | (A == B) is not true |
!= | Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true | (A!= B) is true |
> | Checks if the value of the left operand is greater than the value of the right operand. If yes, then the condition becomes true. | (A > B) is not true |
< | Checks if the value of the left operand is less than the value of the right operand. If yes, then the condition becomes true | (A < B) is true |
>= | Checks if the value of the left operand is greater than or equal to the value of the right operand. If yes, then the condition becomes true. | (A >= B) is not true |
< = | Checks if the value of the left operand is less than or equal to the value of the right operand. If yes, then the condition becomes true. | (A <= B) is true |
Example
#include <stdio.h> int main() { int m=40,n=20; if (m == n) { printf("m and n are equal"); } else { printf("m and n are not equal"); }
Output: m and n are not equal
Logical Operators in C
Used to perform logical operations in C on the given two variables.
Operator | Description | Example |
---|---|---|
&& | Called Logical AND operator. If both the operands are non-zero, then the condition becomes true | (A && B) is false. |
|| | Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true | (A || B) is true |
! | Called Logical NOT Operator. And it is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. | !(A && B) is true |
Example
#include <stdio.h> int main() { int m=40,n=20; int o=20,p=30; if (m>n && m!=0) { printf("&& Operator : Both conditions are true\n"); } if(o>p || p!=20) { printf("|| Operator : Only one condition is true\n"); } if(!(m>n && m !=0)) { printf("! Operator : Both conditions are true\n"); } else { printf("! Operator : Both conditions are true." \"But, status is inverted as false\n"); } }
Output
&& Operator: Both conditions are true
|| Operator: Only one condition is true!
Operator: Both conditions are true. But, status is inverted as false
Bit-Wise Operators in C
Used to perform the bit operations in C on given two variables. Assume A =60 and B=13 in binary format.
A = 0011 1100
B =0000 1101
———————–
A & B = 0000 1100
A | B = 0011 1101
A ^ B = 0011 0001
~A = 1100 0011
P | q | p & q | p | q | p^q |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
Operator | Description | Example |
---|---|---|
& | Binary AND Operator copies a bit to the result if it exists in both operands. | (A & B) = 12, i.e., 0000 1100 |
| | Binary OR Operator copies a bit if it exists in either operand. | (A | B) = 61, i.e., 0011 1101 |
^ | Binary XOR Operator copies the bit if it is set in one operand but not both. | (A ^ B) = 49, i.e., 0011 0001 |
~ | Binary One’s Complement Operator is unary and has the effect of ‘flipping’ bits. | (~A) = ~(60), i.e. -0111101 |
<< | Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. | A << 2 = 240 i.e., 1111 0000 |
>> | Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. | A >> 2 = 15 i.e., 0000 1111 |
Example
#include <stdio.h> int main() { int m = 40,n = 80, AND_opr,OR_opr,XOR_opr,NOT_opr ; AND_opr = (m&n); OR_opr = (m|n); NOT_opr = (~m); XOR_opr = (m^n); printf("AND_opr value = %d\n",AND_opr ); printf("OR_opr value = %d\n",OR_opr ); printf("NOT_opr value = %d\n",NOT_opr ); printf("XOR_opr value = %d\n",XOR_opr ); printf("left_shift value = %d\n", m << 1); printf("right_shift value = %d\n", m >> 1); }
Output
AND_opr value = 0
OR_opr value = 120
NOT_opr value = -41
XOR_opr value = 120
left_shift value = 80
right_shift value = 20
Conditional Operators in C
returns one value if the condition is true, and produces another value if the condition is false. It is also known as a “ternary operator”.
Operator | Description |
---|---|
? | Conditional expression |
Example
#include <stdio.h> int main() { int x=1, y; y = (x ==1 ? 2: 0); printf("x value is %d\n", x); printf("y value is %d", y); }
Output
x value is 1
Y value is 2
Increment-decrement Operators in C
Increment operators are used for increasing the value of a variable by one. And decrement operators are used for decreasing the value by one.
Syntax
Increment operator: ++var_name; (or) var_name++;
Decrement operator: var_name; (or) var_name ;
Example
#include <stdio.h> int main( ) { int i=1; while(i<10) { printf("%d ",i); i++; } }
Output: 1 2 3 4 5 6 7 8 9
Example 2
#include <stdio.h> int main( ) { int i=10; while(i-- > 5) { printf("%d ",i); } return 0; }
Output: 9 8 7 6 5
Special Operators in C
Operator | Description |
---|---|
& | t is used to get the address of the variable |
* | Used as a pointer to the variable |
sizeof( ) | Gives size of the variable |
Example
#include <stdio.h> #include <limits.h> int main() { int a; char b; float c; double d; printf("Storage size for int data type:%d \n",sizeof(a)); printf("Storage size for char data type:%d \n",sizeof(b)); printf("Storage size for float data type:%d \n",sizeof(c)) return 0; }
Output
Storage size for int data Storage size for char data type:1
Storage size for float data type:4
Example 2
#include <stdio.h> int main( ) { int *ptr, q; q = 50; /* address of q is assigned to ptr */ ptr = &q;/* display q's value using ptr variable */ printf("%d", *ptr); return 0; }
Output: 50