Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Generates integer literal of the largest value a variable can hold. Macro takes only variable-name. |
| Date | 2012-04-23 17:11 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <4F95C589.1080208@verizon.net> (permalink) |
| References | <3b194163-c8e2-4250-864a-d3f0a1324677@f5g2000vby.googlegroups.com> |
On 04/23/2012 03:38 PM, John Reye wrote:
> 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
Do you have enough time to read that output? Do you have enough paper
an toner to print it out?
> 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.
Not in general.
For unsigned types, assigning a value of -1 to the variable will set it
to the maximum value of it's type. Would it be acceptable for the macro
to change the variable's value? It wouldn't work in your example code -
would your intended use for this macro allow a re-write to avoid that
problem? Assigning a value of -1 to a variable is a perfectly safe thing
to do for all arithmetic types, and testing whether the result is
greater than 0 is a safe and reliable way of testing for unsignedness.
If you're willing to make non-portable but very often correct
assumptions, like 2's complement and no padding bits or trap
representations, then
((long long)pow(2.0,CHAR_BIT*sizeof(var) - 1)-1)
works for the signed types. If you need portability to systems where
those assumptions don't apply, you're out of luck.
The "no type information" approach seems rather odd - it's not something
that comes up very often in idiomatically written C - you almost always
know the type of anything you work with, except when using typedefs. It
sound almost as if you were trying to write a C++ template in C. You
need to approach problems differently when using C than when using C++.
If you try to force C to work like C++, you're going to frustrate
yourself and mystify those who work on your code later, trying to
implement poor substitutes for genericity in C.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Generates integer literal of the largest value a variable can hold. Macro takes only variable-name. John Reye <jononanon@googlemail.com> - 2012-04-23 12:38 -0700
Re: Generates integer literal of the largest value a variable can hold. Macro takes only variable-name. Thomas Richter <thor@math.tu-berlin.de> - 2012-04-23 22:08 +0200
Re: Generates integer literal of the largest value a variable can hold. Macro takes only variable-name. James Kuyper <jameskuyper@verizon.net> - 2012-04-23 16:45 -0400
Re: Generates integer literal of the largest value a variable can hold. Macro takes only variable-name. John Reye <jononanon@googlemail.com> - 2012-04-23 13:28 -0700
Re: Generates integer literal of the largest value a variable can hold. Macro takes only variable-name. James Kuyper <jameskuyper@verizon.net> - 2012-04-23 17:01 -0400
Re: Generates integer literal of the largest value a variable can hold. Macro takes only variable-name. Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-05-04 00:30 -0700
Re: Generates integer literal of the largest value a variable can hold. Macro takes only variable-name. Ben Pfaff <blp@cs.stanford.edu> - 2012-04-23 13:36 -0700
Re: Generates integer literal of the largest value a variable can hold. Macro takes only variable-name. James Kuyper <jameskuyper@verizon.net> - 2012-04-23 17:11 -0400
Re: Generates integer literal of the largest value a variable can hold. Macro takes only variable-name. John Gordon <gordon@panix.com> - 2012-04-23 22:03 +0000
Re: Generates integer literal of the largest value a variable can hold. Macro takes only variable-name. John Reye <jononanon@googlemail.com> - 2012-05-03 15:35 -0700
Re: Generates integer literal of the largest value a variable can hold. Macro takes only variable-name. Jens Gustedt <jens.gustedt@loria.fr> - 2012-05-04 09:00 +0200
Re: Generates integer literal of the largest value a variable can hold. Macro takes only variable-name. James Kuyper <jameskuyper@verizon.net> - 2012-05-04 07:54 -0400
Re: Generates integer literal of the largest value a variable can hold. Macro takes only variable-name. Tim Rentsch <txr@alumni.caltech.edu> - 2012-05-07 21:40 -0700
csiph-web