gnu.gcc.help
[Top] [All Lists]

Re: What have I done wrong?

Subject: Re: What have I done wrong?
From: Ben Bacarisse <ben.usenet@xxxxxxxxx>
Date: Sat, 26 Jul 2008 12:11:24 +0100
Newsgroups: gnu.gcc.help

Pilcrow <pilcrow@xxxxxxx> writes:

> On Sat, 26 Jul 2008 02:17:58 +0100, Ben Bacarisse <ben.usenet@xxxxxxxxx>
> wrote:
>
>>Pilcrow <pilcrow@xxxxxxx> writes:
>>
>>> I have a fair understanding of perl, now I'm trying to learn C.
>>> I'm trying to use long long, with trouble.  Here is demo code.
>>> I'm using the gcc that came with strawberry perl.
>>> Same output if I say %lld instead of %lli.  
>>> Grateful for any help I can get.
>>>
>>> // demo.c
>>> #include <stdio.h>
>>> #include <limits.h>
>>> int main()
>>> {
>>>     printf("%d\n",sizeof(long long));
>>>     printf("%llu \n",ULLONG_MAX);
>>>     printf("%lli \n",LLONG_MIN);
>>>     printf("%lli \n",LLONG_MAX);
>>>     printf("%lli to %lli \n",LLONG_MIN,LLONG_MAX);
>>> }
>>>
>>> Here is the output.
>>>
>>> C:\>gcc --version
>>> gcc (GCC) 3.4.5 (mingw special)
>>> Copyright (C) 2004 Free Software Foundation, Inc.
>>> This is free software; see the source for copying conditions.  There is
>>> NO
>>> warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
>>> PURPOSE.
>>>
>>>
>>> C:\>gcc -std=c99, -Wall demo.c
>>>
>>> C:\>a
>>> 8
>>> 4294967295
>>> 0
>>> -1
>>> 0 to -2147483648
>>>
>>> C:\>
>>
>>That looks like a C99 compiler using a printf that is not long long
>>aware.  Out of interest try this:
>>
>>#include <stdio.h>
>>#include <limits.h>
>>
>>int main(void)
>>{
>>     char b[100];
>>     snprintf(b, sizeof b, "%llu\n", ULLONG_MAX);
>>     printf("%s", b);
>>     printf("%d\n",sizeof(long long));
>>     printf("%llu\n",ULLONG_MAX);
>>}
>>
>>On my (later) version, I get:
>>
>>18446744073709551615
>>8
>>4294967295
>>
>>I think this is well-known issue with MS C library, but I may be
>>misremembering that.  (Searches...) Ah, yes it is well known:
>>
>>http://www.mingw.org/MinGWiki/index.php/long%20long
>
> Thank you so much.
> I have to say $I64u instead of %llu in both cases:
>
> #include <stdio.h>
> #include <limits.h>
>
> int main(void)
> {
>      char b[100];
>      snprintf(b, sizeof b, "%I64u\n", ULLONG_MAX);
>      printf("%s", b);
>      printf("%d\n",sizeof(long long));
>      printf("max unsigned long long: %I64u\n",ULLONG_MAX);
>      printf("signed long long min: %I64d\n",LLONG_MIN);
>      printf("signed long long max: %I64d\n",LLONG_MAX);
> }
>
> which gives me:
>
> 18446744073709551615
> 8
> max unsigned long long: 18446744073709551615
> signed long long min: -9223372036854775808
> signed long long max: 9223372036854775807
>
> I've definitely been spoiled by perl's great documentation.
> Of course, m$ is guilty of obfuscation. They could have stuck
> with the standard.

Yes it is a shame they have not done it the C99 way (it must be
trivial since the functionality is there).

A tip:  If you have literal formats and want to this conditionally you
can write:

  printf("N = %"LL_MOD"d\n", ll_var);

and

#define LL_MOD "I64"

when using mingw.

-- 
Ben.

<Prev in Thread] Current Thread [Next in Thread>