Thread: 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
|
|
From: Ioannis V. <iv...@at...> - 2003-04-06 16:41:53
|
> -----Original Message----- > From: Ioannis Vranos [mailto:iv...@at...] > Sent: Sunday, April 06, 2003 7:39 PM > To: 'Dev-C++ -Users-List' > Subject: RE: [Dev-C++] Can I handle 64 bits with unsigned > long long int ? > > > Of course it does. In fact I compiled that code with: > > > C:\c>gcc temp.c -o temp > > C:\c>temp > Maximum value of unsigned long long: 4294967295 > > > C:\c> But it doesn't compile with -std=c99. MINGW is a bit messed up I guess. However it compiles C99 by default as it seems. 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 |
|
From: Daniel K. O. <dan...@ya...> - 2003-04-06 14:10:37
|
> 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();
> }
MinGW uses MSVCRT.DLL; the printf doesn't know what is this %llu, and
there's nothing you can do about it. More details here:
http://sourceforge.net/tracker/index.php?func=detail&aid=661165&group_id=243
5&atid=102435
Use "%I64u" for now.
Daniel K. O.
|
|
From: Ioannis V. <iv...@at...> - 2003-04-06 16:39:38
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Daniel K. O.
> Sent: Sunday, April 06, 2003 5:11 PM
> To: 'Dev-C++ -Users-List'
> Subject: RES: [Dev-C++] Can I handle 64 bits with unsigned
> long long int ?
>
>
>
>
> > 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();
> > }
>
>
> MinGW uses MSVCRT.DLL; the printf doesn't know what is this %llu, and
> there's nothing you can do about it.
Of course it does. In fact I compiled that code with:
C:\c>gcc temp.c -o temp
C:\c>temp
Maximum value of unsigned long long: 4294967295
C:\c>
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
|
|
From: Daniel K. O. <dan...@ya...> - 2003-04-06 17:05:24
|
> Of course it does. In fact I compiled that code with: > > > C:\c>gcc temp.c -o temp > > C:\c>temp > Maximum value of unsigned long long: 4294967295 > But it should be 18446744073709551615 (2^64 - 1). What I mean is that the %llu doesn't handle the 64-bit integer, only the first word. Daniel K. O. |
|
From: Ioannis V. <iv...@at...> - 2003-04-06 18:10:34
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Daniel K. O.
> Sent: Sunday, April 06, 2003 8:05 PM
> To: 'Dev-C++ -Users-List'
> Subject: RES: [Dev-C++] Can I handle 64 bits with unsigned
> long long int ?
>
>
>
>
>
>
>
> > Of course it does. In fact I compiled that code with:
> >
> >
> > C:\c>gcc temp.c -o temp
> >
> > C:\c>temp
> > Maximum value of unsigned long long: 4294967295
> >
>
>
> But it should be 18446744073709551615 (2^64 - 1).
> What I mean is that the %llu doesn't handle the 64-bit
> integer, only the
> first word.
Obviously MINGW's unsigned long long isn't 64 bit or there is serious
compiler bug. An equivalent code:
#include <stdio.h>
int main(void)
{
printf ("Maximum value of unsigned long long: %llu\n", (unsigned long
long)-1);
getchar();
}
It still produces:
C:\c>temp
Maximum value of unsigned long long: 4294967295
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
|
|
From: Daniel K. O. <dan...@ya...> - 2003-04-06 18:35:34
|
> Obviously MINGW's unsigned long long isn't 64 bit or there is serious
> compiler bug. An equivalent code:
Ok, let's try again... You have:
unsigned long long i = ULLONG_MAX;
So you have i = 0xFFFFFFFFFFFFFFFF;
And sizeof (i) = 8;
Now, you printf("%llu", i); printf provided on MSVCRT.DLL should "eat" 8
bytes from the stack (0xFFFFFFFFFFFFFFFF), to print the unsigned long long.
But it doesn't. I thinks that %llu is for a 4 bytes int, and only prints the
first word of the unsigned long long (you can put another "%llu", if you
want to see the other half :P ). This bug is not on MinGW, but on
MSVCRT.DLL, where the printf is implemented. While MinGW keeps using the
runtime lib from MS, or MS doesn't update its C Runtime library, you'll have
to use "%I64d", "%I64u", "%I64x", etc.
Again, here is the link about this issue:
"printf function does not support long long"
[all-in-one-line]
http://sf.net/tracker/?func=detail&aid=661165&group_id=2435&atid=102435
[/all-in-one-line]
Daniel K. O.
|
|
From: Ioannis V. <iv...@at...> - 2003-04-06 19:05:24
|
> -----Original Message-----
> From: dev...@li...=20
> [mailto:dev...@li...] On Behalf=20
> Of Daniel K. O.
> Sent: Sunday, April 06, 2003 9:36 PM
> To: 'Dev-C++ -Users-List'
> Subject: RES: [Dev-C++] Can I handle 64 bits with unsigned=20
> long long int ?
>=20
>=20
>=20
>=20
>=20
> > Obviously MINGW's unsigned long long isn't 64 bit or=20
> there is serious
> > compiler bug. An equivalent code:
>=20
>=20
>=20
> Ok, let's try again... You have:
>=20
> unsigned long long i =3D ULLONG_MAX;
>=20
> So you have i =3D 0xFFFFFFFFFFFFFFFF;
Why?
> And sizeof (i) =3D 8;
Irrelevant. It may have padding bits.
> Now, you printf("%llu", i); printf provided on MSVCRT.DLL=20
> should "eat" 8
> bytes from the stack (0xFFFFFFFFFFFFFFFF), to print the=20
> unsigned long long.
> But it doesn't. I thinks that %llu is for a 4 bytes int, and=20
> only prints the
> first word of the unsigned long long (you can put another=20
> "%llu", if you
> want to see the other half :P=20
%llu is for the type unsigned long long. Failure to print the whole =
range of
the unsigned long long values would be a compiler defect.
> ). This bug is not on MinGW, but on
> MSVCRT.DLL, where the printf is implemented. While MinGW=20
> keeps using the
> runtime lib from MS, or MS doesn't update its C Runtime=20
> library, you'll have
> to use "%I64d", "%I64u", "%I64x", etc.
I have no idea of the compiler internals. It may be a bug of the dll you
mention, I do not know. Regarding the specifiers you are mentioning =
above,
as far as I know are not part of the standard.
>=20
>=20
> Again, here is the link about this issue:
>=20
> "printf function does not support long long"
> [all-in-one-line]
> http://sf.net/tracker/?func=3Ddetail&aid=3D661165&group_id=3D2435&at
id=3D102435
[/all-in-one-line]
Ok. Another portable approach would be the use of size_t, e.g.:
#include <stdio.h>
#include <stddef.h>
int main(void)
{
printf ("Maximum value of size_t: %sz\n", (size_t)-1);
getchar();
}
Unfortunately it crashes to me. It seems MINGW has a long way to go
regarding C99 stability.
Also SIZE_MAX seems to not exist in MINGW implementation...
Ioannis Vranos
=20
* 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
|