What are Macros and how to use them?

6

So far during my C programming course, the only contact I had with macros came from standard libraries and I did not know very well what happened when I called these macros. I would like to know what exactly macros are, how to create one in C / C ++ and the difference between macro, function, and constant. Thank you.

    
asked by anonymous 09.06.2015 / 00:06

1 answer

5

Macros or Microinstructions is a precompilation (preprocessing) feature that allows you to create structures that will be replaced before the code is compiled.

A Macro may represent a simple string that will repeat in quantity in the code, but may also represent a relatively complex code block or within an application domain, but repeated several times in the code, and the call would be inelegant.

In case of code macros, they can be used to create language dialects with C or C ++ for specific domain, so you can adopt names of mathematical constants, named code blocks that are better organized and readable for domain specialists in use.

Macros can also be used to identify constants that will be used to define architectures, by restructuring the code by creating a conditional compilation.

Code files are interpreted by a precompiler that replaces such macros with their representations, generating a new code file that will be compiled.

When a Macro calls other macros, it is important to take certain precautions as suggested in C-Preprocessor Tricks, Tips and Idioms .

More information on C PreProcessing

    
09.06.2015 / 02:38