An operator is a symbol (+,-,*,/) that directs the computer to perform certain mathematics and is usually used to manipulate data and variables.
Ex: a+b
Expressions:
Combination of Operators and Operands.
Example: ( 2 * y + 5)
*,+ = operators
2,y,4 = operands
Operators in C:
Arithmetic operators
True or false table
Relational operators
Logical operators
Assignment operators
Increment and decrement operators
Conditional operators
Bitwise operators
Special operators
Arithmetic operators:
Operator Example Meaning
+ a+b Addition –unary
- a-b Subtraction- unary
* a*b Multiply
/ a/b Division
% a%b Modulo division
Operator Meaning
< Is less than
<= Is less than or equal to
> Is greater than
>= Is greater than or equal to
== Equal to
!= Not equal to
Logical Operators:
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
Logical expression or a compound relational expression-
An expression that combines two or more relational expressions
Ex: if (a==b && b==c)
Truth Table:
Value of the expressions
a b a&&b a||b
0 0 0 1
0 1 0 1
1 0 0 1
1 1 1 1
Assignment operators :
Syntax:
v op = exp;
Where v = variable,
op = shorthand assignment operator
exp = expression
Ex: x=x+3 x+=3
Increment & Decrement Operators:
C supports 2 useful operators namely :
1. Increment ++
2. Decrement – operators
The ++ operator adds a value 1 to the operand
The – operator subtracts 1 from the operand ++a or a++ --a or a--
Rules for ++ & -- operators:
1. These require variables as their operands
2. When postfix either ++ or – is used with the variable in a given expression, the expression is evaluated first and then it is incremented or decremented by one
3. When prefix either ++ or – is used with the variable in a given expression, it is incremented or decremented by one first and then the expression is evaluated with the new value
Examples for ++ & -- operators :
Let the value of a =5 and b=++a then
a = b =6 Let the value of a = 5 and b=a++ then
a =5 but b=6
i.e.:
1. a prefix operator first adds 1 to the operand and then the result is assigned to the variable on the left
2. a postfix operator first assigns the value to the variable on left and then increments the operand.
Conditional operators:
Syntax:
exp1 ? exp2 : exp3
Where exp1,exp2 and exp3 are expressions
Working of the ? Operator:
Exp1 is evaluated first, if it is nonzero(1/true) then the expression2 is evaluated and this becomes the value of the expression,
If (expression is false) exp1 is false(0/zero) exp3 is evaluated and its value becomes the value of the expression Ex: m=2; n=3 r=(m>n) ? m : n;
Bitwise operators:
Operator Meaning
&& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< Shift left
>> Shift right
Special operators
(used in programming languages):
Comma operator ( ,)
sizeof operator – sizeof( )
Pointer operators – ( & and *)
Member selection operators – ( . and ->)
1 Comments
Nice
ReplyDelete