printf unsigned long long as hex

Discussion in 'C Programming' started by Schraalhans Keukenmeester, Jul 1, 2007.

  1. Hi,

    printf's format specifier %X only prints values of up to 8 hex digits.
    I am using GCC on Gentoo Linux. (How do I find which GCC version I have?)

    Is it possible to print values as hex up to 15 or 16 hex digits? If not,
    a workaround would be appreciated also.
    TIA
     
    Schraalhans Keukenmeester, Jul 1, 2007
    #1
    1. Advertisements

  2. gcc questions, like all implementation-specific questions, belong in an
    implementation-specific newsgroup, but only after checking the supplied
    documentation. As it happens, your documentation adequately explains
    the switches available in invoking gcc. Check it.
    #include <stdio.h>
    #include <limits.h>

    int main(void)
    {
    unsigned char uc = UCHAR_MAX;
    unsigned short us = USHRT_MAX;
    unsigned int ui = UINT_MAX;
    unsigned long ul = ULONG_MAX;
    unsigned long long ull = ULLONG_MAX;
    puts("[output, specific values are implementation-dependent.]");

    printf("UCHAR_MAX, %u (decimal), %#o (octal), %#x (hex)\n",
    uc, uc, uc);


    printf("USHRT_MAX, %u (decimal), %#o (octal), %#x (hex)\n",
    us, us, us);

    printf("UINT_MAX, %u (decimal), %#o (octal),\n %#x (hex)\n",
    ui, ui, ui);

    printf("ULONG_MAX, %lu (decimal), %#lo (octal),\n %#lx (hex)\n",
    ul, ul, ul);

    printf
    ("ULLONG_MAX, %llu (decimal),\n %#llo (octal),\n %#llx (hex)\n",
    ull, ull, ull);

    puts("[end of output]");
    return 0;
    }

    [output, specific values are implementation-dependent.]
    UCHAR_MAX, 255 (decimal), 0377 (octal), 0xff (hex)
    USHRT_MAX, 65535 (decimal), 0177777 (octal), 0xffff (hex)
    UINT_MAX, 4294967295 (decimal), 037777777777 (octal),
    0xffffffff (hex)
    ULONG_MAX, 4294967295 (decimal), 037777777777 (octal),
    0xffffffff (hex)
    ULLONG_MAX, 18446744073709551615 (decimal),
    01777777777777777777777 (octal),
    0xffffffffffffffff (hex)
    [end of output]
    Bite me.
     
    Martin Ambuhl, Jul 1, 2007
    #2
    1. Advertisements

  3. Schraalhans Keukenmeester

    Flash Gordon Guest

    Schraalhans Keukenmeester wrote, On 01/07/07 19:08:
    Then presumably that is the number of digits required to represent an
    unsigned int on your implemention.
    The correct place to ask that is your man/info pages of gnu.gcc.help,
    but I'm feeling generous, so I suggest you try 'gcc -v'.
    Use the correct format specifier for the type you are printing. 'man 3
    printf' or your textbook will give you lots of information (if you don't
    have a textbook I strongly suggest buying K&R2 (search the group for
    full details), but depending on the details yout might want %lX or %llX,
    but note that the value you pass should be of type unsigned whatever.
     
    Flash Gordon, Jul 1, 2007
    #3
  4. Questions about gcc are off-topic here. You'll have to post to
    gnu.gcc.help, or consult the extensive documentation that comes with
    gcc, to learn about "gcc --version".
    As it happens, gcc is a compiler, not a full implementation. The
    printf function is part of the runtime library, not of the compiler,
    so knowing what version of gcc you have doesn't necessarily help you
    to find out what printf support.

    The sizes of the predefined integer types can vary from one
    implementation to another; the standard merely specifies minimum
    required ranges and relationships. There are plenty of
    implementations where unsigned long is 64 bits; on such systems;
    "%016lx" will print 16 hexadecimal digits.

    In many implementations, unsigned long long is 64 bits, but that type
    was added to the C standard only in 1999, and few implementations
    fully support the C99 standard. (It's legal for unsigned long long to
    be bigger than 64 bits, but I've never heard of such an
    implementation.) The format for printing an unsigned long long value
    in 16 hexadecimal digits, with leading zeros as needed, is "016llx"
    (that's zero one six ell ell ex for those reading with fonts that
    don't distinguish between '1' and 'l'). But this will work only if
    (a) your compiler supports unsigned long long, and (b) your runtime
    library supports the format. It's likely to work on your
    implementation, but be aware that it could break on other systems.
     
    Keith Thompson, Jul 1, 2007
    #4
  5. Schraalhans Keukenmeester

    Army1987 Guest

    "%.15X" or "%.16X"
    If not,
    "00000000%X" (I hope you get the point... :) )
     
    Army1987, Jul 2, 2007
    #5
  6. Schraalhans Keukenmeester

    Army1987 Guest

    long... long...
    I had missed one "long"...

    Ignore completely what I've written above...
    "%llX"
     
    Army1987, Jul 2, 2007
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.