Type Conversions: Coercion Related Problem
Type Conversions: Coercion Related Problem
Type Conversions
Coercion Related Problem Some assignment with type coercion are acceptable.
i = 8 4 5 . 2 4 ; // i h a s 845 i = 8 4 5 . 2 4 ; // i h a s 845
Other cases of assignment with type coercion may not be acceptable due to range violation.
c = 1 0 0 0 0 ; // WRONG: e x c e e d s r a n g e i = 1 . 0 e34 ; // WRONG: r e s u l t s i n o v e r f l o w i n c o n v e r s i o n f = 1 . 0 e34 ; // WRONG: r e s u l t s i n l o s s o f p r e c i s i o n
R. K. Ghosh (IIT-Kanpur)
C Programming
21 / 26
Type Conversions
Example 12 (Casting) Use explicit type casting when needed: ( type ) expression Some example are given below:
float frac part = f ( int ) f ;
i = ( int ) f ; float quotient ; int dividend , d i v i s o r ; quotient = ( float ) dividend / ( float ) divisor ; quotient = ( float ) dividend / divisor ; quotient = dividend / ( float ) divisor ;
R. K. Ghosh (IIT-Kanpur)
C Programming
21 / 26
Type Conversions
Example 13 (Casting) Explicit casting prevents arithmetic overow.
int i ; i n t j = 1000; i = j j; // c o u l d r e s u l t i n o v e r f l o w i = ( long ) j j ; // p r e v e n t s o v e r f l o w i = ( l o n g ) ( j j ) ; // o v e r f l o w a s i t o c c u r s by t h e t i m e o f c a s t i n g
R. K. Ghosh (IIT-Kanpur)
C Programming
21 / 26
Logical Expressions
Relational & Equality Operators Relation operators: <, <=, >=, > Precedence of relation operators are less than arithmetic operators. i+1 > j+3 evaluates to 1, if the relation is true, otherwise false. Expression i < j < k is equivalent to (i < j) < k. So, results in 1, when i = 10, j = 5, k = 40. Equality operators ==, != have lower precedence, eg. i < j == j < k is equvalent to (i < j) == (j < k)
R. K. Ghosh (IIT-Kanpur)
C Programming
22 / 26
Logical Expressions
Logical Operators Three operators: negation (!), and (&&), (||). ! has same precedence as unary operators and right associative, ie., !!a is !(!a). Precedence of || and && are lower than relational and equality opertor and they are left associative. So, i < j && k == m means (i < j) && (k==m) i < j < k is legal but meaningless.
Consider i = 40, j = 160, j = 90 Evaluation order: (i < j) < k, so it returns true.
R. K. Ghosh (IIT-Kanpur)
C Programming
22 / 26
Logical Expressions
Evaluation of Logical Expressions
int i = 2 , j = 3; i n t k = i j == 6 ; // k = ( ( i j ) == 6 ) i n t i = 5 , j = 10 , k = 1 ; p r i n t f (%d , k > i < j ) ; // ( k > i ) < j => 1
=> 1
i n t i = 3 , j = 4 , k =5; p r i n t f (%d , i % j + i < k ) ; // ( ( i % j ) + i ) < k => 0 i n t i = 10 , j = 5 ; p r i n t f (%d , ! i < j ) ; // ( ! i ) < j i n t i = 5 , j = 0 , k = 5; p r i n t f (%d , i && j | | k ) ; // ( i && j ) | | ( k )
=> 1
=> 1
R. K. Ghosh (IIT-Kanpur)
C Programming
22 / 26
Logical Expressions
Example 14
#i n c l u d e < s t d i o . h> main ( ) { int a = 1 , b = 0 , c = 1; printf printf printf printf printf printf printf printf printf printf printf a || }
R. K. Ghosh (IIT-Kanpur) C Programming January 13, 2011 22 / 26
( a && b i s \n%i \n\n , a && b ) ; ( a && c i s \n%i \n\n , a && c ) ; ( a | | b i s \n%i \n\n , a | | b ) ; // b n o t e v a l u a t e d ( a | | c i s \n%i \n\n , a | | c ) ; // c n o t e v a l u a t e d ( ! a i s \n%i \n\n , ! a ) ; ( ! b i s \n%i \n\n , ! b ) ; ( ! ( a && b ) i s \n%i \n\n , ! ( a && b ) ) ; ( ! ( a && c ) i s \n%i \n\n , ! ( a && c ) ) ; ( ! a | | ! b i s \n%i \n\n , ! a | | ! b ) ; ( ! a | | ! c i s \n%i \n\n , ! a | | ! c ) ; ( The v a l u e o f a | | ( ! c && ( ! b | | a ) ) i s \n%i \n\n , ( ! c && ( ! b | | a ) ) ) ; // o n l y a i s e v a l u a t e d
Logical Expressions
Example 15 (boolean values)
#i n c l u d e < s t d i o . h> i n t main ( ) { int k; p r i n t f ( Enter k : ) ; s c a n f (%d , &k ) ; i f (k) p r i n t f ( TRUE, v a l u e o f k = %d\n , k ) ; else p r i n t f ( FALSE , v a l u e o f k = %d\n , k ) ; }
R. K. Ghosh (IIT-Kanpur)
C Programming
22 / 26
Logical Expressions
Example 16
#i n c l u d e < s t d i o . h> i n t main ( ) { i n t i = 3, j = 2 , k = 0 , m; m = ++i | | ++j && ++k ; // (++ i ) | | ((++ j ) && (++k ) ) p r i n t f (%d %d %d %d\n , i , j , k , m) ; }
Outputs -2 2 0 1 Precedence of && greater than ||, but short circuit evaluation occurs in A || (B && C). So, RHS of ||, i.e., (B&&C) not evaluated as LHS (i.e., A) is 1.
R. K. Ghosh (IIT-Kanpur)
C Programming
22 / 26