Sage-Code Laboratory
index<--

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:

#include <stdio.h>
int main()
{
    int a = 10;
    int b = 20;
    double c,d;
    // integer division
    c = a / b;
    printf("c = %lf\n", c); // 0.000000
    // float division
    d = (double) a / b;
    printf("d = %lf\n", d); // 0.500000
    return 0;
}

Output:

c = 0.000000
d = 0.500000

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:

#include <stdio.h>
int main()
{
    int a = 0;
    float b = 1.5;
    //explicit conversion
    a = (int)b;
    printf("a = %d\n", a);
    //implicit conversion
    b = a;
    printf("a = %f\n", b);
    return 0;
}

Output:

a = 1
a = 1.000000 

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.

Name Description Typical value
CHAR_BIT Number of bits in a char 8
SCHAR_MIN Minimum value for a signed char −128
SCHAR_MAX Maximum value for a signed char +127
UCHAR_MAX Maximum value for an unsigned char +255
CHAR_MIN Minimum value for a char −128
CHAR_MAX Maximum value for a char +127
SHRT_MIN Minimum value for a short int −32,768
SHRT_MAX Maximum value for a short int +32,767
USHRT_MAX Maximum value for an unsigned short int +65,535
INT_MIN Minimum value for an int −2,147,483,648
INT_MAX Maximum value for an int +2,147,483,647
UINT_MAX Maximum value for an unsigned int +4,294,967,295
LONG_MAX Minimum value for a long int +9,223,372,036,854,775,808
ULONG_MAX Maximum 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:

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:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char a[10];
    int i;
    i = atoi("12345"); //convert string to integer
    sprintf(a,"%d",i); //convert integer to string
    printf("i = %d\n",i);
    printf("a = %s\n",a);
    return 0;
}

Read next: String Functions