Reputation: 421
I wanted to know what is the maximum number I could type with in a macro expression, until it overflowed. I wrote the following program:
#define I (INT_MAX + 1)
#define J (LONG_MAX + 1)
int main()
{
cout << INT_MAX << I << endl;
cout << LONG_MAX << J << endl;
return 0;
}
I got the following output:
2147483647-2147483648
9223372036854775807-9223372036854775808
I don't understand the overflow for the int case, if it can store a value of LONG_MAX. Also then I remembered that macros can also store decimals like 3.14 (PI), so it should be a float type. I am now confused. Please help.
Upvotes: 0
Views: 467
Reputation: 238401
Macros do not have a type. They do not interact with the type system at all. Macros are part of a pre-processing step that is performed before compilation. Macros are textual replacement. After your source has been pre-processed, the result seen by the compiler is as follows:
int main()
{
cout << INT_MAX << (INT_MAX + 1) << endl;
cout << LONG_MAX << (LONG_MAX + 1) << endl;
return 0;
}
... except INT_MAX
and LONG_MAX
are themselves macros, so they would have been expanded to some integer literal as part of the pre-processing as well. The exact values are implementation specific and can differ between systems, so I did not expand them here in order for the answer remain general.
P.S. The behaviour of the program is undefined, because of signed overflow.
Upvotes: 1