/*
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;
}
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 . . .
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.