Pelles C forum

C language => Expert questions => Topic started by: Logman on March 02, 2019, 09:35:37 PM

Title: printf() %llu specifier
Post by: Logman on March 02, 2019, 09:35:37 PM
I've been using Pelles 64-bit C version for over two years now and just ran into the following problem for the first time. I set up a program like this:

    unsigned long long int num = 18446744073709551615;  // largest unsigned long long int

    printf("num = %llu\n", num);

and it always converts this value to: 9223372036854775807, the largest long long int value. I assume that there is an overflow or two's complement occuring, but haven't been able to determine where or how. Ostensibly, 18446744073709551615 is an 8 byte (64-bit) value. Am I doing something wrong?

Logman
Title: Re: printf() %llu specifier
Post by: frankie on March 02, 2019, 11:16:59 PM
Hello Logman, welcom on PellesC forum.

It seems a problem with preprocessor that doesn't interpret well the value without explicit unsigned suffix, in this case 'ULL' for unsigned long long.
Adding it to the assignment solve the problem as in the snippet below:
Code: [Select]
    unsigned long long int num = 18446744073709551615ULL;  // largest unsigned long long int
    printf("num = %llu\n", num);
I can't tell you right now if this is a bug or not. I have to check specs. In any case interpreting the input as signed and truncating it should preferably trigger a warning at least.
Anyway I hope that you can solve your problem for now.
Title: Re: printf() %llu specifier
Post by: Logman on March 03, 2019, 02:47:19 PM
Yes, the 'ULL' suffix seems to work. Thank you for the input. I really needed to be able to process large unsigned long long int values in a weather simulation program I'm working on and this did the trick.

Logman
Title: Re: printf() %llu specifier
Post by: Pelle on March 03, 2019, 09:43:04 PM
To save you some typing ;), the 'U' suffix should be enough (without any suffix the constant is interpreted as signed long long).
Title: Re: printf() %llu specifier
Post by: TimoVJL on March 04, 2019, 01:08:49 PM
ISO/IEC 9899:TC2 Committee Draft — May 6, 2005 WG14/N1124
Code: [Select]
ISO/IEC 9899:TC2 Committee Draft — May 6, 2005 WG14/N1124
Suffix  Decimal Constant        Octal or Hexadecimal Constant
none    int                     int
        long int                unsigned int
        long long int           long int
                                unsigned long int
                                long long int
                                unsigned long long int

u or U  unsigned int            unsigned int
        unsigned long int       unsigned long int
        unsigned long long int  unsigned long long int

l or L  long int                long int
        long long int           unsigned long int
                                long long int
                                unsigned long long int