RE: [Dev-C++] Can I handle 64 bits with unsigned long long int ?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <iv...@em...> - 2003-04-06 09:37:06
|
> -----Original Message-----
> From: Ioannis Vranos [mailto:iv...@em...]
> Sent: Sunday, April 06, 2003 12:01 PM
> To: 'Dev-C++ -Users-List'
> Subject: RE: [Dev-C++] Can I handle 64 bits with unsigned
> long long int ?
>
>
> Question about C99. That is a first! See below:
>
>
>
> > -----Original Message-----
> > From: dev...@li...
> > [mailto:dev...@li...] On Behalf
> > Of Roberto Ricardi
> > Sent: Sunday, April 06, 2003 2:11 AM
> > To: 'Dev-C++ -Users-List'
> > Subject: [Dev-C++] Can I handle 64 bits with unsigned long
> long int ?
> >
> >
> > Hi all,
> >
> > Can I handle 64 bits numbers with unsigned long long ?
> >
> > #include <stdio.h>
> > #include <stdlib.h>
> > #include <conio.h>
> >
> > unsigned long long int D0,D1 ;
> >
> > int main(int argc, char *argv[])
> > {
> > D0 = 0;
> > D1 = 0xffffffffffff; /* I put first unsigned long int, compiler
> > alerts about truncated number...) */
> > gotoxy(1,9);printf ("Num hexa : ");
> > scanf ("%x",&D0); /*can handle whatever up to 32 bits*/
> > gotoxy(1,11);printf ("Dec0: %lu\nDec1: %lu\n\n", D0, D1);
> >
> > getchar(); getchar();
> > return 0;
> > }
> > /*
> > Results:
> >
> > Dec0 : 4294967295 mean ok if I put just up to 0xffffffff 32bits
> > Dec1 : 0 ??
> > */
> >
> > Why is ever truncated in 32 bits, how can handle long long to
> > get up to
> > 64 bits numbers ?
>
>
> unsigned long long is not guaranteed to be 64-bit. If you
> want an unsigned integral type to be at least 64-bit, you have to do:
>
>
> #include <stdint.h>
>
> // ...
>
> uint_least64_t D0, D1;
>
> // ...
>
>
>
> which is a typedef of the closest to 64-bit type provided by
> platform (closest from above, that is if unsigned long is 32
> bit and unsigned long long is 128 bit for example, the
> typedef will be of unsigned long long).
>
>
> I read it straight from the standard. I do not know whether
> MINGW has implemented it (and as far as I know these
> facilities are "optional", if the platform doesn't support
> >=64 bit, will not have the above type naturally).
>
>
> By the way, have you got any book regarding C99? If yes, can
> you please tell me which?
I checked and saw that MINGW's long long types are 64-bit. You made a
mistake in the format specifier of printf(). Thus an example code of
printf() printing an unsigned long long value is:
#include <stdio.h>
#include <limits.h>
int main(void)
{
printf ("Maximum value of unsigned long long: %llu\n", ULLONG_MAX);
getchar();
}
For the above to compile you must be careful with the compiler switches.
Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL: http://run.to/noicys
Anti-racism movement: http://www.positive-youthfoundation.org
Iraq Peace Pledge: http://www.peacepledge.org
|