Operators
Operators are the symbol which operates on value or a variable. For example: + is a operator to perform addition.
C programming language has wide range of operators to perform
operations. For better understanding of operators in C, these operators
can be classified as:
Operators in C programming |
Arithmetic Operators |
Increment and Decrement Operators |
Assignment Operators |
Relational Operators |
Logical Operators |
Conditional Operators |
Bitwise Operators |
|
Arithmetic Operators:
Operator |
Meaning of Operator |
+ |
addition or unary plus |
- |
subtraction or unary minus |
* |
multiplication |
/ |
division |
% |
remainder after division( modulo division) |
Increment and decrement operators:
In C, ++ and -- are called increment and decrement operators
respectively. Both of these operators are uniary operators, i.e, used on
single operand. ++ adds 1 to operand and -- subtracts 1 to operand
respectively. For example:
Let a=5 and b=10
a++; //a becomes 6
++a; //a becomes 7
--b; //b becomes 9
b--; //b becomes 8
Assignment Operators:
The most common assignment operator is '='. This operator assigns the value in right side to the left side. For example:
var=5 //5 is assigned to var
a=c; //value of c is assigned to a
5=c; // Error! 5 is a constant and its value can't be changed.
Operator |
Example |
Same as |
= |
a=b |
a=b |
+= |
a+=b |
a=a+b |
-= |
a-=b |
a=a-b |
*= |
a*=b |
a=a*b |
/= |
a/=b |
a=a/b |
%= |
a%=b |
a=a%b |
Relational Operator:
Relational operators are used to test relationship between two
operands. If the relation is true, it returns value 1 and if the
relation is false, it returns value 0. For example:
a>b
Here, > is a relational operator. If a is greater than b, a>b returns 1 if not then, it returns 0.
Relational operators are normally used in decision making and loops in C programming.
Operator |
Meaning of Operator |
Example |
== |
Equal to |
5==3 returns false (0) |
> |
Greater than |
5>3 returns true (1) |
< |
Less than |
5<3 returns false (0) |
!= |
Not equal to |
5!=3 returns true(1) |
>= |
Greater than or equal to |
5>=3 returns true (1) |
<= |
Less than or equal to |
5<=3 return false (0) |
Logical Operators
Logical operators are used to combine expressions containing relation operators. In C, there are 3 logical operators.
Operator |
Meaning of Operator |
Example |
&& |
Logial AND |
If c=5 and d=2 then,((c==5) && (d>5)) returns false. |
|| |
Logical OR |
If c=5 and d=2 then, ((c==5) || (d>5)) returns true. |
! |
Logical NOT |
If c=5 then, !(c==5) returns false. |
Conditional Operator:
Conditional operator takes three operands and consists of two symbols
? and : . Conditional operators are used for decision making in C. For
example:
c=(c>0)?10:-10;
If c is greater than 0, value of c will be 10 but, if c is less than 0, value of c will be -10.
To learn more, visit conditional operators page.
Bitwise Operators:
A bitwise operator works on each bit of data. Bitwise operators are used in bit level programming.
Operators |
Meaning of operators |
& |
Bitwise AND |
| |
Bitwise OR |
^ |
Bitwise exclusive OR |
~ |
Bitwise complement |
<< |
Shift left |
>> |
Shift right |
Bitwise operator is advance topic in programming . You go further in C programming without knowing bitwise programming
No comments:
Post a Comment