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 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;
} 
                        

🖥 Program 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
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

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.

🔥 Practice suggestion

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.