C: convert signed unsigned

Actually, I am (possibly) a "simple" problem. Therefore, I do not know how to distinguish a signed integer with an unsigned integer.

My code is:

signed int entry = 0; printf("Decimal Number : "); scanf("%d", &entry); unsigned int uEntry= (unsigned int) entry; printf("Unsigned : %d\n", uEntry); 

If I send the unsigned value to the console (see my last line of code), I always return a signed integer.

Can you help me?

Thanks a lot!

Regards, About

0
source share
2 answers
 printf("Unsigned : %u\n", uEntry); // ^^ 

You must use the %u specifier to tell printf the runtime that uEntry is an unsigned int . If you use %d , the printf function expects an int , thus re-interpreting your input back to the signed value.

+9
source

Add these two lines to the end of your code and you will understand what is happening.

 printf("entry: signed = %d, unsigned = %u, hex = 0x%x\n", entry, entry entry); printf("uEntry: signed = %d, unsigned = %u, hex = 0x%x\n", uEntry,uEntry,uEntry); 
+1
source

Source: https://habr.com/ru/post/1340921/


All Articles