3.10 – Widening and Narrowing of primitive conversions

by subbu on September 10, 2013

Widening and narrowing primitive conversions:

In previous sessions we have discoursed the need of type conversion, typecast operator and conversions with printf(). Here I would like to discourse some other issues like narrowing and widening conversions.

Type casting or type conversion is a way of converting a variable, constant or an expression from one type to other. There are two important issues to address in type conversion called

  • Widening primitive conversion
  • Narrowing primitive conversion

Widening primitive conversion:

It is converting a type that takes less size to the type that takes more size. In C language it is automatically performed without loosing information. It is also called auto conversion or implicit conversion.
Widening primitive conversion

  • char → int, long, float, double, long double
  • int → long, float, double, long double
  • long → float, double, long double
  • float → double, long double
  • double → long double

Example:

#include<stdio.h>
int main()
{
int ch='a';/* ASCII value of 'a' is 97*/
float dh='A';/* ASCII value of 'A' is 65*/
printf("%d\n%f",ch,dh);
return 0;
}

Output:
97
65.000000

Narrowing primitive conversion:

It is converting a type that takes more size to the type that takes less size, results loosing of information. If the r-value is within the range of l-value then information is properly assigned otherwise lower byte values are assigned.

Narrowing primitive conversions

  • long → char, int
  • float → char, int, long
  • double → char, int, long, float
  • long double → char, int, long, float, double

Example:

#include<stdio.h>
int main()
{
float x=97.45;
int y=x;/* truncates to 97*/
char ch=x;/* truncates to 97 */
printf("%d\n%c",y,ch);
return 0;
}

Output:
97
a

Example:

#include<stdio.h>
int main()
{
long int x=32769;
char ch=x;/*prints equivalent character of lower byte*/
printf("%c",ch);
}

Output:

Binary equal to 32769 is 1000 0000 0000 0001, equal character to lower byte 0000 0001 (ASCII 1) is ☺

Example:

#include<stdio.h>
int main()
{
long int x=569876523;
short y=x;
printf("%d",y);
return 0;
}

Output:
-24533

equal binary to 569876523 [0010 0001 1111 0111 1010 0000 0010 1011] is assigned to 4bytes of x.
When we assign 4bytes x to 2bytes y then only last two bytes are copied [1010 0000 0010 1011] While printing y, as the first bit is 1, considers as -ve and reverts 2’compliment [1010 0000 0010 1010], reverts 1’s compliment [0101 1111 1101 1010] and the decimal equal to which is printed with -ve sign as -24533.

Example:

#include<stdio.h>
int main()
{
double x=5.6e50;
float y=x;/* results overflow*/
printf("%f",y);
return 0;
}

Output:
+INF

Example:

#include<stdio.h>
int main()
{
unsigned short x=65500;
short y=x;
printf("%d",y);
return 0;
}

Output:
-36

Here the binary of x (1111 1111 1101 1100) is assigned to y, as the first bit of y is 1, treats it as negative, reverts 2’s complement (1111 1111 1101 1011) , reverts 1’s compliment (0000 0000 0010 0100) and prints decimal equal with -ve sign that is -36

Example:

#include<stdio.h>
int main()
{
short x=-32768;
unsigned short y=x;
printf("%u",y);
return 0;
}

Output:
52080

As we assign negative value, 2’s compliment of 32768 (1100 1011 0111 0000) is assigned to x and that is assigned to y.
Now the decimal equivalent of 1100 1011 0111 0000, 52080 is printed as the output

Previous post:

Next post: