confused with range of data type

xox

Joined Sep 8, 2017
936
Not sure if this is any use to you, but just in case you still need to see if the compiler is configured properly, run this code and compare it with what you've been printing out so far:

Code:
#include <string.h>
#include <limits.h>

/*
    Convert an unsigned integer to any base (returns NULL if the buffer is too small)
*/
char* unsigned_integer_to_text(unsigned long long value, char* digits, char* buffer, size_t buffer_limit)
{
/*
    Default to decimal
*/
    if(digits == NULL)
        digits = "0123456789";
/*
    Number base is obviously equal to the length of the base's digits
*/   
    unsigned long long base = strlen(digits);
    char* back = buffer;
    for(;;)
    {
    /*
        Not enough room...bail out
    */   
        if(buffer_limit-- == 0)
            return NULL;
    /*
        Calculate and copy the next digit, then divide out
    */
        *back++ = digits[value % base];
        value /= base;
    /*
        Done!
    */
        if(value == 0)
            break;
    }
/*
    Final check to make sure there's enough room for the sentinal zero
*/
    if(buffer_limit == 0)
        return NULL;
    *back = 0;
/*
    Digits are in the wrong order, need to reverse
*/
    char* front = buffer;
    while(front < back--)
    {
        char saved = *front;
        *front++ = *back;
        *back = saved;
    }
    return buffer;
}

/*
    Same as above but uses an internal buffer instead (WARNING: Not thread-safe!)
*/
char* unsigned_integer_to_static_text(unsigned long long value, char* digits)
{
    static char global[sizeof(unsigned long long) * CHAR_BIT + 1];
    return unsigned_integer_to_text(value, digits, global, sizeof(global));
}

/*
    Simple hack to return an "all-bits-set" unsigned value
*/
unsigned long long unsigned_long_long_maximum(void)
{
    unsigned long long result = 0;
    unsigned char* next = (unsigned char*)&result, * end = next + sizeof(unsigned long long);
    while(next != end)
        *next++ = 0xff;
    return result;
}

/*
    A few examples
*/
#include <stdio.h>

int main(void)
{
    puts("*** Maximum value of an \"unsigned long long\" on this system ***");
    printf("Bits: %zu\n", sizeof(unsigned long long) * CHAR_BIT);
    unsigned long long maximum = unsigned_long_long_maximum();
    printf("Decimal: %s\n", unsigned_integer_to_static_text(maximum, NULL));
    printf("Hex: 0x%s\n", unsigned_integer_to_static_text(maximum, "0123456789ABCDEF"));
    printf("Octal: 0%s\n", unsigned_integer_to_static_text(maximum, "01234567"));
    printf("Binary: %s\n", unsigned_integer_to_static_text(maximum, "01"));
}
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
Not sure if this is any use to you, but just in case you still need to see if the compiler is configured properly, run this code and compare it with what you've been printing out so far:
It gives below output
Code:
*** Maximum value of an "unsigned long long" on this system ***
Bits: zu
Decimal: 18446744073709551615
Hex: 0xFFFFFFFFFFFFFFFF
Octal: 01777777777777777777777
Binary: 1111111111111111111111111111111111111111111111111111111111111111
 

xox

Joined Sep 8, 2017
936
It gives below output
Code:
*** Maximum value of an "unsigned long long" on this system ***
Bits: zu
Decimal: 18446744073709551615
Hex: 0xFFFFFFFFFFFFFFFF
Octal: 01777777777777777777777
Binary: 1111111111111111111111111111111111111111111111111111111111111111
Okay so the %zu flag doesn't seem to be supported by your compiler. And since that was defined in the C99 standard that means you are almost definitely using a pre-C99 compiler. Which would explain why you can't print unsigned long long values from printf either. But if you've downloaded the most recent version of Mingw it should be up to spec. Maybe try another mirror or else just a different compiler altogether (pretty sure Visual Studio has a most excellent FREE compiler you can download that will obviously work on a Windows machine). Sorry, I use Linux and not much of an expert on Windows software...
 
Top