macros-loop? calling macros X times?

A

Andrew Arro

is it possible to make smth like a loop of macroses? i.e. i want some
macros to be called X times, all that on the PREPROCESSOR lever

i was trying smth like



#define vv_0 100
#define vv_1 101
#define vv_2 102
#define vv_3 103
#define vv_4 104

#define RUN(cur, max) \
#if cur<max \
printf("%d %d\n", cur, vv_ ## cur); \
RUN((cur+1), max) \
#endif


but seems like #if is not possible inside of a macros?
 
M

Martin Ambuhl

Andrew said:
is it possible to make smth like a loop of macroses? i.e. i want some
macros to be called X times, all that on the PREPROCESSOR lever

i was trying smth like



#define vv_0 100
#define vv_1 101
#define vv_2 102
#define vv_3 103
#define vv_4 104

#define RUN(cur, max) \
#if cur<max \
printf("%d %d\n", cur, vv_ ## cur); \
RUN((cur+1), max) \
^^ Are you sure you don't want a ';' here?
#endif


but seems like #if is not possible inside of a macros?

Why should you want it, anyway?

#define RUN(cur, max) do { \
if ( (cur) < (max)) { \
printf("%d %d\n", (cur), vv_ ## (cur)); \
RUN(((cur)+1), (max)); \
}
} while (0);

I'd worry about the possible side effects from multiple evaluations of
the arguments.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top