Thread: sizeof unsigned long long int

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    62

    sizeof unsigned long long int

    I am trying to figure out how many bytes are in an unsigned long long int on my desktop computer. But I keep getting warnings:

    warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long unsigned int’

    Code:
    #include <stdio.h>
    int main(void)
    {
    	unsigned char unit_id[] = { 0x2B, 0xC, 0x6B, 0x54}; // 8-bit
    	unsigned long long int unit_id_val;
    	int i;
        printf("the size of unsigned long long int %d", sizeof(unit_id_val  ) );
    
    
    	return 0;
    }
    Any idea how to fix the warnings?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you are compiling with respect to C99 or later, one option is:
    Code:
    printf("the size of unsigned long long int %zu", sizeof(unit_id_val));
    Otherwise you can cast:
    Code:
    printf("the size of unsigned long long int %u", (unsigned int)sizeof(unit_id_val));
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    sizeof returns a type size_t:
    Quote Originally Posted by C99 6.5.3.4 p4
    4 The value of the result is implementation-defined, and its type (an unsigned integer type)
    is size_t, defined in <stddef.h> (and other headers).
    size_t is not necessarily an int, so %d is not necessarily the right format. Thankfully, we can look up the right one:
    Quote Originally Posted by C99 7.19.6.1 p7
    z Specifies that a following d, i, o, u, x, or X conversion specifier applies to a
    size_t or the corresponding signed integer type argument; or that a
    following n conversion specifier applies to a pointer to a signed integer type
    corresponding to size_t argument.
    So, in order to print a size_t, you need to use the z length modifier, something like:
    Code:
    printf("the size of unsigned long long int %zd", sizeof(unit_id_val));
    EDIT: Dangit! You're too fast laserlight, that's the second time you ninja-posted me today.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generate unsigned long long type random numbers
    By patishi in forum C Programming
    Replies: 27
    Last Post: 09-11-2013, 09:03 PM
  2. problem while printing unsigned long long int
    By Dedalus in forum C Programming
    Replies: 3
    Last Post: 03-08-2012, 04:44 AM
  3. Replies: 1
    Last Post: 10-11-2010, 01:53 AM
  4. unsigned long long division print
    By Kempelen in forum C Programming
    Replies: 4
    Last Post: 01-30-2009, 10:03 AM
  5. unsigned long long to string conversion
    By Wiretron in forum C++ Programming
    Replies: 6
    Last Post: 12-21-2007, 04:02 AM