Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using a macro to create a loop in C

Tags:

c

macros

While looking on some legacy code, I found a rather unusual construction (for me at least):

#define loop(i,start,stop) for((i)=(start);(i)<(stop);(i)++)

This macro is then used everywhere instead of regular for loops construction.

I think it's a bad idea in general because it does not really solve a problem nor simplify anything, but can it be dangerous? By dangerous I mean breaking compilation (best case) or (much worse but more interesting) do something else than expected.

like image 672
ascobol Avatar asked Jan 19 '23 07:01

ascobol


1 Answers

The standard cautionary tale against macros is arguments with side effects:

loop(i, x, y++)
like image 51
Marcelo Cantos Avatar answered Jan 29 '23 06:01

Marcelo Cantos