Home / Programs / Write a program to illustrate the results of type conversion between signed and unsigned long and short integer data types.
Programming Example

Write a program to illustrate the results of type conversion between signed and unsigned long and short integer data types.

👁 23,428 Views
💻 Practical Program
📘 Step by Step Learning
Write a program to illustrate the results of type conversion between signed and unsigned long and short integer data types.

Program Code

/*
 Program:  Write a program to illustrate the results of type conversion between signed and unsigned long and short integer data types. 
  
 Author: www.atnyla.com  
 
*/ 

#include "stdio.h"  
int main()
{
short int si;
long int li;
unsigned short int usi;
unsigned long int uli;

si = -10;
li = si;                                  /* sign extension - li should be -10 */
printf("si = %8hd li = %8ld\n",si,li);

usi = 40000U;                             /* usigned decimal constant */
uli = usi;                                /* zero extension - uli should be 40000 */
printf("usi = %8hu uli = %8lu\n",usi,uli);

uli = 0xabcdef12;                         /* sets most bits ! */
usi = uli;                                /* will truncate-discard more sigficant bits */
printf("usi = %8hx uli = %8lx\n",usi,uli);

si = usi;                                 /* preserves bit pattern */
printf("si = %8hd usi = %8hu\n",si,usi);

si = -10;
usi = si;                                 /* preserves bit pattern */
printf("si = %8hd usi = %8hu\n",si,usi);

return 0;
} 

Output

si =      -10 li =      -10
usi =    40000 uli =    40000
usi =     ef12 uli = abcdef12
si =    -4334 usi =    61202
si =      -10 usi =    65526
Press any key to continue . . .

Explanation

None

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.