TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Jay B
from: Jasen Betts
date: 2004-04-25 12:53:50
subject: [C] typecasting

Hi Jay.

24-Apr-04 10:46:12, Jay B wrote to All


 JB> From: "Jay B" 

 JB> Hey, I got the following code:

 JB> int main(void) {
 JB>     return 0;
 JB>     int i = -33;

 JB>     printf("%d\n", i);
 JB>     printf("%d\n", (unsigned int)i);
 JB> }

 JB> I get -33 for both, why? Since I'm typecasting the second as an unsigned
 JB> int,
 JB> shouldn't it get rid of the negative sign

no, printf doesn't understand the type of the parameters you give it.
you have to tell it.

try this

     int i = -33;
     printf("%u\n", (unsigned) i);

here the signed value is printed asif it was unsigned.

     printf("%u\n", i);

here you'll probably get the same result.

It'll be some large positive number dependant upon the negative input
number and the design of the processor.

 -=> Bye <=-
---
* Origin: I'm pink, therefore I'm SPAM. (3:640/1042)
SEEN-BY: 633/267 270
@PATH: 640/1042 531 954 774/605 123/500 106/2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.