Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a define in C?

Is it possible to write a #define that defines a #define?

For example:

#define FID_STRS(x) #x
#define FID_STRE(x) FID_STRS(x)
#define FID_DECL(n, v) static int FIDN_##n = v;static const char *FIDS_##n = FID_STRE(v)

But instead:

#define FID_DECL2(n, v) #define FIDN_##n v \
                               FIDS_##n FID_STRE(v)

FID_DECL works fine but creates two static variables. Is it possible to make FID_DECL2 work and having define two defines?

like image 681
Elias Bachaalany Avatar asked Feb 28 '11 15:02

Elias Bachaalany


People also ask

How do you define #define in C?

#define is a preprocessor directive that is used to define macros in a C program. #define is also known as a macros directive. #define directive is used to declare some constant values or an expression with a name that can be used throughout our C program.

Where to put #define in C?

Put them where you need them. If you need it only for one file then put it at the top of that file. If you need it for multiple files then put it in a header file. And if defines are needed only in one function: inside the function or at the top of the file?

How do you print #define value in C?

printf("%d", 5);

How do you define a constant in C using #define?

Variables can be declared as constants by using the “const” keyword before the datatype of the variable. The constant variables can be initialized once only. The default value of constant variables are zero. A program that demonstrates the declaration of constant variables in C using const keyword is given as follows.


1 Answers

No; preprocessing is performed in a single pass. If you want or need more advanced behavior, consider using another tool to preprocess the source, like m4.

Further, the # in the replacement list (at the beginning of #define FIDN... would be parsed as the # (stringize) operator: the operand of this operator must be a named macro parameter, which define is not.

like image 68
James McNellis Avatar answered Oct 06 '22 20:10

James McNellis