473,387 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Preprocessor possibilities

Is it possible to write a macro (in unextended C89) such that

TEST( int, (1,2,3) );

expands to

int array[]={ 1,2,3 };

? I strongly suspect that it is not, but I don't wish to overlook a
solution if one exists.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Dec 13 '05 #1
15 1292


Christopher Benson-Manica wrote On 12/13/05 12:39,:
Is it possible to write a macro (in unextended C89) such that

TEST( int, (1,2,3) );

expands to

int array[]={ 1,2,3 };

? I strongly suspect that it is not, but I don't wish to overlook a
solution if one exists.


#define TEST(type,list) type array[] = { list }

This seems pretty straightforward -- have I somehow
misread your question?

--
Er*********@sun.com

Dec 13 '05 #2

Eric Sosman wrote:
Christopher Benson-Manica wrote On 12/13/05 12:39,:
Is it possible to write a macro (in unextended C89) such that

TEST( int, (1,2,3) );

expands to

int array[]={ 1,2,3 };

? I strongly suspect that it is not, but I don't wish to overlook a
solution if one exists.


#define TEST(type,list) type array[] = { list }

This seems pretty straightforward -- have I somehow
misread your question?

--
Er*********@sun.com


What about the parenthesis? I think he is trying to avoid C99 varargs
macros.

-David

Dec 13 '05 #3
David Resnick <ln********@gmail.com> wrote:
What about the parenthesis? I think he is trying to avoid C99 varargs
macros.


Yes, I am. I am dealing with a C++ implementation that does not
support the C99 varargs macros (among other things).

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Dec 13 '05 #4


David Resnick wrote On 12/13/05 13:14,:
Eric Sosman wrote:
Christopher Benson-Manica wrote On 12/13/05 12:39,:
Is it possible to write a macro (in unextended C89) such that

TEST( int, (1,2,3) );

expands to

int array[]={ 1,2,3 };

? I strongly suspect that it is not, but I don't wish to overlook a
solution if one exists.


#define TEST(type,list) type array[] = { list }

This seems pretty straightforward -- have I somehow
misread your question?

What about the parenthesis? I think he is trying to avoid C99 varargs
macros.


Aha: I *did* misread the question! Thanks, D R,
and sorry, C B-M.

--
Er*********@sun.com

Dec 13 '05 #5
Christopher Benson-Manica a écrit :
Is it possible to write a macro (in unextended C89) such that

TEST( int, (1,2,3) );

expands to

int array[]={ 1,2,3 };

? I strongly suspect that it is not, but I don't wish to overlook a
solution if one exists.

The obvious way :

#define TEST(T, a, b, c)\
T array[] = {a, b, c}

int main(void)
{

TEST (int, 1, 2, 3);

return 0;
}

But I guess you want a variable list. Il suggest this :

#include <stdio.h>

int main(void)
{

int array[] =
{
#define ITEM(value)\
value,
#include "test.itm"
#undef ITEM
};

size_t i;

for (i = 0; i < sizeof array / sizeof *array; i++)
{
printf ("array[%u] = %d\n", (unsigned) i, array[i]);
}

return 0;
}

with:

/* test.itm */

ITEM (12)
ITEM (34)
ITEM (56)

which is a trick I got here on c.l.c a few years ago. I'm using it day
and night to automate code generation ! Very powerful ! (Prefcet to give
a string to an enum, for example...)

--
A+

Emmanuel Delahaye
Dec 13 '05 #6
Eric Sosman a écrit :

Christopher Benson-Manica wrote On 12/13/05 12:39,:
Is it possible to write a macro (in unextended C89) such that

TEST( int, (1,2,3) );

expands to

int array[]={ 1,2,3 };

#define TEST(type,list) type array[] = { list }

This seems pretty straightforward -- have I somehow
misread your question?


AFAICT, it doesn't work as expected:

#include <stdio.h>

#define TEST(type, list) type array[] = { list }

int main(void)
{
TEST( int, (1,2,3) );

size_t i;

for (i = 0; i < sizeof array / sizeof *array; i++)
{
printf ("array[%u] = %d\n", (unsigned) i, array[i]);
}

return 0;
}

produces

array[0] = 3

Some comma operator trick, I guess...

--
A+

Emmanuel Delahaye
Dec 13 '05 #7
Christopher Benson-Manica wrote:
Is it possible to write a macro (in unextended C89) such that

TEST( int, (1,2,3) );

expands to

int array[]={ 1,2,3 };

? I strongly suspect that it is not, but I don't wish to overlook a
solution if one exists.


No, there isn't. The closest thing you can do is something like the
following which is quite ugly:

#define TEST(x,y) x array[]={y}
#define SEP ,
TEST(int, 1 SEP 2 SEP 3);

and I am not positive that even this is valid in C89.

Robert Gamble

Dec 13 '05 #8
Christopher Benson-Manica a écrit :
Is it possible to write a macro (in unextended C89) such that

TEST( int, (1,2,3) );

expands to

int array[]={ 1,2,3 };

? I strongly suspect that it is not, but I don't wish to overlook a
solution if one exists.

The obvious way :

#define TEST(T, a, b, c)\
T array[] = {a, b, c}

int main(void)
{

TEST (int, 1, 2, 3);

return 0;
}

But I guess you want a variable list. Il suggest this :

#include <stdio.h>

int main(void)
{

int array[] =
{
#define ITEM(value)\
value,
#include "test.itm"
#undef ITEM
};

size_t i;

for (i = 0; i < sizeof array / sizeof *array; i++)
{
printf ("array[%u] = %d\n", (unsigned) i, array[i]);
}

return 0;
}

