The #define directive defines an identifier and a string that will be substituted for the identifier each time the identifier is encountered in the source file. The ANSI C standard refers to the identifier s the macro name and to the replacement process as macro substitution. The general form of the directive is
#define identifier string
If this statement is included at the beginning of the program, then the preprocessor replaces every occurrence of the identifier by the string. The most two common forms of macro substitution are :
Both definations define a macro and some replacement string, which will be used to replace later occurrences of the macro name in the rest of the program.
For the simple macro, its name is simply replaced by its replacement string.
The argumented macro is also replaced by its replacement string; wherever the replacement string contains an identifier which is the name of one of the macro’s formal parameters, the actual string given as the argument is used in place of the identifier in the replacement string. The scope of the names of the formal parameters is limited to the body of the #define directive. For both forms of macro, leading or trailing white space around the replacement text is discarded.
Simple string replacement is commonly used to define constants. For example,
#define TRUE 1
#define Pl 3.1415
When the identifier associated with a simple macro is seen, it is replaced by the macro replacement tokens, and then rescanned to make for further replacements.
C programmer commonly use uppercase letters for defined identifiers. This convention helps anyone reading the program known at a glance that a macro substitution will take place. Also it is best to put all #defines at the start of the file or separate include file rather than sprinkling them throughout the program.
The preprocessor permits to define more complex and more useful form of replacements. The general form of this macro is
#define identifier(arg 1, arg 2,…,argn) string
Where arg1, arg2, …, argn are formal macro arguments that are analogous to formal parameters in function definition. Notice that there is no space in between the identifier and the left parathesis. When there will be the occurrences of macro with argument in the program, the preprocessor substitutes the string by replacing the formal parameter with the actual parameters.
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc