Created: 2023-08-16 17:21
Status: #concept
Subject: Programming
Tags: C C Data Type Byte ASCII
C Type Conversion
Type Casting System
Type casting is considered a unary Operator that lets us explicitly transform types.
- it has higher precedence than arithmetic operators and will execute first:
(float) dividend / divisor
becomes( (float) dividend ) / divisor
.
(type-name) expression
type-name
specifies the type the expression should be converted into.
/* an idiom to extract the fractional part of a float value */
float f, frac_part;
frac_part = f - (int) f;
Common Pitfalls
We cannot perform implicit type casting on the data types which are not compatible with each other such as:
- Converting
float
to anint
will truncate the fraction part hence losing the meaning of the value. - Converting
double
tofloat
will round up the digits. - Converting
long int
toint
will cause dropping of excess high order bits.
Keep in mind the following rules for programming practice when dealing with different data type to prevent from data loss:
- Integer types can be converted to
float
. - Float types can be converted to
double
. - Character types can be converted to
integer
.
The keyword "can" does not mean we should always do it, but they are acceptable routes to take when converting from a narrower Byte size type to a wider one.
You can use the sizeof Operator to find the size of a reserved type keyword, a Variable, or Constant value.
sizeof (char) // evaluates to 1
sizeof (int) // usually evaluates to 4 in 32-bit machines
int i, j;
sizeof (i + j) // evaluates to 4 since it is an int
Implicit Type Conversion System
When you perform arithmetic Operations between two different C Data Types, the compiler will convert a data type to match the other one.
- this happens because computers cannot add data with different types and must convert the data to have matching Byte sizes.
When there is a value with too many bits, the compiler will truncate the extra bits and only read the ones that fit, leading to unexpected behavior.
Example
When adding a 16-bit short int
with a 32-bit int
will make the compiler convert the short int into a 32-bit int
.
Similarly, if we use an int
and a float
, the compiler will make both operands float
since float
requires more Bytes to store data.
char c;
int i;
float f;
double d;
i = c; /* c is converted to int */
f = i; /* i is converted to float */
d = f; /* f is converted to double */
Common Pitfall: Assigning Float to Integer Variables
Converting
float
to int
truncates the fractional/decimal part of the number.
int i;
i = 842.97f; /* i is now 842 */
i = -842.97f; /* i is now -842 */
When is it performed?
- In Expressions
- during arithmetic Operations:
12 + 1.5f
- during arithmetic Operations:
- In Functions
- when we assign a different type value to the function parameters.
- when we return a different type instead of the
return
type.
- In Variable Initialization
- When we assign a wrong C Data Type, the compiler will prioritize the declared variable's type:
int x = 'a'
will become97
due toa
's ASCII value.- Characters are internally mapped & represented as Integers.
- When we assign a wrong C Data Type, the compiler will prioritize the declared variable's type: