Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Constant macro parameters

Reply
Thread Tools

Constant macro parameters

 
 
Ricardo Gibert
Guest
Posts: n/a
 
      09-05-2003
How can I "force" a macro to accept integer constants or expressions whose value is determined during compilation? For example, if
we have:

#define TEST(X) blah blah with X

I want the macro to be used as

x += TEST(5);
y += TEST(3*7);

and not as

z += TEST(i);
z += TEST(i*3);

when I would prefer a compile time error.

Is this possible in C?


 
Reply With Quote
 
 
 
 
Kevin Easton
Guest
Posts: n/a
 
      09-05-2003
Ricardo Gibert <> wrote:
> How can I "force" a macro to accept integer constants or expressions whose value is determined during compilation? For example, if
> we have:
>
> #define TEST(X) blah blah with X
>
> I want the macro to be used as
>
> x += TEST(5);
> y += TEST(3*7);
>
> and not as
>
> z += TEST(i);
> z += TEST(i*3);
>
> when I would prefer a compile time error.
>
> Is this possible in C?


Yes, in C89 (which is the standard of C currently implemented by
virtually all compilers).

#define TEST(X) ((void)sizeof(int [X]), X)

This requires a diagnostic from a conforming C89 compiler if X is not a
compile-time constant. For example:

$ gcc -Wall -ansi -pedantic foo.c -o foo
foo.c: In function `main':
foo.c:14: warning: ANSI C forbids variable-size array `type name'
foo.c:15: warning: ANSI C forbids variable-size array `type name'

Unfortunately, most compilers are by default non-conforming, and many of
them implement variable sized arrays as an extension, so they don't
issue this required diagnostic unless specifically asked to run in
conforming mode.

In addition, variable sized arrays are part of the newer C99 C Standard,
so this method also doesn't work on the few compilers that support it.

- Kevin.

 
Reply With Quote
 
 
 
 
Stephan Wilms
Guest
Posts: n/a
 
      09-05-2003
Hi,

You might exploit the rule that the expression for a "case" construct
must be an unsigned integer expression. I was thinking along these
lines:

#define TEST(X) {switch(1) { case X: ; }}

This is required to produce an error diagnostic for a non constant
integral case expression.

--
Stephan


"Ricardo Gibert" <> wrote in message news:<UJS5b.113549$kP.32940@fed1read03>...
> How can I "force" a macro to accept integer constants or expressions whose value is determined during compilation? For example, if
> we have:
>
> #define TEST(X) blah blah with X
>
> I want the macro to be used as
>
> x += TEST(5);
> y += TEST(3*7);
>
> and not as
>
> z += TEST(i);
> z += TEST(i*3);
>
> when I would prefer a compile time error.
>
> Is this possible in C?

 
Reply With Quote
 
Kevin Easton
Guest
Posts: n/a
 
      09-05-2003
Stephan Wilms <> wrote:
> Hi,
>
> You might exploit the rule that the expression for a "case" construct
> must be an unsigned integer expression. I was thinking along these
> lines:
>
> #define TEST(X) {switch(1) { case X: ; }}
>
> This is required to produce an error diagnostic for a non constant
> integral case expression.


The problem with that is that you can't include a switch statement in a
macro that is supposed to behave like an expression.

- Kevin.

 
Reply With Quote
 
Scott Fluhrer
Guest
Posts: n/a
 
      09-05-2003

"Kevin Easton" <kevin@-nospam-pcug.org.au> wrote in message
news:newscache$2q1qkh$msk$...
> Ricardo Gibert <> wrote:
> > How can I "force" a macro to accept integer constants or expressions

whose value is determined during compilation? For example, if
> > we have:
> >
> > #define TEST(X) blah blah with X
> >
> > I want the macro to be used as
> >
> > x += TEST(5);
> > y += TEST(3*7);
> >
> > and not as
> >
> > z += TEST(i);
> > z += TEST(i*3);
> >
> > when I would prefer a compile time error.
> >
> > Is this possible in C?

>
> Yes, in C89 (which is the standard of C currently implemented by
> virtually all compilers).
>
> #define TEST(X) ((void)sizeof(int [X]), X)
>
> This requires a diagnostic from a conforming C89 compiler if X is not a
> compile-time constant. For example:
>
> $ gcc -Wall -ansi -pedantic foo.c -o foo
> foo.c: In function `main':
> foo.c:14: warning: ANSI C forbids variable-size array `type name'
> foo.c:15: warning: ANSI C forbids variable-size array `type name'
>
> Unfortunately, most compilers are by default non-conforming, and many of
> them implement variable sized arrays as an extension, so they don't
> issue this required diagnostic unless specifically asked to run in
> conforming mode.
>
> In addition, variable sized arrays are part of the newer C99 C Standard,
> so this method also doesn't work on the few compilers that support it.


