954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

casting long int to unsigned int

unsigned int is 4 bytes in the system i'm programming. i printed sizeof(unsigned int) and it said 4.

int main(int argc, char * argv[])
{
        unsigned int a = 0;
        long int b = 2607503366;
/* the b data type must strictly be a long int */ 
        a = (unsigned int) b;
        printf("%d\n",a);
        return 0;
}

it prints out;

-1687463930


but it must print the same thing. what am i doing wrong?

thanks.

asilter
Junior Poster in Training
60 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Because you used a signed type, and your unsigned value is outside the permitted range of your machine.

You also used the wrong printf format, try %lu instead.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You