with:

/* test.itm */

ITEM (12)
ITEM (34)
ITEM (56)

which is a trick I got here on c.l.c a few years ago. I'm using it day
and night to automate code generation ! Very powerful ! (Perfect to give
a string to an enum, for example...)

--
A+

Emmanuel Delahaye
Dec 13 '05 #9

Christopher Benson-Manica wrote:
Is it possible to write a macro (in unextended C89) such that

TEST( int, (1,2,3) );

expands to

int array[]={ 1,2,3 };

? I strongly suspect that it is not, but I don't wish to overlook a
solution if one exists.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.


I assume you reject the (clunky)

#define TEST_1(type,val1) type array[]={val1}
#define TEST_2(type,val1,val2) type array[]={val1,val2}
....

-David

Dec 13 '05 #10
David Resnick <ln********@gmail.com> wrote:
I assume you reject the (clunky) #define TEST_1(type,val1) type array[]={val1}
#define TEST_2(type,val1,val2) type array[]={val1,val2}


Yes :-)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Dec 13 '05 #11
Emmanuel Delahaye <em***@yourbranoos.fr> wrote:
But I guess you want a variable list. Il suggest this :
(snip interesting technique)
which is a trick I got here on c.l.c a few years ago. I'm using it day
and night to automate code generation ! Very powerful ! (Perfect to give
a string to an enum, for example...)


That does look like a creative technique, and I'll look for
opportunities to use it. Unfortunately, it doesn't handle the actual
situation I hav; I admit that what I want to do is not a stupendous
idea. The ultimate idea was to allow use of "array literals" (such as
are found in JavaScript) to allow for macros like

if( TEST(int,1,(1,2,3)) ) { /* Checks for second argument in third */
/* ... */
}

None of the other options (inline function, C++ templates, etc.) are
particularly appetizing here either, so it seems that there is no
trick to avoiding tried-and-true (but boring) methods.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Dec 13 '05 #12
Robert Gamble <rg*******@gmail.com> wrote:
#define TEST(x,y) x array[]={y}
#define SEP ,
TEST(int, 1 SEP 2 SEP 3); and I am not positive that even this is valid in C89.


It seems to work on my implementation, FWIW, but I concur that it is
probably too ugly to consider.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Dec 13 '05 #13
Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
That does look like a creative technique, and I'll look for
opportunities to use it. Unfortunately, it doesn't handle the actual
situation I hav; I admit that what I want to do is not a stupendous
idea. The ultimate idea was to allow use of "array literals" (such as
are found in JavaScript) to allow for macros like

if( TEST(int,1,(1,2,3)) ) { /* Checks for second argument in third */
/* ... */
}


For small ranges, you can do something like this:
#define BIT(x) (1ul << (x))
#define BITS(x, y) (BIT(x) | (y))

if (BIT(x) & BITS(1, BITS(2, BIT(3))))
Not ideal.
--
"For those who want to translate C to Pascal, it may be that a lobotomy
serves your needs better." --M. Ambuhl

"Here are the steps to create a C-to-Turbo-Pascal translator..." --H. Schildt
Dec 13 '05 #14
On 2005-12-13, Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
David Resnick <ln********@gmail.com> wrote:
I assume you reject the (clunky)

#define TEST_1(type,val1) type array[]={val1}
#define TEST_2(type,val1,val2) type array[]={val1,val2}


Yes :-)


how about (c99 only)

#define TEST(type,...) type array[] = { __VA_ARGS__ }
Dec 13 '05 #15
Jordan Abel wrote:
On 2005-12-13, Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
David Resnick <ln********@gmail.com> wrote:
I assume you reject the (clunky)

#define TEST_1(type,val1) type array[]={val1}
#define TEST_2(type,val1,val2) type array[]={val1,val2}


Yes :-)


how about (c99 only)

#define TEST(type,...) type array[] = { __VA_ARGS__ }


Apparently you missed the part about c99 not being an option, but yes,
this would be the obvious solution if it were.

Robert Gamble

Dec 13 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

205
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
24
by: Nudge | last post by:
I have an array, and an unrolled loop which looks like this: do_something(A); do_something(A); .... do_something(A); I thought: why should I type so much? I should write a macro. So I was...
16
by: Trying_Harder | last post by:
Is it possible to redefine a macro with global scope after undefining it in a function? If yes, could someone explain how? /If/ my question above isn't very clear you can refer to the...
18
by: /* frank */ | last post by:
My teacher said that array in C is managed by preprocessor. Preprocesser replace all array occurences (i.e. int a ) with something that I don't understand/remember well. What's exactly happens...
6
by: Merrill & Michele | last post by:
I know that for my own programs, the things that precede the main call are either remarks or begin with a hash. What else can you put up there? MPJ
13
by: Chris Croughton | last post by:
Is the following code standard-compliant, and if so what should it do? And where in the standard defines the behaviour? #include <stdio.h> #define DEF defined XXX int main(void) { int...
9
by: Walter Roberson | last post by:
I have run into a peculiarity with SGI's C compiler (7.3.1.2m). I have been reading carefully over the ANSI X3.159-1989 specification, but I cannot seem to find a justification for the behaviour....
2
by: Paolo | last post by:
I imported a VC++6.0 project into VC++7.1. The conversion operation makes a mess with Preprocessor Definitions, adding a "$(NoInherit)" for each file. For example: I had a DLL project in VC++6.0...
32
by: spibou | last post by:
Is the output of the C preprocessor deterministic ? What I mean by that is , given 2 compilers which conform to the same standard, will their preprocessors produce identical output given as input...
31
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.