What format specifier, modifier, do I need for a 64 bit unsigned int - c++

This is a discussion on What format specifier, modifier, do I need for a 64 bit unsigned int - c++ ; Hi, I know that %u is for unsigned integers, but, if memory serves me, that's only for 32 bit integers. What modifier would I use just before the 'u'? For example, (again if memory serves), to display an object of ...

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11

What format specifier, modifier, do I need for a 64 bit unsigned int

  1. Default What format specifier, modifier, do I need for a 64 bit unsigned int

    Hi,

    I know that %u is for unsigned integers, but, if memory serves me,
    that's only for 32 bit integers. What modifier would I use just
    before the 'u'? For example, (again if memory serves), to display an
    object of type long long, I would use "%ll", correct? So, what's
    necessary for an unsigned 64 bit integer?

    You know, I don't know if it would make any difference but I'm using a
    64 bit compiler. I'm wondering if, in this case, it matters at all.
    Since the word size of the compiler is 64 bits, would I simply use %u
    and call it good?

    Andy

  2. Default Re: What format specifier, modifier, do I need for a 64 bit unsigned int

    Andrew Falanga said:

    > Hi,
    >
    > I know that %u is for unsigned integers, but, if memory serves me,
    > that's only for 32 bit integers.


    Not so. It is for unsigned ints.

    > What modifier would I use just
    > before the 'u'? For example, (again if memory serves), to display an
    > object of type long long, I would use "%ll", correct?


    Nearly. To display the /value/ of an object of type long long, in a decimal
    representation, you would use "%lld".

    > So, what's
    > necessary for an unsigned 64 bit integer?


    It depends on the type. If it's a short int, use "%hu". If it's an int, use
    "%u". If it's a long int, use "%lu". And if it's a long long int (always
    assuming your platform supports them, of course), use "%llu". C imposes no
    upper limit on the width of the basic integral data types. Even char is
    allowed to be 64 bits wide - or more!

    > You know, I don't know if it would make any difference but I'm using a
    > 64 bit compiler.


    C is C. It doesn't matter how many bits your compiler has, as long as it
    conforms to the language spec.

    > I'm wondering if, in this case, it matters at all.
    > Since the word size of the compiler is 64 bits, would I simply use %u
    > and call it good?


    No, your choice should be based on the specific type, not on bit-count.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

  3. Default Re: What format specifier, modifier, do I need for a 64 bit unsignedint

    On May 27, 9:31 am, Richard Heathfield <r...@see.sig.invalid> wrote:
    > Andrew Falanga said:
    >
    > > Hi,

    >
    > > I know that %u is for unsigned integers, but, if memory serves me,
    > > that's only for 32 bit integers.

    >
    > Not so. It is for unsigned ints.
    >
    > > What modifier would I use just
    > > before the 'u'? For example, (again if memory serves), to display an
    > > object of type long long, I would use "%ll", correct?

    >
    > Nearly. To display the /value/ of an object of type long long, in a decimal
    > representation, you would use "%lld".
    >
    > > So, what's
    > > necessary for an unsigned 64 bit integer?

    >
    > It depends on the type. If it's a short int, use "%hu". If it's an int, use
    > "%u". If it's a long int, use "%lu". And if it's a long long int (always
    > assuming your platform supports them, of course), use "%llu". C imposes no
    > upper limit on the width of the basic integral data types. Even char is
    > allowed to be 64 bits wide - or more!
    >
    > > You know, I don't know if it would make any difference but I'm using a
    > > 64 bit compiler.

    >
    > C is C. It doesn't matter how many bits your compiler has, as long as it
    > conforms to the language spec.
    >
    > > I'm wondering if, in this case, it matters at all.
    > > Since the word size of the compiler is 64 bits, would I simply use %u
    > > and call it good?

    >
    > No, your choice should be based on the specific type, not on bit-count.
    >
    > --
    > Richard Heathfield <http://www.cpax.org.uk>
    > Email: -http://www. +rjh@
    > Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    > "Usenet is a strange place" - dmr 29 July 1999


    Thank you. To be honest, it's been a while since I've had to use
    format specifiers. Most of the code I've written lately I've been
    able to use the C++ I/O objects. Although this is C++, I'm coming
    into the project late and don't quite know the full extent of the
    environment. I don't know why, but we have to use format specifiers
    for output. Thanks again.

    Andy

  4. Default Re: What format specifier, modifier, do I need for a 64 bit unsigned int

    On Tue, 27 May 2008 08:45:29 -0700 (PDT), Andrew Falanga
    <af300wsm@gmail.com> wrote:

    >On May 27, 9:31 am, Richard Heathfield <r...@see.sig.invalid> wrote:
    >> Andrew Falanga said:
    >>
    >> > Hi,

    >>
    >> > I know that %u is for unsigned integers, but, if memory serves me,
    >> > that's only for 32 bit integers.

    >>
    >> Not so. It is for unsigned ints.
    >>
    >> > What modifier would I use just
    >> > before the 'u'? For example, (again if memory serves), to display an
    >> > object of type long long, I would use "%ll", correct?

    >>
    >> Nearly. To display the /value/ of an object of type long long, in a decimal
    >> representation, you would use "%lld".
    >>
    >> > So, what's
    >> > necessary for an unsigned 64 bit integer?

    >>
    >> It depends on the type. If it's a short int, use "%hu". If it's an int, use
    >> "%u". If it's a long int, use "%lu". And if it's a long long int (always
    >> assuming your platform supports them, of course), use "%llu". C imposes no
    >> upper limit on the width of the basic integral data types. Even char is
    >> allowed to be 64 bits wide - or more!
    >>
    >> > You know, I don't know if it would make any difference but I'm using a
    >> > 64 bit compiler.

    >>
    >> C is C. It doesn't matter how many bits your compiler has, as long as it
    >> conforms to the language spec.
    >>
    >> > I'm wondering if, in this case, it matters at all.
    >> > Since the word size of the compiler is 64 bits, would I simply use %u
    >> > and call it good?

    >>
    >> No, your choice should be based on the specific type, not on bit-count.
    >>
    >> --
    >> Richard Heathfield <http://www.cpax.org.uk>
    >> Email: -http://www. +rjh@
    >> Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    >> "Usenet is a strange place" - dmr 29 July 1999

    >
    >Thank you. To be honest, it's been a while since I've had to use
    >format specifiers. Most of the code I've written lately I've been
    >able to use the C++ I/O objects. Although this is C++, I'm coming
    >into the project late and don't quite know the full extent of the
    >environment. I don't know why, but we have to use format specifiers
    >for output. Thanks again.


    I have no idea if C++ adopted the exact width integer types of C99 but
    if your system supports int64_t and uint64_t (the only types
    guaranteed to be exactly 64 bits) then it should support the
    corresponding formats (PRId64 and PRIu64).


    Remove del for email

  5. Default Re: What format specifier, modifier, do I need for a 64 bit unsignedint

    On May 27, 7:58 pm, Barry Schwarz <schwa...@dqel.com> wrote:
    > On Tue, 27 May 2008 08:45:29 -0700 (PDT), Andrew Falanga
    >
    >
    >
    > <af300...@gmail.com> wrote:
    > >On May 27, 9:31 am, Richard Heathfield <r...@see.sig.invalid> wrote:
    > >> Andrew Falanga said:

    >
    > >> > Hi,

    >
    > >> > I know that %u is for unsigned integers, but, if memory serves me,
    > >> > that's only for 32 bit integers.

    >
    > >> Not so. It is for unsigned ints.

    >
    > >> > What modifier would I use just
    > >> > before the 'u'? For example, (again if memory serves), to display an
    > >> > object of type long long, I would use "%ll", correct?

    >
    > >> Nearly. To display the /value/ of an object of type long long, in a decimal
    > >> representation, you would use "%lld".

    >
    > >> > So, what's
    > >> > necessary for an unsigned 64 bit integer?

    >
    > >> It depends on the type. If it's a short int, use "%hu". If it's an int, use
    > >> "%u". If it's a long int, use "%lu". And if it's a long long int (always
    > >> assuming your platform supports them, of course), use "%llu". C imposes no
    > >> upper limit on the width of the basic integral data types. Even char is
    > >> allowed to be 64 bits wide - or more!

    >
    > >> > You know, I don't know if it would make any difference but I'm using a
    > >> > 64 bit compiler.

    >
    > >> C is C. It doesn't matter how many bits your compiler has, as long as it
    > >> conforms to the language spec.

    >
    > >> > I'm wondering if, in this case, it matters at all.
    > >> > Since the word size of the compiler is 64 bits, would I simply use %u
    > >> > and call it good?

    >
    > >> No, your choice should be based on the specific type, not on bit-count.

    >
    > >> --
    > >> Richard Heathfield <http://www.cpax.org.uk>
    > >> Email: -http://www. +rjh@
    > >> Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    > >> "Usenet is a strange place" - dmr 29 July 1999

    >
    > >Thank you. To be honest, it's been a while since I've had to use
    > >format specifiers. Most of the code I've written lately I've been
    > >able to use the C++ I/O objects. Although this is C++, I'm coming
    > >into the project late and don't quite know the full extent of the
    > >environment. I don't know why, but we have to use format specifiers
    > >for output. Thanks again.

    >
    > I have no idea if C++ adopted the exact width integer types of C99 but
    > if your system supports int64_t and uint64_t (the only types
    > guaranteed to be exactly 64 bits) then it should support the
    > corresponding formats (PRId64 and PRIu64).
    >
    > Remove del for email


    Thanks. I learned something interesting. Using %lu produced nothing
    but a '?' in the output as did using %u. The only thing that works is
    %ld, but this would be for a signed in, no?

    Andy

  6. Default Re: What format specifier, modifier, do I need for a 64 bit unsigned int

    Andrew Falanga <af300wsm@gmail.com> writes:

    [ questions and answers about printf formats deleted ]

    > Thanks. I learned something interesting. Using %lu produced nothing
    > but a '?' in the output as did using %u. The only thing that works is
    > %ld, but this would be for a signed in, no?


    "%ld" is for signed long int (also known as signed long, long int, or
    long). "%lu" is for unsigned long int.

    If "%ld" works, I'd be very surprised if "%lu" produces just a '?'.
    Can you show us some actual code that exhibits this behavior?

    Note: By "actual code" I mean a small, self-contained, compilable
    program that we can try outselves without modification.
    Copy-and-paste your actual code; don't re-type it.

    --
    Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

  7. Default Re: What format specifier, modifier, do I need for a 64 bit unsignedint

    On May 28, 10:33 am, Keith Thompson <ks...@mib.org> wrote:
    > Andrew Falanga <af300...@gmail.com> writes:
    >
    > [ questions and answers about printf formats deleted ]
    >
    > > Thanks. I learned something interesting. Using %lu produced nothing
    > > but a '?' in the output as did using %u. The only thing that works is
    > > %ld, but this would be for a signed in, no?

    >
    > "%ld" is for signed long int (also known as signed long, long int, or
    > long). "%lu" is for unsigned long int.
    >
    > If "%ld" works, I'd be very surprised if "%lu" produces just a '?'.
    > Can you show us some actual code that exhibits this behavior?
    >
    > Note: By "actual code" I mean a small, self-contained, compilable
    > program that we can try outselves without modification.
    > Copy-and-paste your actual code; don't re-type it.
    >
    > --
    > Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
    > Nokia
    > "We must do something. This is something. Therefore, we must do this."
    > -- Antony Jay and Jonathan Lynn, "Yes Minister"


    I wished I could give you what you're asking for here but in this case
    I can't. Besides, I'm so new to the build environment that, to be
    honest if I could copy/paste, it wouldn't do much good. Nowhere do I
    see people using printf() or other standard calls. It's all things
    like:

    Print()
    UINT64

    and so forth. Since both of these, on the surface anyway, go outside
    of standard C or C++, it's beyond the scope anyway. I don't even know
    what include files are necessary for all this magic to take place.
    Basically, this is what I have (that would be compilable but for my
    lack of understanding the environment):

    // some includes here, don't know what
    int main() {
    UINT64 bigInt = 3;

    Print("bigInt contains %lu\n", bigInt);

    return 0;
    }

    That's what I have, but the output looks like:
    bigInt contains ?

    The compiler isn't catching that the %ld is for a signed, rather than
    unsigned, 64bit int. For this reason, I'm betting that somewhere,
    someone's not turning on something like "-Wformat" (that's for GNU
    gcc). I dug up that little tid-bit after finding out why I've been
    slapped before in the past for using a format specifier that didn't
    match the data type.

    Shoot, this environment is so unfamiliar to me that I've never seen a
    system where you need to define the entry point in a header file, but
    you do with this.

    Andy

  8. Default Re: What format specifier, modifier, do I need for a 64 bit unsigned int

    Andrew Falanga <af300wsm@gmail.com> writes:
    > On May 28, 10:33 am, Keith Thompson <ks...@mib.org> wrote:
    >> Andrew Falanga <af300...@gmail.com> writes:
    >>
    >> [ questions and answers about printf formats deleted ]
    >>
    >> > Thanks. I learned something interesting. Using %lu produced nothing
    >> > but a '?' in the output as did using %u. The only thing that works is
    >> > %ld, but this would be for a signed in, no?

    >>
    >> "%ld" is for signed long int (also known as signed long, long int, or
    >> long). "%lu" is for unsigned long int.
    >>
    >> If "%ld" works, I'd be very surprised if "%lu" produces just a '?'.
    >> Can you show us some actual code that exhibits this behavior?
    >>
    >> Note: By "actual code" I mean a small, self-contained, compilable
    >> program that we can try outselves without modification.
    >> Copy-and-paste your actual code; don't re-type it.

    [...]
    >
    > I wished I could give you what you're asking for here but in this case
    > I can't. Besides, I'm so new to the build environment that, to be
    > honest if I could copy/paste, it wouldn't do much good. Nowhere do I
    > see people using printf() or other standard calls. It's all things
    > like:
    >
    > Print()
    > UINT64
    >
    > and so forth. Since both of these, on the surface anyway, go outside
    > of standard C or C++, it's beyond the scope anyway. I don't even know
    > what include files are necessary for all this magic to take place.
    > Basically, this is what I have (that would be compilable but for my
    > lack of understanding the environment):
    >
    > // some includes here, don't know what
    > int main() {
    > UINT64 bigInt = 3;
    >
    > Print("bigInt contains %lu\n", bigInt);
    >
    > return 0;
    > }
    >
    > That's what I have, but the output looks like:
    > bigInt contains ?
    >
    > The compiler isn't catching that the %ld is for a signed, rather than
    > unsigned, 64bit int. For this reason, I'm betting that somewhere,
    > someone's not turning on something like "-Wformat" (that's for GNU
    > gcc). I dug up that little tid-bit after finding out why I've been
    > slapped before in the past for using a format specifier that didn't
    > match the data type.
    >
    > Shoot, this environment is so unfamiliar to me that I've never seen a
    > system where you need to define the entry point in a header file, but
    > you do with this.


    Obviously I don't know what Print() does. I'll assume in the
    following that its format string means the same thing as the format
    string for printf() (if it doesn't, somebody should find the designer
    and hurt him).

    I'm still very surprised that this would print a '?' character. It
    should attempt to print, in decimal, the value of the argument, which
    it will assume is of type unsigned long. Barring undefined behavior,
    you should get nothing but a string of decimal digits.

    In any case, you need to make sure that the type of the argument
    matches the format string. In this case, you don't really know that
    UINT64 and unsigned long are the same type, or even compatible.

    You can avoid this problem with a cast:

    printf("bigInt contains %lu\n", (unsigned long)bigInt);

    or, assuming Print behaves sanely:

    Print("bigInt contains %lu\n", (unsigned long)bigInt);

    This is one of the few cases where a cast is a good idea.

    --
    Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

  9. Default Re: What format specifier, modifier, do I need for a 64 bit unsignedint

    On May 28, 4:34 pm, Keith Thompson <ks...@mib.org> wrote:
    > Andrew Falanga <af300...@gmail.com> writes:
    > > On May 28, 10:33 am, Keith Thompson <ks...@mib.org> wrote:
    > >> Andrew Falanga <af300...@gmail.com> writes:

    >
    > >> [ questions and answers about printf formats deleted ]

    >
    > >> > Thanks. I learned something interesting. Using %lu produced nothing
    > >> > but a '?' in the output as did using %u. The only thing that works is
    > >> > %ld, but this would be for a signed in, no?

    >
    > >> "%ld" is for signed long int (also known as signed long, long int, or
    > >> long). "%lu" is for unsigned long int.

    >
    > >> If "%ld" works, I'd be very surprised if "%lu" produces just a '?'.
    > >> Can you show us some actual code that exhibits this behavior?

    >
    > >> Note: By "actual code" I mean a small, self-contained, compilable
    > >> program that we can try outselves without modification.
    > >> Copy-and-paste your actual code; don't re-type it.

    > [...]
    >
    > > I wished I could give you what you're asking for here but in this case
    > > I can't. Besides, I'm so new to the build environment that, to be
    > > honest if I could copy/paste, it wouldn't do much good. Nowhere do I
    > > see people using printf() or other standard calls. It's all things
    > > like:

    >
    > > Print()
    > > UINT64

    >
    > > and so forth. Since both of these, on the surface anyway, go outside
    > > of standard C or C++, it's beyond the scope anyway. I don't even know
    > > what include files are necessary for all this magic to take place.
    > > Basically, this is what I have (that would be compilable but for my
    > > lack of understanding the environment):

    >
    > > // some includes here, don't know what
    > > int main() {
    > > UINT64 bigInt = 3;

    >
    > > Print("bigInt contains %lu\n", bigInt);

    >
    > > return 0;
    > > }

    >
    > > That's what I have, but the output looks like:
    > > bigInt contains ?

    >
    > > The compiler isn't catching that the %ld is for a signed, rather than
    > > unsigned, 64bit int. For this reason, I'm betting that somewhere,
    > > someone's not turning on something like "-Wformat" (that's for GNU
    > > gcc). I dug up that little tid-bit after finding out why I've been
    > > slapped before in the past for using a format specifier that didn't
    > > match the data type.

    >
    > > Shoot, this environment is so unfamiliar to me that I've never seen a
    > > system where you need to define the entry point in a header file, but
    > > you do with this.

    >
    > Obviously I don't know what Print() does. I'll assume in the
    > following that its format string means the same thing as the format
    > string for printf() (if it doesn't, somebody should find the designer
    > and hurt him).
    >
    > I'm still very surprised that this would print a '?' character. It
    > should attempt to print, in decimal, the value of the argument, which
    > it will assume is of type unsigned long. Barring undefined behavior,
    > you should get nothing but a string of decimal digits.
    >
    > In any case, you need to make sure that the type of the argument
    > matches the format string. In this case, you don't really know that
    > UINT64 and unsigned long are the same type, or even compatible.
    >
    > You can avoid this problem with a cast:
    >
    > printf("bigInt contains %lu\n", (unsigned long)bigInt);
    >
    > or, assuming Print behaves sanely:
    >
    > Print("bigInt contains %lu\n", (unsigned long)bigInt);
    >
    > This is one of the few cases where a cast is a good idea.
    >
    > --
    > Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
    > Nokia
    > "We must do something. This is something. Therefore, we must do this."
    > -- Antony Jay and Jonathan Lynn, "Yes Minister"


    Supposedly, Print() works in the same fashion as printf(). So far,
    this seems to be true.

    I did do some digging and found where the build environment is pulling
    these types from and according to the header files in the appropriate
    include directories, the types, such as UINT64, are based upon the
    ANSI C integer types. Indeed, UINT64 is typedef'd as uint64_t. This
    typedef, uint64_t, is itself dependent upon whether or not an MS
    environment is being used for compilation or not. In the case of MS,
    the type is

    unsigned __int64

    and if not MS it's

    unsigned long long

    At any rate, honestly, I'm confused as to why I'm getting a '?'
    instead of some sort of integer value also.

    Thanks again,
    Andy

  10. Default Re: What format specifier, modifier, do I need for a 64 bit unsigned int

    Andrew Falanga wrote:

    > At any rate, honestly, I'm confused as to why I'm getting a '?'
    > instead of some sort of integer value also.


    Presumably, the Print() function does not forward the format string and
    arguments to printf(), but parses the format string itself.
    If that is the case, the question-mark is probably the result of an
    unrecognised/not implemented specifier. IOW, Print() does not
    understand %u.
    The only way to verify this, is to ask the author/maintainer of Print()
    or a very seasoned user of that function.

    >
    > Thanks again,
    > Andy


    Bart v Ingen Schenau
    --
    a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
    c.l.c FAQ: http://c-faq.com/
    c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/

+ Reply to Thread
Page 1 of 2 1 2 LastLast