Category of Operators
Operand1 operator
Here Operand1 may be a variable , a constant or an expression etc.
For example: -b , ~a , x+1 etc.
For example: a+b , a && b , etc.
For example: a+b+c , a && b || c , a>b>c
TYPES OF OPERATOR
There are various operators used in C language.There are mainly eight operators used in C language.Arithmetic Operators
Suppose A=5 & B=10 are two variables, then Arithmetic operators used for different operations as :Operator | Description | example |
---|---|---|
+ | Adds two operands | A+B gives 15 |
- | subtracts second operand from first | A-B gives -5 |
* | multiply the two operands | A*B gives 50 |
/ | divide numerator by denominator | B/A gives 2 |
% | modulus operator | B%A gives 0 |
Relational Operators
It is required to compare the relationship between operands and bring out at decisions and program accordingly for which relational operators are used.Mainly relational operators are categorised into two sections one is comparison section and second one is equality section.There are mainly 6 relational operators used in the C language , first two operators are in the equality section and last four are in the comparison section.
Suppose A=5 & B=20 are two variables,then arithmetic operators used for different operations as:
Operator | Description | example |
== | Checks if the value of two operands is equal or not, if yes then condition becomes true . | (A==B) is not true. |
!= | Checks if the value of two operands is equal or not, if values are not equal then condition becomes true . | (A!=B) is true. |
> | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (A>B) is not true. |
< | Checks if the value of left operand is less than the value of a right operand, if yes then condition becomes true . | (A |
>= | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true . | (A>=B)< is not true. |
<= | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true . | (A<=B) is true. |
These operators are used in if statement, switch statement, conditional operator statement, while statement, do-while statement and for statement.These operators are mainly used for decision making statement .These operators give the results in two branches ,one is of true type and other is of false type.
Logical Operators
A logical operator is used to compare or evaluate logical and relational expressions. These operations are used for compound relational expression or logical expressions .These are used in decision making statement and some looping statements.
Suppose A=5 & B=10 are two variables,then arithmetic operators used for different operations as:
Operator | Description | example |
&& | Called logical AND operator.If both the operands are non zero then condition becomes true. | (A&&B) is true. |
|| | Called logical OR operator.If any of the two operands is non zero then condition becomes true. | (A||B) is true. |
! | Called logical NOT operator.Use to reverse the logical state of its operand. If a condition is true then logical NOT operator will make false. | !(A&&B) is false. |
Assignment Operators
The assignment operator evaluates an expression on the right of the expression and substitute it to the value of a variable on the left of the expression.
Operator | Description | example |
= | Simple assignment operator, assigns values from right side operands to left side operand. | C=A+B will assign value of A+B into C. |
+= | Add AND assignment operator, it adds right operand to the left operand and assign the result to left operand. | C+=A is equivalent to C=C+A. |
-= | Subtract AND assignment operator ,it subtracts right operand from the left operand and assign the result to left operand. | C-=A is equivalent to C=C-A |
*= | multiply AND assignment operator ,it multiplies right operand with the left operand and assign the result to left operand. | C*=A is equivalent to C=C*A |
/= | divide AND assignment operator ,it divides left operand with the right operand and assign the result to left operand. | C/=A is equivalent to C=C/A. |
%= | modulus AND assignment operator ,it takes modulus using two operands and assign the result to left operand. | C%=A is equivalent to C=C%A |
<<= | left shift AND assignment operator. | C<<=2 is equivalent to C=C<<2 |
>>= | right shift AND assignment operator. | C>>=2 is equivalent to C=C>>2 |
&= | BitwiseAND AND assignment operator. | C &=2 is equivalent to C=C&2 |
^= | Bitwise exclusive OR and assignment operator. | C ^=2 is equivalent to C=C^2 |
|= | Bitwise inclusive OR and assignment operator. | C |=2 is equivalent to C=C|2 |
Conditional or Ternary Operator
The conditional operator consists of two symbols the question mark(?) and the colon (:).The syntax for a ternary operator is as follows:
(exp1 ? exp2 : exp3;)Operator | Description | example |
? : | Conditional Exlression | If condition is true ? Then value X:Otherwise value Y |
Increment & Decrement Operators
These operators also fall under unary operator but are quite distinct than unary minus(-).The increment(++) and decrement(--) operators are very useful in C language.Increment operators are used to increment value one by one and decrement operators are used to decrement the value one by one. They are extensively used in for and while loops .These operators can be used in either the prefix for postfix notations.
Suppose A=5 a variable,then arithmetic operators used for different operations as:Operator | Description | example |
++ | Increment operator,increases integer value by one | A++ will give 6 |
-- | Decrement operator,decreases integer value by one | A-- will give 4 |
Bitwise Operators
As programmers will occasionally need to work with streams of ones and zeros.To facilitate this requirement, C provides a range ofbit operators.
Suppose A=171 & B=3 are two variables:Operator | Description | example |
& | Binary AND Operator copies a bit to the result if it exists in both operands. | (A&B) will give 3 |
| | Binary OR Operator copies a bit if it exists in either operand. | (A|B) will give 171 |
^ | Binary XOR Operator copies a bit if it is set in one operand but not both. | (A^B) will give 168 |
~ | Binary ONES Complement Operator is unary and has the effect of 'flipping' bits. | (~A) will give 84 |
<< | Binary LEFT SHIFT. The left operands value is moved left by the number of bits specified by the right operand. | (A<<1) will give 342 |
>> | Binary RIGHT SHIFT Operator. The left operands value is moved right by the number of bits specified by the right operand. | (A>>1) will give 85 |
Special Operators
C supports some special operators of interest such as comma operator, size of operator, pointer operators etc.
Operator | Description | example |
sizeof() | Returns the size of an variable. | sizeof(a) will give 4, where a is integer. |
& | Returns the address of an variable. | &a, will give actual address of the variable. |
* | pointer to variable. | *a, will point to the variable. |
Prev
next
Comments
Post a Comment