![]() ![]() In the following example, we define NUMBER_OF_PLAYERS to the constant "2". The directive #define can be used to define a macro. The only exception is when a macro is used to replace something that should have been a function but is implemented using a macro for the sake of efficiency. Whenever the preprocessor finds the macro in the application source code it replaces the macro with the definition.īasically, there are two types of macros, object-like macros and function-like macros, the difference is that function-like macros have parameters.īy convention, macro names are written using upper-case only. One of most useful features of the preprocessor is to allow the user to define macros, which simply is an identifier that is mapped to a piece of source code. The latter is for your own application-specific headers. The first is used to include standard headers like stdio.h. ![]() It can take two forms, for example: #include When the preprocessor finds this directive it simply opens the file specified and inserts the content of it, as though the content of the file would have been written at the location of the directive. The most straight-forward preprocessor directive is #include. If your source file contains any #include statements it is probably a good idea to send the output of gcc -E to a file so you can scroll down past the thousands of lines of text they may generate.ĥ.In this article we will introduce the preprocessor and covered the basics, including object- and function-like macros, the include directive, conditional compilation, and end with the two special directives #error and #pragma. You can tell gcc to tell you how everything expands using gcc -E source_file.c. One problem with using a lot of macros is that you can end up with no idea what input is actually fed to the compiler after the preprocessor is done with it. The macro _VA_ARGS_ then expands to whatever arguments matched this ellipsis in the macro call.ġ # include 2 3 /* note: must appear outside of any function, and has no trailing semicolon */ 4 DeclareSort( int, int) 5 6 int 7 main( int argc, char ** argv) 8 ĭo this too much and you will end up reinventing C++ templates, which are a more or less equivalent mechanism for generating polymorphic code that improve on C macros like the one above by letting you omit the backslashes. To define a variadic macro, define a macro with arguments where the last argument is three periods. by capitalization) to clearly distinguish them from function names, where this issue doesn't come up.Ĭ99 added variadic macros that may have a variable number of arguments these are mostly useful for dealing with variadic functions (like printf) that also take a variable number of arguments. For this reason it is generally best to avoid side-effects in macro arguments, and to mark macro names (e.g. ![]() For example, Square(++x) expands to ((++x)*(++x)) if x starts out equal to 1, this expression may evaluate to any of 2, 6, or 9 depending on when the ++ operators are evaluated, and will definitely leave 3 in x instead of the 2 the programmer probably expects. Macros can have odd effects if their arguments perform side-effects. The usual caveats about using lots of parentheses apply. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |