lookiconnector.blogg.se

Macro in c
Macro in c







macro in c

Whenever the C preprocessor sees SQUARE(x) in the program source code, it replaces it with ( x * x). The macro template takes argument x which is a value of the appropriate type( integer for multiplication). But there is a big difference between complex macro and a C function. The complex macros take arguments like functions and it looks like a function. The next section is about complex macros that do exactly the same. It is not a good practice to use large piece codes for the macro expansion.

macro in c

Macro in c code#

You can use operators, replace conditions or execute any code, the preprocessor will replace it into a source code (macro expansion). In other words, all elements of a program are suitable for macro expansion.

macro in c

In the above program, we defined three different types of macro templates – operators, conditions, and statements. #define STATEMENT2 printf("q is greater than 100\n") //print statements #define STATEMENT1 printf("p is greater than 10\n") The following program that demonstrates other possible usage of a #define directive. The #define directive is not only used to label a constant, but also other types of statements. The macro on the other hand only requires you to change the value of the #define directive.Īnother advantage of macro expansion is that it is much faster than the variables or constants. To change the constant, you must change it in all place in source code individually. The #define directive is a good way of declaring constant compared to a constant or a variable value. It replaces the macro template with the value of macro expansion. When the program run and if the C preprocessor sees an instance of a macro within the program code, it will do the macro expansion.









Macro in c