Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Generates integer literal of the largest value a variable can hold.Macro takes only variable-name.

Reply
Thread Tools

Generates integer literal of the largest value a variable can hold.Macro takes only variable-name.

 
 
John Reye
Guest
Posts: n/a
 
      05-03-2012
Here's a nice hack for... getting the max value for any unsigned type:
from "unsigned char" to "unsigned long long":

#include <stdio.h>
#include <limits.h>

#define GET_TYPE_INT_OR_LARGER_0(var) (var & 0)

#define GET_SHIFT_CHAR_BIT2(var) ((sizeof(var) >= sizeof(int)) ? 0 :
(CHAR_BIT * (sizeof(int)-sizeof(var))))

/* only unsigned */
#define GET_MAX_UNSIGNED_RVALUE(var) ((GET_TYPE_INT_OR_LARGER_0(var)
-1U) >> GET_SHIFT_CHAR_BIT2(var))


int main(void)
{
typedef unsigned long long my_ull;

my_ull var2;
printf("%llx\n", GET_MAX_UNSIGNED_RVALUE(var2));

unsigned i;
printf("%x\n", GET_MAX_UNSIGNED_RVALUE(i));

unsigned short s;
printf("%x\n", GET_MAX_UNSIGNED_RVALUE(s));

unsigned char c;
printf("%x\n", GET_MAX_UNSIGNED_RVALUE(c));

return 0;
}

What do you think?

Does the macro GET_MAX_UNSIGNED_RVALUE() really create a literal?
(i.e. something that can be used in #if ... preprocessing)
Or does it depend on the compiler?

Thanks.
 
Reply With Quote
 
 
 
 
Jens Gustedt
Guest
Posts: n/a
 
      05-04-2012
Am 05/04/2012 12:35 AM, schrieb John Reye:
> Here's a nice hack for... getting the max value for any unsigned type:
> from "unsigned char" to "unsigned long long":
>
> #include <stdio.h>
> #include <limits.h>
>
> #define GET_TYPE_INT_OR_LARGER_0(var) (var & 0)
>
> #define GET_SHIFT_CHAR_BIT2(var) ((sizeof(var) >= sizeof(int)) ? 0 :
> (CHAR_BIT * (sizeof(int)-sizeof(var))))


> ....


> What do you think?
>
> Does the macro GET_MAX_UNSIGNED_RVALUE() really create a literal?
> (i.e. something that can be used in #if ... preprocessing)
> Or does it depend on the compiler?


no

- IIRC ternary expressions are not allowed in the preprocessor
- sizeof is not evaluated in the preprocessor

Jens
 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      05-04-2012
בתאריך יום ש*י,23 באפריל 2012 21:28:21 UTC+1, מאת John Reye:
>
> Writing portable C-code that works across different word-sizes must be
> a real horror!
>

Mostly integers count things. That usually means things in memory.

Even a char integer, if you think about it, is ultimately going to be used as an index into a table of glyphs.

So basically you need an integer that can index the largest array you have in memory.

--
Basic Algorithms: now also available as an iBook
http://www.malcolmmclean.site11.com/www

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      05-04-2012
On 05/04/2012 03:00 AM, Jens Gustedt wrote:
> Am 05/04/2012 12:35 AM, schrieb John Reye:
>> Here's a nice hack for... getting the max value for any unsigned type:
>> from "unsigned char" to "unsigned long long":
>>
>> #include <stdio.h>
>> #include <limits.h>
>>
>> #define GET_TYPE_INT_OR_LARGER_0(var) (var & 0)
>>
>> #define GET_SHIFT_CHAR_BIT2(var) ((sizeof(var) >= sizeof(int)) ? 0 :
>> (CHAR_BIT * (sizeof(int)-sizeof(var))))

>
>> ....

>
>> What do you think?
>>
>> Does the macro GET_MAX_UNSIGNED_RVALUE() really create a literal?
>> (i.e. something that can be used in #if ... preprocessing)
>> Or does it depend on the compiler?

>
> no
>
> - IIRC ternary expressions are not allowed in the preprocessor


The relevant restrictions are implied by the fact that the thing
following a #if is described by the grammar as a constant-expression.
"Constant expressions shall not contain assignment, increment,
decrement, function-call, or comma operators, except when they are
contained within a subexpression that is not evaluated." (6.6p3)
The ternary operator ?: is not one of the prohibited ones.

> - sizeof is not evaluated in the preprocessor


More precisely, during evaluation of a #if expression, the only
identifiers that have any special meaning are macros, and "defined".
Otherwise, "all remaining identifiers (including those lexically
identical to keywords) are replaced with the pp-number 0" (6.10.1p4) As
a result, sizeof(int) gets converted into 0(0), which is a syntax error.
--
James Kuyper
 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      05-08-2012
John Reye <(E-Mail Removed)> writes:

> 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?
> [snip example]


In C11, all standard numeric types can be handled
using _Generic. Details left to the reader.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Worlds Largest Photo and Worlds Largest Camera... Somebody Digital Photography 1 08-16-2007 02:51 AM
integer int i; *i++ and ++*i have a different integer value after the increment Robben C++ 14 12-27-2005 03:43 AM
Looping Problem (Generating files - only the last record generates a file) vasilijepetkovic@yahoo.com Python 6 10-26-2005 08:10 PM
What's wrong with rpc-literal? Why use doc-literal? Anonieko Ramos ASP .Net Web Services 0 09-27-2004 09:06 AM
Largest possible value of Integer Constants Nicholas C Programming 7 09-12-2003 04:33 PM



Advertisments