Menu Close

Articles

C: Type casting

Type casting is the conversion of one data type into another data. This conversion can be implicit or explicit. The implicit conversion is possible when there is not possible to loose information during data transformation. Explicit casting is necessary when data conversion may cause data losses or data alteration. 

Implicit casting

Sometimes implicit casting of integer type is also called “integer promotion”. There is an type hierarchy from smaller integer size data larger size that can be done with no data loses and is performed implicit using assign operator.

Integer promotion:  char -> short int -> int -> long int 

Float promotion: float -> double

In addition, integer can be converted to float or double. Unsigned can be converted to signed integer with no data loses. If data casting fail, C will just give you bad result and will not produce an error.

Expressions result 

C operators are overloaded. For example division operator “/” can operate with integer types or float types. Sometimes the result is unexpected. Therefore we need to pay attention to operands and perform explicit conversion if appropriate.

Example:

Output:

Explicit casting

This is necessary when conversion is unsafe. The notation is very strange, you must get use to it. Same notation is used in Java. You must prefix the variable with type name enclosed in parentheses:

Example:

Output:

Numeric Limits

Library file: limits.h  includes definitions of the characteristics of common variable types. The values are implementation specific, but may not be of lower magnitude than specified standard C.

NameDescriptionTypical value
CHAR_BITNumber of bits in a char8
SCHAR_MINMinimum value for a signed char−128
SCHAR_MAXMaximum value for a signed char+127
UCHAR_MAXMaximum value for an unsigned char+255
CHAR_MINMinimum value for a char−128
CHAR_MAXMaximum value for a char+127
SHRT_MINMinimum value for a short int−32,768
SHRT_MAXMaximum value for a short int+32,767
USHRT_MAXMaximum value for an unsigned short int+65,535
INT_MINMinimum value for an int−2,147,483,648
INT_MAXMaximum value for an int+2,147,483,647
UINT_MAXMaximum value for an unsigned int+4,294,967,295
LONG_MAXMinimum value for a long int+9,223,372,036,854,775,808
ULONG_MAXMaximum value for an unsigned long int+18,446,744,073,709,551,615

Note: Use these limits to check you data before conversion.

String conversion

Type casting is available only for numeric data type. You can not cast a composite data type. For conversion from a string to a number there are available these functions:

  • atoi(): convert a string to integer
  • atof(): convert a string to float
  • atol(): convert a string to long
  • atoll(): convert a string to long long
  • sprintf(): convert a float into a string

Note: Conversion function itoa() and itol() are not implemented in all compilers. Most of the time you use printf() or fprintf() to convert numbers into messages. Sometimes you prepare a string using spritf() 

Example:

Next article: String functions