Macros in C Programming (Understanding and Its Functions)


What are macros?
What are macros?

Macros in C Programming – C programming language is a high-level programming language that is commonly used in everyday life. Why is knowing about macros important? This is because so that you can understand and understand the process behind the c program code before compiling.

what are macros
what are macros

In general, a macro is something that is comprehensive and on a large scale. Then what is the meaning of Macro in the programming world? In the world of programming, macros are codes that give instructions to the preprocessor. What is a preprocessor? preprocessor is a process that is carried out before the program is compiled by the given macro. The output produced by the preprocessor is in the form of program code, not a binary file.

Types of macros and their functions

There are various types of Macro directives which of course have different functions, including:

  • The macro ‘#define’ is used to describe constants and their functions.
  • The ‘#include’ macro is used to add code to the program.
  • The ‘#ifdef’ macro is used to check if the macro has been defined.
  • Macro ‘#ifndef’ is used to check that the macro has not been defined.
  • Macro ‘#undef’ function to delete macros that have been defined.
  • The ‘#if’ macro is used to create an if condition.
  • The ‘#else’ macro is used to create an alternative to the if condition.
  • The ‘#elif’ macro is used to create else and if conditions.
  • The ‘#endif’ macro is used to stop or terminate the if condition.
  • The ‘#error’ macro is used to print an error message to stderr.
  • Macro ‘#pragma’ serves to give instructions to the compiler

Macro directives are symbols used to create and define macros.

Operators in Macro

  1. Stringize operator (#), this operator uses the fence symbol (#) which functions to change the parameters in the macro into a text string.
  2. Connection Operator (\), this operator uses the backslash symbol (\). The function of this operator is to connect macros if more than one line.
  3. Token Pasting Operator (‘##’) , this operator uses the double fence symbol. The function of this operator is to take the name of the variable or parameter given to the macro.
  4. The defined() operator is used to check the condition of the macro whether it has been defined or not.

For additional information, macros are processed by the preprocessor, not by the compiler. So first the information that this article can provide, hopefully it will be useful and thank you