Programming Reference/Librarys
Question & Answer
Q&A is closed
/* define a symbolic constant */ #define identifier replacement list /* define a Macro */ #define identifier (identifier_list) replacement list
The #define directive replaces any additional occurrences of identifier in the source code by the specified list as a replacement string. This replacement affects only terminal symbols (ie independent occurrence of identifier in the source code), but not strings that appear in a comment section of another identifier or part of a string constants. substitute list is a sequence of symbols, it can use reserved words, constants, and complete instructions be included. replacement list must be separated by one or more “white” spaces (whitespaces) of identifier. Spaces, which were preceded by replacement list are, like trailing spaces of the replacement text. If one behind identifier identifier_list appear replaced the #define directive of any occurrence of identifier (identifier_list) with a version of replacement list, in the listed parameters are adapted to the current variable names: The identifiers in this case represents a parameter list for the macro
When a macro with parameters exists, other occurrences of the identifier used to be interpreted along with an appropriate identifier list as a macro call, which leads to a corresponding extension (ie the insertion of the replacement list in the source code). The preprocessor fit for such extensions to those used in the replacement list of identifiers: each parameter from this list, which is not a string (#) sign (#@) or symbol operator (##) is preceded and no symbol operator followed by the replaced macro call variable names used. If the macro call in turn macro identifier as a parameter, then these macros are expanded before being inserted into the replacement list. Parameter name in the replacement list are placeholders for the to be used in an extension of the macro current identifiers and can be replaced without any restrictions by the context: A parameter name may appear several times as needed in the replacement list, a particular order is required only by the logic of the macro itself .
Since macros as opposed to functions can not work with a variable number of parameters, a macro call must always have as many actual parameters, as it includes the replacement list. Especially with complex parameters should be provided with appropriate brackets ensure that no misunderstandings regarding the evaluation and grouping of the individual expressions arise.
Recursive find extensions for parameter name does not occur: If the definition of a macro identifier xyz xyz in the replacement list, this identifier will not be extended again - not even when the string xyz is only through the expansion of another macro.
The parameters in the identifier list of a macro definition must be separated by commas and within that list each unique. Example:
#define MIN (x, y) ((x <y) x: y)
The scope of the identifier used in macro definitions extends only to the particular definition, ie it ends with the replacement list.
Between the identifier of the macro and the opening brace must be no blank. Example:
#define SQR(a) (a * a) / * correct * / #define SQR (a) (a * a) / * leads to syntax error * /
Macro definitions can span multiple lines. The unity of the line would have to be displayed at the end of a line continue by a backslash \ (backslash). Example:
#define LONGTEXT "This is a long text to" \ "Continued the next line is."
#include <stdio.h> /* including standard library */ //#include <windows.h> /* uncomment this for Windows */ #define ANSWER 42 int main(void){ printf("the answer to life the universe and everything is %d \n", ANSWER ); }
user@host:~$ ./define the answer to life the universe and everything is 42
#include <stdio.h> /* including standard library */ //#include <windows.h> /* uncomment this for Windows */ #define ANSWER 42 int print ( void ) { printf("the answer to life the universe and everything is %d \n", ANSWER ); return 0; } int main(void) { print(); return 0; }
user@host:~$ ./define the answer to life the universe and everything is 42
#include <stdio.h> /* including standard library */ //#include <windows.h> /* uncomment this for Windows */ #define A (10 + 20 + 2) #define B A + 10 #define TEXT "the answer to life the universe and averything is" #define NEWLINE '\n' #define ANSWER printf("%s %i%c",TEXT , B, NEWLINE); /** A = (10 + 20 + 2) B = (10 + 20 + 2) + 10 TEXT = the answer to life the universe and averything is (Char Array) NEWLINE = \n (Single Char) ANSWER = printf("%s %i%c", TEXT, B , NEWLINE); so the programm will use the macro ANSWER */ int main(void) { ANSWER return 0; }
user@host:~$ ./define the answer to life the universe and everything is 42