473,503 Members | 241 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how do I let #define defines a macro

Hello..

a header file of one of my project has macro definitions like this
(for example)

#define PART_A_SORT_1_LABEL_STRING1 100
#define PART_A_SORT_1_LABEL_STRING2 200
#define PART_A_SORT_1_LABEL_STRING3 300
#define PART_A_SORT_1_LABEL_STRING4 400
#define PART_A_SORT_1_LABEL_STRING5 500
....

#define PART_A_SORT_7_LABEL_STRING1 1100
#define PART_A_SORT_7_LABEL_STRING2 3200
#define PART_A_SORT_7_LABEL_STRING3 2300
#define PART_A_SORT_7_LABEL_STRING4 1400
#define PART_A_SORT_7_LABEL_STRING5 6500

so, I tried to make a Macro defines another macros like this.

#define MY_DEFINE(PART_SORT, C1, C2, C3, C4, C5) \
#define PART_SORT##_LABEL_STRING1 C1 \
#define PART_SORT##_LABEL_STRING2 C2 \
#define PART_SORT##_LABEL_STRING3 C3 \
#define PART_SORT##_LABEL_STRING4 C4 \
#define PART_SORT##_LABEL_STRING5 C5

yeah, I thought the first ugly, big bulk of definitions will be
compacted like this.

MY_DEFINE(PART_A_SORT_1, 100, 200, 300, 400, 500)
....
MY_DEFINE(PART_A_SORT_7, 1100, 3200, 2300, 1400, 6500)
but I found #define treats '#' and '##' special. ## concats, # makes
string.

Now I guess you know what I'm trying to do.

Is there WAY to achieve this? or
REASON why I cannot?

Reguards.

Oct 1 '07 #1
2 2279
>#define MY_DEFINE(PART_SORT, C1, C2, C3, C4, C5) \
>#define PART_SORT##_LABEL_STRING1 C1 \
#define PART_SORT##_LABEL_STRING2 C2 \
#define PART_SORT##_LABEL_STRING3 C3 \
#define PART_SORT##_LABEL_STRING4 C4 \
#define PART_SORT##_LABEL_STRING5 C5

yeah, I thought the first ugly, big bulk of definitions will be
compacted like this.

MY_DEFINE(PART_A_SORT_1, 100, 200, 300, 400, 500)
...
MY_DEFINE(PART_A_SORT_7, 1100, 3200, 2300, 1400, 6500)
but I found #define treats '#' and '##' special. ## concats, # makes
string.
The expansion of a macro may not generate a preprocessor directive
even if the expansion looks like one.
>Now I guess you know what I'm trying to do.

Is there WAY to achieve this? or
No - not in normal C.
>REASON why I cannot?
Once you generate the preprocessor directive using the preprocessor,
it's too late to interpret it as a preprocessor directive. Unless
you manage to run things through the preprocessor TWICE. This is
likely to cause other problems and there's no guarantee that the
preprocessor is a separate program that can accept its own output
as input. In actual practice you can probably get away with it.

If you want to pre-process C files before compiling them, there are
better macro languages than the C preprocessor. m4 might be better
for what you want. This, of course, requires some adjustment in
your build procedure. Some programs used to process input to produce
C code (such as the Oracle Pro*C compiler, yacc, and early versions
of C++) can do some really exotic things to the source code.

Oct 1 '07 #2
On 10 1 , 2 24 , gordonb.uz...@burditt.org (Gordon Burditt) wrote:
#define MY_DEFINE(PART_SORT, C1, C2, C3, C4, C5) \
#define PART_SORT##_LABEL_STRING1 C1 \
#define PART_SORT##_LABEL_STRING2 C2 \
#define PART_SORT##_LABEL_STRING3 C3 \
#define PART_SORT##_LABEL_STRING4 C4 \
#define PART_SORT##_LABEL_STRING5 C5
yeah, I thought the first ugly, big bulk of definitions will be
compacted like this.
MY_DEFINE(PART_A_SORT_1, 100, 200, 300, 400, 500)
...
MY_DEFINE(PART_A_SORT_7, 1100, 3200, 2300, 1400, 6500)
but I found #define treats '#' and '##' special. ## concats, # makes
string.

The expansion of a macro may not generate a preprocessor directive
even if the expansion looks like one.
Now I guess you know what I'm trying to do.
Is there WAY to achieve this? or

No - not in normal C.
REASON why I cannot?

Once you generate the preprocessor directive using the preprocessor,
it's too late to interpret it as a preprocessor directive. Unless
you manage to run things through the preprocessor TWICE. This is
likely to cause other problems and there's no guarantee that the
preprocessor is a separate program that can accept its own output
as input. In actual practice you can probably get away with it.

If you want to pre-process C files before compiling them, there are
better macro languages than the C preprocessor. m4 might be better
for what you want. This, of course, requires some adjustment in
your build procedure. Some programs used to process input to produce
C code (such as the Oracle Pro*C compiler, yacc, and early versions
of C++) can do some really exotic things to the source code.- -

- -
Yes, that's it. So it was..
I can clearly understand now.
Thank you.

Oct 1 '07 #3

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

Similar topics

97
27692
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
2
5523
by: | last post by:
What is the scope of #define statement? Say we have an inlude file as follows ---MyInclude1.h--- #ifndef MYINCLUDE1_H #define MYINCLUDE1_H #define EXAMPLE1 1000 #define EXAMPLE2 2000...
18
8899
by: Bryan Parkoff | last post by:
"#define" can only be inside the global scope before main() function. "#if" can be tested after "#define" is executed. The problem is that "#define" can't be inside main() function. I do not wish...
7
2200
by: JustBoo | last post by:
Whilst reading through the C++ FAQ. I came across Section 29.8 and realized I never had fully thought about where a #define resided in a Standard C++ program. I thought they were a compile-time...
3
3073
by: iler.ml | last post by:
I am writing code that uses two third-party libraries. They both define same macro OP_ENCRYPT, and luckily for me, they both define it to 0. (In one include, it's '#define OP_ENCRYPT 0', in...
19
2398
by: Sensei | last post by:
Hi! I'm concerned about the legality of such a definition: #define funcX funcY where funcX belongs to the *standard* C functions. Is it legal to do this? The standard says "any function...
17
2755
by: niraj.tiwari | last post by:
What is meaning of the following define:- #define x(argl...) x1(##argl)
4
2413
by: Mohammad Omer Nasir | last post by:
I was read Linux kernel code in which I saw few define macro defines in functions. The snap of the function is following and file name "err_ev6.c". static int ev6_parse_cbox(u64 c_addr, u64...
14
2417
by: raghu | last post by:
Hello I have a doubt plz clarify that #ifndef SYSTEM_H #define SYSTEM_H what will be the value of SYATEM_H after this #define statement and before that statement. Thanking you all Bye
0
7089
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
7339
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...
0
7463
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5581
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5017
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3157
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
738
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.