user105033
user105033

Reputation: 19568

C define 64bit on 32bit

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?

Upvotes: 2

Views: 3645

Answers (5)

Keith Thompson
Keith Thompson

Reputation: 263337

#define TIMEFIXCONST 11644473600

Any use of the macro TIMEFIXCONST expands at compile time to the constant 11644473600. It's not stored anywhere unless you store it.

A decimal integer constant is of type int, long int, or long long int, depending on the value of the constant and the ranges of the types (it's the first of those types that's wide enough to hold the value). There's no need to add an L or LL suffix unless you want to be explicit about the type. Even then, 11644473600L may be of type long long rather than long.

(In C89/C90, it's of type int, long int, or unsigned long int, but if unsigned long int is only 32 bits the constant itself is an error.)

Since 11644473600 requires at least 34 bits (plus the sign bit if any), it's probably of type long if long is 64 bits, or long long otherwise.

If you assign that value to an int variable:

int n = TIMEFIXCONST;

it will be converted to int, and the result is implementation-defined. You'll probably get a compile-time warning; if you don't, find out how to enable more warnings in your compiler.

Upvotes: 0

Thomas Padron-McCarthy
Thomas Padron-McCarthy

Reputation: 27632

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

Upvotes: 3

Arkaitz Jimenez
Arkaitz Jimenez

Reputation: 23188

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.

Upvotes: 5

jesup
jesup

Reputation: 6984

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.

Upvotes: 1

Daniel A. White
Daniel A. White

Reputation: 190942

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.

Upvotes: 0

Related Questions