DECLARATION OF INTEGER (int) TYPE:
CASTING DATA TYPES:
Result type of arithmetic phrase depends of operand types inside it. When a phrase is consisted of various data types, result type is set by defined casting rules. Rules for casting various data types in C language are oriented to higher data type. There are two ways of casting data types in C:
· automatic (implicit)
· given (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. It may sound mixed up, but here is an example:
short int -> int -> unsigned int ->long int -> unsigned long int -> float -> double -> long double
This table shows standard automatic casting of one type of operand to another type of operand in binary phrases (according to MSDN Library for MS Visual C++ 6.0):
One of operands is long double.
Second operand transforms to long double.
Previous rule doesn’t apply, and one of operands is double.
Second operand transforms to double.
Previous rule doesn’t apply, and one of operands is float.
Second operand transforms to float.
Previous rules don’t apply, and none of operands is real type operand.
Integer casting is made by following rules:
Important: rules for integer and real data type casting can differ from one translator programs to another.
unsigned long b;
float f, g;
g = a + f; // a transforms to float
d = a + b; // a and b transform to unsigned long, adding
// is produced in unsigned long domain and then
// the result type unsigned long is transformed
// to double
long lm = LONG_MAX, l; // LONG_MAX = 2147483647
unsigned long um = ULONG_MAX; // ULONG_MAX = 4294967295
l = lm + 1; // result: -2147483648 (fill over to neg. field)
f = lm + 1; // result: -2147483648.000000 (result is made
// in long domain)
l = um + 1; // result: 0 (fill over)
f = um + 1; // result: 0.000000 (result is made in
// unsigned long domain)
Explicit Casting (given transformation) of data types has higher priority then automatic transformation. General declaration of explicit (given) cast (cast operator):
Operand can be variable or phrase.
a = (int) c;
b = (double)d + c;