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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Range of data type "unsigned int"

Status
Not open for further replies.

Lauro

Programmer
Jul 29, 2001
10
BR
Well,

First of all, I'm a newbie user.
I need your help!!!
I use a Borland C++ compiler version 3.0. So the range of the unsigned int data type is from 0 up to 65,000. Well,
I tried store the value 40,030 into an variable that it used this data type but, when I runned the program I received the following value: -25,216.
What's happened??
See the source code below:

#include <stdio.h>
#include <conio.h>

unsigned int fatorial(int N);

void main(void)
{
int ValInt;
unsigned int ProdFator;
clrscr();
printf(&quot;Calcule fatorial de &quot;);
scanf(&quot;%d&quot;,&ValInt);
ProdFator = fatorial(ValInt);
printf(&quot;%d! ‚ igual a %d&quot;, ValInt, ProdFator);
}

unsigned int fatorial(int N)
{
int contador;
unsigned int ValFator = 1;

for(contador = 1; contador <= N; ++contador) ValFator = contador * ValFator;
return(ValFator);
}
 
Hello,
this is normal. You are displaying a signed decimal value (%d) instead of an unsigned decimal value (%u).
Change this
printf(&quot;%d! ‚ igual a %d&quot;, ValInt, ProdFator);
into
printf(&quot;%d! ‚ igual a %u&quot;, ValInt, ProdFator);
Wouter Dijkslag

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top