"long long int" and "unsigned long long int"

Write here if you have problems with your C source code

Moderator: Board moderators

Post Reply
renatov
New poster
Posts: 20
Joined: Fri Sep 21, 2012 6:33 am

"long long int" and "unsigned long long int"

Post by renatov »

Question: how to input/output "long long int" and "unsigned long long int" data in a portable way using C language?


I'm currently compiling in Windows XP with Mingw and I'm trying to use %lld and %llu. Here are the code and its output:

Code: Select all

#include <stdio.h>
#include <limits.h>

int main()
{

    /* header */
    fprintf(stdout, "%22s | %8s | %7s | %4s | %19s | %20s\n", "DATA TYPE", "printf()", "scanf()", "BITS", "MIN", "MAX");

    /* long long int */
    fprintf(stdout, "%22s | %8s | %7s | %4u | %19lld | %20lld | %8d\n", "long long int", "lld", "lld", sizeof(long long int) * CHAR_BIT, LLONG_MIN, LLONG_MAX);

    /* unsigned long long int */
    fprintf(stdout, "%22s | %8s | %7s | %4u | %19llu | %20llu | %8d\n", "unsigned long long int", "llu", "llu", sizeof(unsigned long long int) * CHAR_BIT, 0, ULLONG_MAX);

    return 0;
}
Output:

Code: Select all

             DATA TYPE | printf() | scanf() | BITS |                 MIN |               MAX
         long long int |      lld |     lld |   64 |                   0 |          -2147483648
unsigned long long int |      llu |     llu |   64 |                   0 |           4294967295
As you can see, %lld and %llu don't work properly. Any ideas?
renatov
New poster
Posts: 20
Joined: Fri Sep 21, 2012 6:33 am

Re: "long long int" and "unsigned long long int"

Post by renatov »

I think in Windows the specifier for long long int is %I64d and for unsigned long long int is %I64u. This is the code and its output in Windows:


Code: Select all

#include <stdio.h>
#include <limits.h>

int main()
{

    /* header */
    fprintf(stdout, "%22s | %8s | %7s | %4s | %19s | %20s\n", "DATA TYPE", "printf()", "scanf()", "BITS", "MIN", "MAX");

    /* long long int */
    fprintf(stdout, "%22s | %8s | %7s | %4u | %19I64d | %20I64d | %8d\n", "long long int", "I64d", "I64d", sizeof(long long int) * CHAR_BIT, LLONG_MIN, LLONG_MAX);

    /* unsigned long long int */
    fprintf(stdout, "%22s | %8s | %7s | %4u | %19I64u | %20I64u | %8d\n", "unsigned long long int", "I64u", "I64u", sizeof(unsigned long long int) * CHAR_BIT, 0ULL, (unsigned long long) ULLONG_MAX);

    return 0;
}
Output:

Code: Select all

             DATA TYPE | printf() | scanf() | BITS |                  MIN |               MAX
         long long int |     I64d |    I64d |   64 | -9223372036854775808 |  9223372036854775807
unsigned long long int |     I64u |    I64u |   64 |                    0 | 18446744073709551615
Post Reply

Return to “C”