Passing the value of a variable to macro in C

前端 未结 3 1630
清酒与你
清酒与你 2020-12-20 18:11

I\'m trying to pass the value of a variable to a macro in C, but I don\'t know if this is possible. Example:

#include 

#define CONCVAR(_n) x          


        
3条回答
  •  臣服心动
    2020-12-20 18:40

    Substituting the value of i into the macro is impossible, since macro substitutions happen before your code is compiled. If you're using GCC, you can see the pre-processor output by adding the '-E' command line argument (Note however, that you'll see all the #include's inserted in your code.)

    C is a static language and you can not decide symbol names at runtime. However, what you're trying to achieve is possible if you use an array and refer to elements using subscripts. As a rule of thumb, if you have many variables like x0, x1, etc, you should probably be using a container like an array.

提交回复
热议问题