Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what happened when I used %d in printf() 1

Status
Not open for further replies.

chopstick

Programmer
Apr 9, 2006
8
SG
what happened when I use %d in printf() to display a long int or unsigned number? consider the code below:

#include<stdio.h>
int main(void)
{
unsigned un=40000;
printf("un=%u and not %d",un,un);
return 0;
}

and what I get is "un=40000 and not -25536", what was going on here?

thanks for help.
 
> and what I get is "un=40000 and not -25536", what was going on here?
Because at the bit level, they're both the same.
Whether it's unsigned or not depends on the interpretation.

Also, why are you still using a 16-bit compiler?

> unsigned un=40000;
Whilst this implies unsigned int un=40000;, it is better to actually say so.

> what happened when I use %d in printf() to display a long int or unsigned number
Well if you want a long, say so
Code:
long int a = 40000;
printf("%ld", a );
%d just gets you a regular size signed integer.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top