Operators are the mathematical symbols that are used to perform a mathematical operation on operands. These symbols tell the compiler to perform respective operations. An expression is formed by joining constants and variables in C programming.
Example:
1 | int a = 10 + 5; |
In the above example, 10 and 5 are operands and + is the operation performed between these two operands.
Types of Operator in C:
- Arithmetic Operator
- Increment and Decrement Operators
- Relational Operator
- Assignment Operators
- Logical Operators
- Bitwise Operators
- sizeof Operator
- Other Operators
C Arithmetic Operator
These are used to perform mathematical operations on operands such as addition, subtraction, multiplication, division etc.
- Addition: ‘+’ operator adds two operands. eg, a+b.
- Subtraction: ‘-‘ operator subtracts two operands. eg, a-b.
- Multiplication: ‘*’ operator multiplies two operands. eg, a*b.
- Division: ’/’ operator divides the operand by the second. eg, a/b.
- Modulus: ’%’ operator returns the remainder when the first operand is divided by the second. For example, a%b.
Example of Arithmetic Operator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <stdio.h> int main() { int a = 24; int b = 12; int result; result = a + b; printf("Addition: %d\n", result); result = a - b; printf("Subtraction: %d\n", result); result = a * b; printf("Multiplication: %d\n", result); result = a / b; printf("Division: %d\n", result); result = a % b; printf("Modulus %d\n", result); return 0; } |
The output of Arithmetic Operator:
1 2 3 4 5 | Addition: 36 Subtraction: 12 Multiplication: 288 Division: 2 Modulus 0 |
C Increment and Decrement Operators
These are the Unary Operators as they operate on a single operand. They are ++
and --
operators. ++ is used to increase the value by 1 and — is used to decrease the value by 1. There are two kinds of Increment and Decrement Operators.
- Post-Increment or Post-Decrement:
First, the value is used for operation and then incremented or decremented. Represented as a++ or a–. - Pre-Increment Pre-Decrement:
Here First the value is incremented or decremented then used for the operation. Represented as ++a or –a.
Example of Increment and Decrement Operator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <stdio.h> void main() { int x = 5; //post increment printf("%d\n", x++); printf("%d\n\n", x); x = 10; //preincrement printf("%d\n", ++x); printf("%d\n", x); } |
The output of Increment and Decrement Operator.
1 2 3 4 5 | 5 6 11 11 |
C Relational Operator
Relational operators are used to compare two values and return boolean results. These operators are used to check for relations like equality, greater than, less than between two values. They are used with a loop statement and also with if-else statements.
List of relational operators in C:
Operator | Name | Example |
---|---|---|
== | Equal to. | A == B |
!= | Not equal. | A != B |
> | Greater than. | A > B |
< | Less than. | A < B |
>= | Greater than or equal to. | A >= B |
<= | Less than or equal to. | A <= B |
Example of Relational Operator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <stdio.h> int main() { int a = 10, b = 10, c = 20; printf("%d == %d is %d \n", a, b, a == b); printf("%d == %d is %d \n", a, c, a == c); printf("%d > %d is %d \n", a, b, a> b); printf("%d > %d is %d \n", a, c, a> c); printf("%d<%d is %d \n", a, b, a < b); printf("%d<%d is %d \n", a, c, a < c); printf("%d != %d is %d \n", a, b, a != b); printf("%d != %d is %d \n", a, c, a != c); printf("%d >= %d is %d \n", a, b, a>= b); printf("%d >= %d is %d \n", a, c, a>= c); printf("%d <= %d is %d \n", a, b, a <= b); printf("%d <= %d is %d \n", a, c, a <= c); return 0; } |
The output of Relational Operator.
1 2 3 4 5 6 7 8 9 10 11 12 | 10 == 10 is 1 10 == 20 is 0 10 > 10 is 0 10 > 20 is 0 10 < 10 is 0 10 < 20 is 1 10 != 10 is 0 10 != 20 is 1 10 >= 10 is 1 10 >= 20 is 0 10 <= 10 is 1 10 <= 20 is 1 |
C Assignment Operators
Assignment operator is used to assigning a value to any variable. This operator assigns the value of the right-hand side of an operator to the left-hand side.
These are the short version formed by combining the two operators.
For example Instead of writing, int a = a+3
, we can write a += 3
.
- Assignment operator: ‘=’ This operator assigns the value on the right to the variable on the left.
a = 20; B = 30; Ch = 'cha';
- Add AND assignment operator: ‘+=’ This operator first adds the current value of the variable on the left to the value on the right and then assigns the result to the variable on the left.
c+=7; a+=b;
- Subtract AND assignment operator: ‘-=’ This operator first subtracts the current value of the variable on the left from the value on the right and then assigns the result to the variable on the left.
c-=7; a-=b;
- Multiply AND assignment operator: ‘*=’ This operator first multiplies the current value of the variable on the left to the value on the right and then assigns the result to the variable on the left.
c*=7; a*=b;
- Divide AND assignment operator: ‘/=’ This operator first divides the current value of the variable on the left by the value on the right and then assigns the result to the variable on the left.
c/=7; a/=b;
- Modulus AND assignment operator: ‘%=’ It takes modulus using two operands and assigns the result to the left operand.
C %= A is equivalent to C = C % A ;
Example of Assignment Operators
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <stdio.h> int main() { int x = 10, y; y = x; // y is 10 printf("Value of y: %d\n", y); y += x; // y is 20 printf("Value of y: %d\n", y); y -= x; // y is 10 printf("Value of y: %d\n", y); y *= x; // y is 100 printf("Value of y: %d\n", y); y /= x; // y is 10 printf("Value of y: %d\n", y); y %= x; // y = 0 printf("Value of y: %d\n", y); return 0; } |
The output of Assignment Operators:
1 2 3 4 5 6 | Value of y: 10 Value of y: 20 Value of y: 10 Value of y: 100 Value of y: 10 Value of y: 0 |
C Logical Operators
Logical Operators are used in conditional statements and loops for evaluating a condition with binary values. All of the binary logical operators return two boolean values that are true and false depending upon 0 and 1.
Operator | Description | Example |
&& (logical and) | If both the operands are non-zero, then the condition becomes true. | (X && Y) is false |
|| (logical or) | If any of the two operands are non-zero, then the condition becomes true. | (X || Y) is true |
! (logical not) | Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. | !(X && Y) is true |
Example of Logical Operators
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include <stdio.h> int main() { int x = 5; int y = 20; int z; if (x && y) { printf("Line 1 - Condition is true\n"); } if (x || y) { printf("Line 2 - Condition is true\n"); } /*let's change the value of a and b */ x = 0; y = 10; if (x && y) { printf("Line 3 - Condition is true\n"); } else { printf("Line 3 - Condition is not true\n"); } if (!(x && y)) { printf("Line 4 - Condition is true\n"); } return 0; } |
The output of Logical Operator.
1 2 3 4 5 | 1st Display: true 2nd Display: true =====Values are changed====== 3rd Display: false 4th Display: true |
C Bitwise Operators
In C programming, Bitwise operators are used to perform a bit-level operation. All the mathematical operations are converted to bit-level which makes processing faster and also saves power.
Operators | Name of operators |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise complement |
<< | Shift left |
>> | Shift right |
Example of Bitwise Operators.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <stdio.h> int main() { int a = 15, b = 28; printf("Output = %d\n", a &b); printf("Output = %d\n", a | b); printf("Output = %d\n", a ^ b); printf("Output = %d\n", ~19); printf("Output = %d\n", ~-22); printf("Left shift by %d: %d\n", b>> 1); printf("Left shift by %d: %d\n", b << 1); return 0; } |
The output of Bitwise operator.
1 2 3 4 5 6 7 | Output = 12 Output = 31 Output = 19 Output = -20 Output = 21 Left shift by 14: 0 Left shift by 56: 0 |
sizeof Operator
sizeof
operator is a unary operator that returns the size of constants, variables, array, etc.
Example: sizeof(x), return size of the variable x.
Example of sizeof Operator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <stdio.h> int main() { int x; float y; double z; char ch; printf("Size of int = %lu bytes\n", sizeof(z)); printf("Size of float = %lu bytes\n", sizeof(y)); printf("Size of double = %lu bytes\n", sizeof(z)); printf("Size of char = %lu byte\n", sizeof(ch)); return 0; } |
The output of sizeof Operator.
1 2 3 4 | Size of int = 8 bytes Size of float = 4 bytes Size of double = 8 bytes Size of char = 1 byte |
Other Operators:
Comma Operator (,) | This operator is used to link related operators together. We use a comma operator when we declare many variables in a single line as shown below. Example: a, c, d = 0, z; |
& operator | This is used to get the address of the variable. Example: pt = &x |
* operator | This is used as a pointer to a variable. Example: int *pt; |
C Program to demonstrate & and * operator
In this C program, & is used to get the address of a variable and * is used to get the value of variable.
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <stdio.h> int main() { int *ptr, x; x = 10; ptr = &x; printf("%d", *ptr); return 0; } |
Output: 10