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

C Programming Language Data Types in C Language (Article) Data Types in C Language (Program)

23337

Program:

/*
 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

This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.