unsigned long long

Discussion in 'C Programming' started by Richard A. Huebner, Nov 11, 2003.

  1. Is the unsigned long long primitive data type supported in ANSI
    standard C?

    I've tried using it a couple of times in standard C, but to no avail.
    I'm using both MS VIsual C++ 6, as well as the gcc compiler that comes
    with RedHat linux 9.

    If not, what data type will yield the largest unsigned integer for me?

    Thanks for your help,

    Rich
     
    Richard A. Huebner, Nov 11, 2003
    #1
    1. Advertisements

  2. In C99, yes. In C90, no. Most current implementations of ANSI standard
    C are still C90.
    unsigned long.
     
    Joona I Palaste, Nov 11, 2003
    #2
    1. Advertisements

  3. Richard A. Huebner

    Tom St Denis Guest

    Actually GCC has supported "unsigned long long" for quite some time. MSVC
    supports "unsigned __int64" which is a 64-bit type..

    Tom
     
    Tom St Denis, Nov 11, 2003
    #3
  4. Richard A. Huebner

    Nudge Guest

    I don't know how to answer your question.

    uint64_t might be useful still?
     
    Nudge, Nov 11, 2003
    #4
  5. Yes, but hardly in ANSI standard C, or what?
     
    Joona I Palaste, Nov 11, 2003
    #5
  6. Richard A. Huebner

    Tom St Denis Guest

    ANSI is old. I for one welcome our new ISO overlords.

    Tom
     
    Tom St Denis, Nov 11, 2003
    #6
  7. I would think that the difference between the ANSI and ISO versions of
    the C standard is entirely bureaucratic and has no effect on the
    technical content.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "'So called' means: 'There is a long explanation for this, but I have no
    time to explain it here.'"
    - JIPsoft
     
    Joona I Palaste, Nov 11, 2003
    #7
  8. Richard A. Huebner

    Tom St Denis Guest

    Be that as it may "unsigned long long" is part of ISOC AFAIK.

    Tom
     
    Tom St Denis, Nov 11, 2003
    #8
  9. ISO C99, Tom. Not ISO C90.
     
    Joona I Palaste, Nov 11, 2003
    #9
  10. Richard A. Huebner

    Tom St Denis Guest

    Tom assumes we're dealing with the latest not the oldest.

    Otherwise crack out some K&R !

    Tom
     
    Tom St Denis, Nov 11, 2003
    #10
  11. Richard A. Huebner

    P.J. Plauger Guest

    The current ANSI standard for C is identical to the current ISO
    standard for C, aka C99.

    P.J. Plauger
    Dinkumware, Ltd.
    http://www.dinkumware.com
     
    P.J. Plauger, Nov 11, 2003
    #11
  12. Richard A. Huebner

    Joe Wright Guest

    Try this.

    /*
    Sizes of various things in bits..
    */
    #include <stdio.h>
    #include <limits.h>

    int main(void) {
    unsigned int a, b, c;
    unsigned long long x, y, z;

    c = -1;
    b = 1 << (sizeof(int)*CHAR_BIT-1);
    a = ~b;

    z = -1;
    y = 1LL << (sizeof(long long)*CHAR_BIT-1);
    x = ~y;

    printf("Size of void = %2lu bits\n", sizeof(void) *
    CHAR_BIT);
    printf("Size of char = %2lu bits\n", sizeof(char) *
    CHAR_BIT);
    printf("Size of short = %2lu bits\n", sizeof(short) *
    CHAR_BIT);
    printf("Size of int = %2lu bits\n", sizeof(int) *
    CHAR_BIT);
    printf("Size of long = %2lu bits\n", sizeof(long) *
    CHAR_BIT);
    printf("Size of long long = %2lu bits\n", sizeof(long long) *
    CHAR_BIT);
    printf("Size of int * = %2lu bits\n", sizeof(int *) *
    CHAR_BIT);
    printf("Size of char * = %2lu bits\n", sizeof(char *) *
    CHAR_BIT);
    printf("Size of void * = %2lu bits\n", sizeof(void *) *
    CHAR_BIT);
    printf("Size of float = %2lu bits\n", sizeof(float) *
    CHAR_BIT);
    printf("Size of double = %2lu bits\n", sizeof(double) *
    CHAR_BIT);
    printf("Size of long double = %2lu bits\n", sizeof(long double) *
    CHAR_BIT);
    printf("Max int = %11d\n", a);
    printf("Min int = %11d\n", b);
    printf("Max unsigned int = %11u\n", c);
    printf("Max long long = %20lld\n", x);
    printf("Min long long = %20lld\n", y);
    printf("Max unsigned long long = %20llu\n", z);
    return 0;
    }
     
    Joe Wright, Nov 11, 2003
    #12
  13. Richard A. Huebner

    Eric Sosman Guest

    I tried it, and the compiler said:

    "foo.c", line 12: warning: integer overflow detected: op "<<"
    "foo.c", line 19: cannot take sizeof void
    cc: acomp failed for foo.c
     
    Eric Sosman, Nov 11, 2003
    #13
  14. Joe Wright wrote:

    Apart from Eric's comments, you may find it a good idea to cast the
    expression sizeof(char) * CHAR_BIT to unsigned long before passing it to a
    variadic function such as printf.
     
    Richard Heathfield, Nov 12, 2003
    #14
  15. Richard A. Huebner

    Joe Wright Guest

    What was wrong? Should ..
    y = 1LL << (sizeof(long long)*CHAR_BIT-1);
    be
    y = 1ULL << (sizeof(long long)*CHAR_BIT-1);
    or something?
     
    Joe Wright, Nov 12, 2003
    #15
  16. Richard A. Huebner

    Joe Wright Guest

    Hmm.. size_t is unsigned long on my system. But I guess we're not
    supposed to know that.
     
    Joe Wright, Nov 12, 2003
    #16
  17. Richard A. Huebner

    Dan Pop Guest

    Or, more generally, to the actual type expected by the respective
    conversion description.

    Unless you're using a conforming C99 implementation, there is no
    conversion description that is guarantee to properly handle a size_t
    value. Even worse, in the case of sizeof(type) * CHAR_BIT, the type
    of the expression can be either size_t or int.

    Dan
     
    Dan Pop, Nov 12, 2003
    #17
  18. Richard A. Huebner

    Dan Pop Guest

    An excellent illustration of the point I've made in another thread.

    Dan
     
    Dan Pop, Nov 12, 2003
    #18
  19. Richard A. Huebner

    Dan Pop Guest

    Try it yourself, with the compiler in conforming mode, this time!

    Dan
     
    Dan Pop, Nov 12, 2003
    #19
  20. Richard A. Huebner

    Dan Pop Guest

    Even if you know that, do you also know what is size_t on the systems of
    the people trying to use your program?

    Dan
     
    Dan Pop, Nov 12, 2003
    #20
    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.