How about

#define TEST(X) (sizeof (struct { int tag : (X) ? 1 : 2; } ), X )

I don't know of any compiler that supports variable sized bitfields.

--
poncho


 
Reply With Quote
 
Ricardo Gibert
Guest
Posts: n/a
 
      09-06-2003

"Scott Fluhrer" <> wrote in message news:1L06b.151$ et...
>
> "Kevin Easton" <kevin@-nospam-pcug.org.au> wrote in message
> news:newscache$2q1qkh$msk$...
> > Ricardo Gibert <> wrote:
> > > How can I "force" a macro to accept integer constants or expressions

> whose value is determined during compilation? For example, if
> > > we have:
> > >
> > > #define TEST(X) blah blah with X
> > >
> > > I want the macro to be used as
> > >
> > > x += TEST(5);
> > > y += TEST(3*7);
> > >
> > > and not as
> > >
> > > z += TEST(i);
> > > z += TEST(i*3);
> > >
> > > when I would prefer a compile time error.
> > >
> > > Is this possible in C?

> >
> > Yes, in C89 (which is the standard of C currently implemented by
> > virtually all compilers).
> >
> > #define TEST(X) ((void)sizeof(int [X]), X)
> >
> > This requires a diagnostic from a conforming C89 compiler if X is not a
> > compile-time constant. For example:
> >
> > $ gcc -Wall -ansi -pedantic foo.c -o foo
> > foo.c: In function `main':
> > foo.c:14: warning: ANSI C forbids variable-size array `type name'
> > foo.c:15: warning: ANSI C forbids variable-size array `type name'
> >
> > Unfortunately, most compilers are by default non-conforming, and many of
> > them implement variable sized arrays as an extension, so they don't
> > issue this required diagnostic unless specifically asked to run in
> > conforming mode.
> >
> > In addition, variable sized arrays are part of the newer C99 C Standard,
> > so this method also doesn't work on the few compilers that support it.

>
> How about
>
> #define TEST(X) (sizeof (struct { int tag : (X) ? 1 : 2; } ), X )


This works! Thanks!

Ideally, a more appropriate error message would be preferable (I use mingw), but my attempts at incorporating the const keyword to
this end did not bear fruit. Good enough for gov't work!

>
> I don't know of any compiler that supports variable sized bitfields.
>
> --
> poncho
>
>



 
Reply With Quote
 
Dave Thompson
Guest
Posts: n/a
 
      09-15-2003
On Fri, 05 Sep 2003 03:30:03 GMT, Kevin Easton
<kevin@-nospam-pcug.org.au> wrote:

> Ricardo Gibert <> wrote:
> > How can I "force" a macro to accept integer constants or expressions whose value is determined during compilation? For example, if
> > we have:

<snip>
> Yes, in C89 (which is the standard of C currently implemented by
> virtually all compilers).
>
> #define TEST(X) ((void)sizeof(int [X]), X)
>

Don't actually need the cast, the left argument of the comma operator
is already a void context.

> This requires a diagnostic from a conforming C89 compiler if X is not a
> compile-time constant. For example: <snip gcc>
> Unfortunately, most compilers are by default non-conforming, and many of
> them implement variable sized arrays as an extension, so they don't
> issue this required diagnostic unless specifically asked to run in
> conforming mode.
>
> In addition, variable sized arrays are part of the newer C99 C Standard,
> so this method also doesn't work on the few compilers that support it.
>

In the latter case could make it sizeof(struct { int foo [X]; }).

But at least gcc (2.95.2) actually supports variable-bound arrays
within a struct, and not just at the end! Bleah -- how un-C-like!


- David.Thompson1 at worldnet.att.net
 
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
"error C2057: expected constant expression", "error C2466: cannot allocate an array of constant size 0". Why doesn't my simple program work??? hn.ft.pris@gmail.com C++ 13 01-22-2007 02:03 PM
pointers to constant characters and constant pointers to characters sam_cit@yahoo.co.in C Programming 4 12-14-2006 11:10 PM
len(var) is [CONSTANT] equal to len(var) == [CONSTANT]? Tor Erik Soenvisen Python 14 11-23-2006 09:57 PM
"Non-constant" constant can't be used as template argument Martin Magnusson C++ 2 10-08-2004 08:41 AM
Understanding How To Use #ifdef Constant #define Constant Sequence In Multible Files Christopher M. Lusardi C++ 1 09-02-2004 07:43 AM



Advertisments