C Operators
C Operators with examples
C-operators are a symbol that informs the compiler to perform specific mathematical or logical functions. C language has built-in operators and provides the following types of operators −
1. C Arithmetic Operators
This operator is used to perform arithmetic operations on two operands.
If A = 10 and B = 5 then,
Operator | Description | Example |
+ | It is used to add two operands. | A + B = 15 |
– | This operator is used to subtract the second operand from the first. | A − B = 5 |
* | It is used to multiplies both operands | A * B = 50 |
/ | This operator is used to divide numerator by de-numerator. | A / B = 2 |
% | This operator is used to remainder after an integer division. | A% B = 0 |
++ | This operator (the increment operator) increases the integer value by one. | A++ = 11 |
— | This operator (the decrement operator) decreases the integer value by one. | A — = 9 |
Program for arithmetic operators in c
#include <stdio.h> int main (){ float m = 25; float n = 10; printf("m = %f, n = %f \n\n", m, n); //Add m and n float result_add = m + n; printf("a + b = %f\n", result_add); //Subtract n from m float result_sub = m - n; printf("m - n = %f\n", result_sub); //Multiply a and b float result_mul = a * b; printf("m * n = %f\n", result_mul); //Divide a by b float result_div = a / b; printf("m / n = %f\n", result_div); int result_modulo = (int) m % (int) n; printf("m %% n = %d\n", result_modulo); return 0; }
Output
a = 10.000000, b = 5.000000 a + b = 15.000000 a - b = 5.000000 a * b = 50.000000 a / b = 2.00000 a % b = 0
2. C Relational Operators
Relational operators are used to comparing the values of two operands. It returns true when matched the value and returns false when values do not match.
3. C Logical Operators
Suppose variable A holds 1 and variable B holds 0, then −
Operator | Description | Example |
&& | It is called Logical AND operator. If both the operands are non-zero, then the condition becomes true. | (A && B) is false. |
|| | It is called Logical OR Operator. If any of the two operands are non-zero, then the condition becomes true. | (A || B) is true. |
! | It is called Logical NOT Operator. It is used to opposite the logical state of its operand. If a condition is true, then the Logical NOT operator will make it false. | !(A && B) is true. |
1. Logical AND operator (&&)
Program for logical AND operator in C
#include <stdio.h> void range_func(int x){ if(x >= 10 && x <= 25) printf("%d belongs to range [10, 25].\n", x); else printf("%d do not belongs to range [10, 25].\n", x); } int main (){ range_func(15); range_func(25); range_func(50); return 0; }
Output
15 belongs to range [10, 25]. 25 belongs to range [10, 25]. 50 do not belongs to range [10, 25].
2. Logical OR operator (||)
This operator is used to combine two or more conditions and it returns true when any of the conditions is true, otherwise returns false.
Program for logical OR operator in C
#include <stdio.h> void range_func(int x){ //|| operator is used to combine conditions //returns true when either x < 100 or x > 200 if(x < 100 || x > 200) printf("%d do not belongs to range [100, 200].\n", x); else printf("%d belongs to range [100, 200].\n", x); } int main (){ range_func(50); range_func(100); range_func(150); return 0; }
Output
50 do not belongs to range [100, 200]. 100 belongs to range [100, 200]. 150 belongs to range [100, 200].
3. Logical NOT operator (!)
This operator is used to return the opposite boolean results.
Program logical NOT operator in c
#include <stdio.h> void range_func(int x){ //! operator is used to return //true when x <= 100 if(!(x >= 100)) printf("%d is less than 100.\n", x); else printf("%d is greater than or equal to 100.\n", x); } int main (){ range_func(50); range_func(100); range_func(150); return 0; }
Output
50 is less than 100. 100 is greater than or equal to 100. 150 is greater than or equal to 100.
4. C Bitwise operators
This operator is used to perform bitwise operations on two operands. Bitwise operator works on bits and performs the bit-by-bit operations.
Operator | Name | Description |
& | AND | It returns 1 if both bits at the same position in both operands are 1, else returns 0 |
| | OR | It returns 1 if one of two bits at the same position in both operands is 1, else returns 0 |
^ | XOR | It returns 1 if only one of two bits at the same position in both operands is 1, else returns 0 |
~ | NOT | It reverses all the bits |
>> | Right shift | The left operand is moved right by the number of bits present in the right operand |
<< | Left shift | The left operand is moved left by the number of bits present in the right operand |
5. C Assignment Operators
C language supports the following assignment operators.
Operator | Description | Example |
---|---|---|
= | An assignment operator assigns values from right side operands to left side operand | C = A + B will assign the value of A + B to C |
+= | Add AND assignment operator adds the right operand to the left operand and assigns the result to the left operand. | C += A is equivalent to C = C + A |
-= | Subtract AND assignment operator 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 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 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 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 |
6. C Miscellaneous operators
The table below describes other operators supported by C:
Operator | Description |
comma operator(,) | Comma operator evaluates each of its operands (from left to right) and returns the value of the last operand. |
sizeof() | Returns size of a data type is constant or variable. |
Ternary operator (?:) | Returns one of the two values based on the value of the boolean expression. |
Address of operator(&) | It returns the address of a variable. |
Dereference operator(*) | Dereference operator is used to a pointer to a variable. |