Is there a function in C that returns the maximum value of a variable like this (I will name the function “maxvalue” in example below)?
int a;
printf("%d", maxvalue(a)); // 32767
unsigned int b;
printf("%d", maxvalue(b)); // 65535
So basically the function returns values like INT_MAX when the variable is signed INT, UINT_MAX when unsigned int, etc.
Such a function is not defined by the C standard library. You can try defining a macro that calculates it:
When using it, be careful it is assigned to a type large enough. For example:
This prints out: