Disclaimer, new to programming, working my way through C++ Prime Plus 6th ed.
I’m working though listing 3.1.
#include <iostream>
#include <climits>
int main()
{
using namespace std;
int n_int = INT_MAX;
cout << "int is " << sizeof n_int << " bytes." << endl;
return 0;
}
So I get, that creates a variable sets the max int value.
However, is there any reason why I should not and can’t go:
cout << "int is " << sizeof (INT_MAX) << " bytes." << endl;
As it gives the correct length. But when I try with (SHRT_MAX) it returns 4 bytes, when I’d hoped it would return 2.
Again with (LLONG_MAX) it returns correctly 8 bytes, however (LONG_MAX) incorrectly returns 8.
Any clarification would be great.
The values defined in
<climits>are macros that expand to integer literals. The type of an integer literal is the smallest integer type that can hold the value, but no smaller thanint.So
INT_MAXwill have typeint, and sosizeof INT_MAXis the same assizeof (int). However,SHRT_MAXwill also have typeint, and sosizeof SHRT_MAXwill not necessarily equalsizeof (short).