C Operators
.
* An operator is a specific symbol that instruct to the compiler to perform mathematical or logical operations.
**An operator is a symbol used to perform mathematical and logical operations in a program.
*** C language supports a huge set of operators that are classified as follows…
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Increment & Decrement Operators
- Assignment Operators
- Bitwise Operators
- Conditional Operator
- Specific/Special Operators
1)Arithmetic Operators (+, -, *, /, %)
The arithmetic operators are the symbols like addition, subtraction, multiplication, division and percentage modulo that are used to perform basic mathematical operations.
The following table provides information about arithmetic operators…
Operator | Meaning | Example |
+ | Addition | 15 + 3 = 18 |
– | Subtraction | 15 – 3 = 12 |
* | Multiplication | 15 * 3 = 45 |
/ | Division | 15 / 5 = 3 |
% | Remainder of Division | 15 % 2 = 1 |
⇒ The addition operator can be used either with numerical data types or
character data type both. When it is used with ‘numerical values’, it performs
mathematical addition and when it is used with ‘character data type values’, it
performs concatenation (appending).
⇒ In case of division operator, remainder is used with integer data type
only.
2)Relational Operators (<, >, <=, >=, ==, !=)
For the comparison between two values, symbols of ‘relational operators’ are used.
In other words, the relational operators are used to check the relationship between two values. Every relational operator has two results TRUE or FALSE.
In simple words, the relational operators are used to detect conditions in a program. The following table provides information about relational operators…
Operator | Meaning | Example |
< | Returns TRUE if first value is smaller than second value otherwise returns FALSE | 14 < 7 is FALSE |
> | Returns TRUE if first value is larger than second value otherwise returns FALSE | 14 > 7 is TRUE |
<= | Returns TRUE if first value is smaller than or equal to second value otherwise returns FALSE | 14 <= 7 is FALSE |
>= | Returns TRUE if first value is larger than or equal to second value otherwise returns FALSE | 14 >= 7 is TRUE |
== | Returns TRUE if both values are equal otherwise returns FALSE | 14 == 7 is FALSE |
!= | Returns TRUE if both values are not equal otherwise returns FALSE | 14 != 7 is TRUE |
3)Logical Operators (&&, ||, !)
The symbolic representation that are used to combine multiple conditions into one condition are called ‘logical operators’.
The following table provides information about logical operators…(for better understanding )
Operator | Meaning | Example |
&& | Logical AND – Returns TRUE if all conditions are TRUE otherwise returns FALSE | 14 < 7 && 16 > 7 is FALSE |
|| | Logical OR – Returns FALSE if all conditions are FALSE otherwise returns TRUE | 10 < 5 || 12 > 10 is TRUE |
! | Logical NOT – Returns TRUE if condition is FLASE and returns FALSE if it is TRUE | !(10 < 5 && 12 > 10) is TRUE |
⇒ Logical AND(&& ) – Logical AND returns
‘TRUE’ only if all conditions are ‘TRUE’, if any of the conditions are ‘FALSE’
then complete condition will become ‘FALSE’.
⇒ Logical OR(||) – Logical
OR returns FALSE only if all
conditions are FALSE, if any of the conditions is ‘TRUE’ then complete
condition becomes ‘TRUE’.
4)Increment & Decrement Operators (++ & –)
The increment and decrement operators are called as unary operators because, both needs to use only one operand. The increment operators add one to the existing value of the operand and the decrement operator subtracts one from the existing value of the operand. The following table provides information about increment and decrement operators…
Operator | Meaning | Example |
++ | Increment – Add one to existing value |
int a = 7; a++; ⇒ a = 8 |
— | Decrement – Subtract one from existing value |
int a = 7; a–; ⇒ a = 6 |
Both the operators are used in front of the operand (++a) or after the operand (a++). If it is used infront of the operand, we call it as pre-increment or pre-decrement and if it is used after the operand, we call it as post-increment or post-decrement.
Pre-Increment or Pre-Decrement
In case of pre-increment, variable value is increased by one before the expression evaluation. In case of pre-decrement, the value of the variable is decreased by one before the expression evaluation. Simply we can say that when we use pre-increment or pre-decrement, first of all the value of the variable is increased or decreased by one, then the modified value is used in the evaluation of expression.
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int i = 7,j;
j = ++i; // Pre-Increment
printf(“i = %d, j = %d”,i,j);
}
Output: i=8, j=8
Process returned 0 (0*0 )
Press any key to continue.
Post-Increment or Post-Decrement
On the other hand in case of post-increment, the value of the variable is increased by one after the expression evaluation.
In case of post-decrement, the value of the variable is decreased by one after the expression evaluation.
It clears, when we use post-increment or post-decrement, first of all the expression is evaluated with existing value, then the variable value is increased or decreased by one.
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int i = 7,j;
j = i++; // Post-Increment
printf(“i = %d, j = %d”,i,j);
}
Output: i= 8 , j= 7
Press any key to continue.
5)Assignment Operators (=, +=, -=, *=, /=, %=)
The assignment operators are used to assign RHS value (also called Rvalue) to the LHS variable (also called Lvalue).
The assignment operator is used in different variants along with arithmetic operators.
à The following table describes all the assignment operators avaliable in C language:-
Operator | Meaning | Example |
= | Assign the RHS value to LHS variable | A = 15 |
+= | Add both left and right hand side values and store the result into left hand side variable |
A
+= 10 ⇒ A = A+10 |
-= |
Subtract RHS
value from LHS variable value and store the result into LHS variable. |
A
-= B ⇒ A = A-B |
*= |
Multiply RHS
value with LHS variable value and store the result into LHS variable |
A
*= B ⇒ A = A*B |
/= |
Divide LHS
variable value with RHS variable value and store the result into LHS variable |
A
/= B ⇒ A = A/B |
%= |
Divide LHS
variable value with RHS variable value and store the remainder into LHS
variable. |
A
%= B ⇒ A = A%B |
6)Bitwise Operators (&, |, ^, ~, >>, <<)
In c programming language, the bitwise operators are used to perform bit level operations.
When we use the bitwise operators, the operations are performed based on the binary values.
The following table describes all the bitwise
operators in C programming language.
Let us consider two variables A and B as A = 25 (11001) and B = 20 (10100).
Operator | Meaning | Example |
& | the result of Bitwise AND is 1 if all the bits are 1 otherwise it is 0 |
A
& B ⇒ 16 (10000) |
| | the result of Bitwise OR is 0 if all the bits are 0 otherwise it is 1 |
A
| B ⇒ 29 (11101) |
^ | the result of Bitwise XOR is 0 if all the bits are same otherwise it is 1 |
A
^ B ⇒ 13 (01101) |
~ | the result of Bitwise once complement is nagation of the bit (Flipping) |
~A ⇒ 6 (00110) |
<< | the Bitwise left shift operator shifts all the bits to the left by specified number of positions |
A
<< 2 ⇒ 100 (1100100) |
>> | the Bitwise right shift operator shifts all the bits to the right by specified number of positions |
A
>> 2 ⇒ 6 (00110) |
7)Conditional Operator (?:)
The conditional operator is also called as ternary operator because it requires to use three(3) operands. This operator is used for decision making.
In this operator, at first we verify a condition, then we perform one operation out of two(2) operations that is based on the condition result.
If the condition is ‘TRUE’ the first option is performed, if the condition is FALSE the second option is being performed.
à The following syntax are used as the conditional operator…
Condition ? TRUE Part : FALSE Part ;
Example
A = (12<17) ? 120 : 200 ; ⇒ A value is 80
8)Special Operators (sizeof, pointer, comma, dot etc.)
In c programming language, there are some special operators given below:-
sizeof operator
This operator is used to find the size of the memory (in bytes) allocated for a variable. This operator is used with the following syntax…
sizeof(variableName);
Example
sizeof(A); ⇒ result is 2 if A is an integer
Pointer operator (*)
:-Pointer operator is used to define pointer variables in c programming language.
Comma operator (,)
:-Coma operator is used to separate variables while they are declaring, separate the expressions in function calls etc..
Dot operator (.)
:-Dot operator is used to access members of structure or union.
Mitthu Nirmal
21 Apr 2019Supppbbbb and amazing site made for all bca students