Operators are used to perform some operations in C Programming Language and can be categorized in two ways-
- Based on Operands
- Based on functionality
1. Based on Operands
1. Unary Operators
Operators which take only one operand or perform operations on single operands are known as Unary Operators. Example: ++,--,- etc are unary operators.
2. Binary Operators
Operators which take two operands or perform operations on two operands are knows as Binary Operators. Example: +, *, /, % etc.
3. Ternary Operators
Operators which take three operands or perform operations on three operands are known as Ternary Operators. Example: Conditional Operator(? :).
2. Based on Functionality
Operators in C Language |
1. Arithmetic Operators
Operators are used to perform arithmetic operations are known as Arithmetic Operators. Example: +,-,*,/ and %.
Operator Symbol | Meaning | Example (X=9, Y=2) | Result |
+ | Addition | X+Y | 11 |
- | Subtraction | X-Y | 7 |
* | Multiplication | X*Y | 18 |
/ | Division | X/Y | 4 |
% | Modulus or Remainder | X%Y | 1 |
**Point to Remember- the division result of integer values is always an integer value.
2. Relational Operators
Operators are used to perform Relational operations are known as Relational Operators. Example: <,>,<=,>=,== and !=. The result of Relational Operator is always either True or False.
Operator Symbol | Meaning | Example (X=9, Y=2) | Result |
= = | Equal to | X= =Y | False |
! = | Not Equal to | X! = Y | True |
< | Less Than | X<Y | False |
> | Greater Than | X>Y | True |
< = | Less Than or Equal to | X < = Y | False |
> = | Greater Than or Equal to | X > = Y | True |
3. Logical Operators
Operators are used to perform Logical Operations are known as Logical Operators. Example: ||,&&,!.
Logical AND Table | Logical OR Table | Logical NOT Table | |||||
X | Y | X && Y | X | Y | X || Y | X | !X |
True | True | True | True | True | True | True | False |
True | False | False | True | False | True | ||
False | True | False | False | True | True | False | True |
False | False | False | False | False | False |
Operator Symbol | Meaning | Example (X=9, Y=2) | Result |
&& | Logical AND | X > 5 && Y >5 | False |
|| | Logical OR | X > 5 || Y > 5 | True |
! | Logical NOT | !(X > 5 || Y > 5) | False |
4. Assignment Operators
Operators are used to perform Assignment operations are known as Assignment Operators. Example: =,+=,-=,*=,/= and %=.
Assignment Operators +=, -=, *=, /= and %= are also known as short hand operators in C Programming.
let us understand why these are called short hand operators-
a=a+5 is similar to a+=5 according to result but wait here += is single operator according to compiler hence a+=5 is faster than a=a+5 and that's why these are called short hand operators. similarly -=, /=,*= and %=.
Operator Symbol | Meaning | Example (X=9, Y=2) | Result |
= | Assignment | Z=X | value of X is assigned to Z |
+= | addition before assignment | X+=Y | X is 11 similar to X=X+Y |
-= | subtraction before assignment | X-=Y | X is 7 similar to X=X-Y |
*= | Multiply before assignment | X*=Y | X is 18 similar to X=X*Y |
/= | Divide before assignment | X/=Y | X is 4 similar to X=X/Y |
%= | Modulus before assignment | X%=Y | X is 1 similar to X=X%Y |
5. Increment and Decrement Operators(Unary Operators)
Operators are used to perform Increment and Decrement operations are known as Increment and Decrement Operators. Example: ++, --.
These operators are used in pre and post order and hence there will be of two meanings.
Let us understand the difference between pre and post increment or decrement-
assume we have two variables a and b. variable a having value 5 and we have to assign value with increment operator or decrement operator like-
b=++a; //assume a=5
//here ++ is used before variable a hence pre increment is used. by this processor, read a than increment its value by 1 and than assign to variable b. after operation a=6 and b=6.
b=a++; //assume a=5
//here ++ is used after variable a hence post increment is used. By this processor read a, assign its value to b and than it increment the value by 1. After operation a=6 and b=5.
Similar with pre and post decrement also
b=--a; //assume a=5 and after operation a=4 and b=4.
b=a--; // assume a=5 and after operation a=4 and b=5.
Operator Symbol | Meaning | Example (X=9) | Result |
++ | Increment | ++X (pre increment) X++ (post increment) |
X is 10 |
-- | Decrement | --X (pre decrement) X-- (post decrement) |
X is 8 |
5. Bitwise Operators
Operators are used to perform operations on Bits are known as Bitwise Operators. Example: |,&,~,<<,>>
Operator Symbol | Meaning | Example (X=6, Y=3) | Result |
& | Bitwise AND | X&Y | 2 |
| | Bitwise OR | X|Y | 7 |
~ | Bitwise NOT | ~X | -7 |
>> | Bitwise Right Shift | Y>>1 | 1 |
<< | Bitwise Left Shift | Y<<1 | 6 |
6. Conditional Operators
Operators are used to perform operations on Conditional basis are known as Conditional Operators and also known as Ternary Operator. Example: ? :
Operator Symbol | Meaning | Example (X=9, Y=2) | Result |
? : | Conditional Operator | (X>9)?X=5:Y=5 | 5 is assigned to Y |
7. Special Operators
Operators are used to perform some special operations are known as Special Operators. Example: sizeof(), pointer operator(*), Address Operator(&) and Comma operator(,).
Operator Symbol | Explaination |
sizeof() | sizeof() is not a function it is an operator which is used to return an integer value of allocation size of a variable. Example: int a; //here a is an integer variable float b; //here b is a float variable sizeof(a) //it gives 2 because a is integer and its allocation size is 2 byte. sizeof(b) // it gives 4 because b is float variable and its allocation size is 4 byte. |
* | pointer operator is used to declare pointer as well as to fetch the value from an address which is pointed by pointer. Example: int a=5; //a variable having value 5 int *b; // here * is used to declare pointer variable b=&a; //pointing to location a printf("%d", *b); //here * is used to fetch value from address pointed by pointer b. |
& | Address operator is used to take an address of a variable. Example: int *b=&a;// here address of a is assigned to pointer b. |
-> | arrow operator is used in a program when the member of structure or union are fetched by using pointer. We will discuss it in Structure or Union section. |
, | comma operator is used to make different operation side by side in sequence. Example: c=5,b=c,a=b; in this 5 is assigned to c first than c is assigned to b and than finally b is assigned to a. |
No comments:
Post a Comment