Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expressions in C preprocessor macro

Tags:

c

regex

macros

I would like to know if there is any kind of regular expression expansion within the compiler(GCC) pre processor. Basically more flexible code generation macros.

If there is not a way, how do you suggest i accomplish the same result

like image 977
Paulo Neves Avatar asked Oct 08 '11 14:10

Paulo Neves


People also ask

What is a macro expression in C?

A macro is a fragment of code that is given a name. You can define a macro in C using the #define preprocessor directive. Here's an example. #define c 299792458 // speed of light. Here, when we use c in our program, it is replaced with 299792458 .

What is regular expression in C?

A regular expression is a sequence of characters that is used to search pattern. It is mainly used for pattern matching with strings, or string matching, etc. They are a generalized way to match patterns with sequences of characters. It is used in every programming language like C++, Java, and Python.

What is preprocessor in C with example?

The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation (Proprocessor direcives are executed before compilation.). It is called a macro processor because it allows you to define macros, which are brief abbreviations for longer constructs.

What is preprocessor in C language?

The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation. It is called a macro processor because it allows you to define macros, which are brief abbreviations for longer constructs.


2 Answers

The C preprocessor can't do that.

You might want to use a template processor (for instance Mustache but there are many others) that generates what you need before passing it to the compiler.

like image 68
Gregory Pakosz Avatar answered Sep 21 '22 07:09

Gregory Pakosz


Also, if you are planning a bigger project and you know this feature will be beneficial you might want to write your own preprocessor that you can run automatically from some build system. Good example of such solution would be moc which enhances C++ for the purpose of Qt framework. Purist might of course disagree.

like image 29
RushPL Avatar answered Sep 20 '22 07:09

RushPL