09_Operators in ‘C’_pptx
-
Upload
niharranjanroy -
Category
Documents
-
view
220 -
download
0
Transcript of 09_Operators in ‘C’_pptx
-
8/3/2019 09_Operators in C_pptx
1/4
10/19/2011
1
Operators in C
Nihar Ranjan Roy
Arithmetic Operators
Operator name Syntax
Basic assignment a = bAddition a + b
Subtraction a - b
Unary plus (integer promotion) +a
Unary minus (additive inverse) -a
Multiplication a * b
Division a / b
Modulo (remainder) a % b
IncrementPrefix ++aSuffix a++
DecrementPrefix --aSuffix a--
Nihar Ranjan Roy 2
int i=5;
printf("%d",++i);
printf("%d",i++);
printf("%d",i);
6 6 7
Comparison operators/Relational operators
Operator name Syntax
Equal to a == b
Not equal to a != b
Greater than a > b
Less than a < b
Greater than or equal to a >= b
Less than or equal to a
-
8/3/2019 09_Operators in C_pptx
2/4
10/19/2011
2
Bitwise operators
Operator name Syntax
Bitwise NOT ~a
Bitwise AND a & b
Bitwise OR a | b
Bitwise XOR a ^ b
Bitwise left shift a > b
Nihar Ranjan Roy 5
a=72, b=184C=a&b;
72 0 1 0 0 1 0 0 0
184 1 0 1 1 1 0 0 0
8 0 0 0 0 1 0 0 0
int i=16;i=ib)?a:b; //biggest of a & bprintf("%d",c);
Operator Precedence
The precedence determines the order of binding in chainedexpressions, when it is not expressly specified by parentheses.
For example,
++x*3 It is ambiguous without some precedence rule(s). The precedence table tells us that:
x is 'bound' more tightly to ++ than to *,
so that whatever ++ does (now or latersee below), it does it ONLYto x (and not to x*3);
it is equivalent to (++x, x*3).
Nihar Ranjan Roy 8
-
8/3/2019 09_Operators in C_pptx
3/4
10/19/2011
3
Operator Precedence
Precedence Operator Description Associativity
1
++ Suffix increment
Left-to-right
-- Suffix decrement
() Function call
[] Array subscripting
. Element selection by reference
-> Element selection through pointer
2
++ Prefix increment
Right-to-left
-- Prefix decrement+ Unary plus- Unary minus
!Logical NOT
~ Logical bitwise NOT(type) Type cast
& Address-of
sizeof Size-of
Nihar Ranjan Roy 9
Operator Precedence..
3
* Multiplication
Left-to-right
/ division% modulus (remainder)
4+ Addition
- subtraction
5
> Bitwise right shift
6
< For relational operators < respectively For relational operators > respectively>= For relational operators respectively
7== For relational = respectively!=
8 & Bitwise AND
9 ^ Bitwise XOR (exclusive or)
10 | Bitwise OR (inclusive or)
11 && Logical AND
12 || Logical OR
Precedence Operator Description Associativity
Nihar Ranjan Roy 10
Operator Precedence..
15 ?: Ternary conditional
Right-to-left13
= Direct assignment
+= Assignment by sum
-= Assignment by difference
*= Assignment by product
/= Assignment by quotient
%= Assignment by remainder
= Assignment by bitwise right shift
&= Assignment by bitwise AND^= Assignment by bitwise XOR
|= Assignment by bitwise OR15 , Comma Left-to-right
Precedence Operator Description Associativity
Nihar Ranjan Roy 11
Evaluate
Evaluate 3*x++,
where though the post-fix ++ is designed to act AFTER the entire expression isevaluated, the precedence table makes it clear that ONLY x gets incremented(and NOT3*x);
it is functionally equivalent to something like (tmp=3*x, ++x, tmp) with tmp beinga temporary value.
Nihar Ranjan Roy 12
-
8/3/2019 09_Operators in C_pptx
4/4
10/19/2011
4
Casting (Implicit & Explicit)
Implicit Casting (automatic transformation) works in a way that a variable (operand)of data type that is smaller in length (than data type of second variable) (operand),transforming internally to variable of data type with longer number length.
Shortint
intUnsigned
intLong int
Unsignedlong int
float doubleLongdouble
float a;
int b=1,c=2;
a=b+c;
Nihar Ranjan Roy 13
Casting (Implicit & Explicit)
Explicit Casting
When one data types has higher priority then automatic transformation
This is not done automatically, the programmer has to do it explicitly
Syntax
(data_type) operand
Float a;
int b=1,c=2;
a=b/c;
a=(float) b/c;
//output is 0.00
//output is 0.5000
Nihar Ranjan Roy 14
Overflow
char a=255;
a=a+1
printf("%d",a);
Output?
Nihar Ranjan Roy 15
0