Lemonbonbon
Lemonbonbon

Reputation: 748

Using uint64_t in 32 bit application

Due to some libraries, I have to compile my application in 32 bit, but I need to use integer variables that exceed the max number of 32 bit types. So for example if I try to use uint64_t I get an overflow at 2147483647.

I thought it is possible to use 64 bit integer variables in 32 bit application, so what did I miss here? Do I have to include some specific header oder do I have to set some option therefore? Using VS17.

EDIT:

I did some testing, and in this example program, I can reproduce my overflow problem.

#include <iostream>

int main()
{
    uint64_t i = 0;

    while (true)
    {
        std::printf("%d\n",i);
        i += (uint64_t)10000;
    }
    return 0;
}

Upvotes: 3

Views: 1846

Answers (1)

eerorika
eerorika

Reputation: 238461

The bug is here:

std::printf("%d\n",i);
             ^^

You've used the wrong format specifier, and therefore the behaviour of the program is undefined. %d is for signed int. You need to use

std::printf("%" PRIu64 "\n",i);

PRIu64 is declared in <cinttypes>.

P.S. You also haven't included the header which declares std::printf.

Upvotes: 4

Related Questions