An arithmetic operation between two integers gives an integer result. Operation between two real numbers gives a real result. Let us turn our attention to cases when the operands in an expression are of different types. The general rule is that if a binary operator has operands of different types, then the ‘lower’ type is promoted to the ‘higher’ type, before the operation proceeds. If either operand is long double convert other to long double and result is long double. If one operand is integer and other float, integer is converted to float and result is float and so on.
5 / 2. 0 = 5.0 / 2.0 = 2.5
2.0 / 5 = 2.0 /5.0 = 0.4
2 / 5 = 2 / 5 = 0
5 / 2 = 5 / 2 = 2
5.0 / 2.0 = 5.0 / 2.0 = 2.5
Further if type of expression on the right of ‘=’ and type of variable on the left differ, then the value of expression is promoted or demoted depending upon the type of variable on left side of ‘=’,
E.g.int j ; float b ; j = 3.5 ; b = 30 ;
Here though 3.5 is a real number. When it is assigned to j (which is an integer), it is converted to an integer i. e. j becomes 3. Conversion to an integer truncates the decimal part. Also 30 assigned to b convert it to floating point value 30.000. Since b is a float variable.
In the following expression.
Here since the expression on the right is a mixed type, whenever an operator has different types of operands, they are converted according to above given rule and then expression solved. Whatever will be the resulting type of expression on right it is finally truncated to integer value and assigned to s. Since s is of integer type.
C Related Interview Questions
|DBMS Interview Questions||C++ Interview Questions|
|C & Data Structures Interview Questions||Java Interview Questions|
|Go (programming language) Interview Questions||F Sharp (programming language) Interview Questions|
|C preprocessor Interview Questions||R Programming language Interview Questions|
|D Programming Language Interview Questions||Lua (programming language) Interview Questions|
|Embedded C Interview Questions|
Introduction To C-programming
Data Types And Storage Classes
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.