Limitations of the C ++ Preprocessor Directive

I have a C ++ preprocessor directive that looks something like this:

#if (SOME_NUMBER != 999999999999999)
// do stuff
#endif

999999999999999 is obviously greater than 2 32 so the value will not fit into a 32-bit integer. Will the preprocessor correctly use the 64-bit integer to allow comparisons, or will it truncate one or both values?

+3
source share
3 answers

Try using the suffix LL:

#if (SOME_NUMBER != 999999999999999LL)
// do stuff
#endif

In my gcc this work is fine:

#include <iostream>

#define SOME_NUMBER 999999999999999LL

int main()
{

#if (SOME_NUMBER != 999999999999999LL)
    std::cout << "yes\n";
#endif

    return 0;
}

With or without the LL suffix.

+2
source

You can try using the constant UINT_MAXdefined in the "limits.h" section:

#if (SOME_NUMBER != UINT_MAX)
// do stuff
#endif

UINT_MAX The value changes depending on the integer size.

+1

Preprocessor arithmetic works as a normal constant expressions (see. Standard, 16.1 / 4), except that intand unsigned intare treated as if they were longand unsigned long. Therefore, if you have a 64-bit type, you can use it as usual.

+1
source

Source: https://habr.com/ru/post/1789820/


All Articles