J
John Reye
Assume you have a variable called
var
of unknown type. (Or that the type can be changed).
How can one construct a literal, to be the largest value that this
variable can hold, without information about the variables type?
#include <stdio.h>
#define MAX_VALUE_OF_VAR(var) (how should this macro look like, please
use helper macros if necessary)
int main()
{
signed char var = 0; // only change this line (to other types)
do {
++var;
printf("%lld, ", (long long int) var);
} while (var != MAX_VALUE_OF_VAR(var));
return 0;
}
In the above, I currently expect the output to be 1, 2, 3, ..., 127
If above is changed:
signed short var = 0;
.... then I would like the output to be
1, 2, 3, ..., 32767
If above is changed:
signed int var = 0;
.... and recompile, then I would like the output to be
1, 2, 3, ..., 2147483647
etc.
Is it possible to construct a macro MAX_VALUE_OF_VAR that handles both
signed and unsigned?
I've tried and am having a lot of trouble with integer promotions and
the MSB for negative numbers.
Is this possible at all.
Thanks for pointers.
var
of unknown type. (Or that the type can be changed).
How can one construct a literal, to be the largest value that this
variable can hold, without information about the variables type?
#include <stdio.h>
#define MAX_VALUE_OF_VAR(var) (how should this macro look like, please
use helper macros if necessary)
int main()
{
signed char var = 0; // only change this line (to other types)
do {
++var;
printf("%lld, ", (long long int) var);
} while (var != MAX_VALUE_OF_VAR(var));
return 0;
}
In the above, I currently expect the output to be 1, 2, 3, ..., 127
If above is changed:
signed short var = 0;
.... then I would like the output to be
1, 2, 3, ..., 32767
If above is changed:
signed int var = 0;
.... and recompile, then I would like the output to be
1, 2, 3, ..., 2147483647
etc.
Is it possible to construct a macro MAX_VALUE_OF_VAR that handles both
signed and unsigned?
I've tried and am having a lot of trouble with integer promotions and
the MSB for negative numbers.
Is this possible at all.
Thanks for pointers.