tags:

views:

339

answers:

4

If I do:

#define TIMEFIXCONST 11644473600

on a 32bit machine, will it overflow or will it be stored as a long long and still work properly? Should I just define a global unsigned long long and use that instead?

A: 

If you store this in an int, it will overflow on both x64 and x86. If you store it in a long, you wont have the problem on either platform. The #define has no bearing on memory.

Daniel A. White
It's not guaranteed to fit into a `long` either - `int` is a minimum of 16 bits and `long` is a minimum of 32 bits (and different x86 and x86-64 ABIs define these differently).
caf
+1  A: 

A (non?)standard way to do that would be

#define TIMEFIXCONST 11644473600LL

Then it will be treated as "long long". What happens after that depends on the statement you use it in (overflow, etc). If you try to assign it to a 32-bit variable it will get truncated and the compiler should throw a warning.

jesup
That's standard - as long as the standard is C99. It's non-standard if the standard is C89.
Jonathan Leffler
+5  A: 

A macro is only a text substitution, you can't overflow a macro.
It depends on where do you assign later TIMEFIXCONST.

But as a rule of thumb, when using constants use const int or const long long if you require.

Arkaitz Jimenez
+3  A: 

The number is not "stored" anywhere. It will just be inserted in the program source code where you use the macro, just as if you had written it directly. But if you want the literal itself to be of type long long, write:

#define TIMEFIXCONST 11644473600LL
Thomas Padron-McCarthy