You can define your own names for constants that you use very often without having to resort to memory-consuming variables, simply by using the #definepreprocessor directive. Its format is:
#define identifier value
For example:
This defines two new constants: PI and NEWLINE. Once they are defined, you can use them in the rest of the code
as if they were any other regular constant, for example:
In fact the only thing that the compiler preprocessor does when it encounters #define directives is to literally
replace any occurrence of their identifier (in the previous example, these were PIand NEWLINE) by the code to
which they have been defined (3.14159and '\n'respectively).
The #definedirective is not a C++ statement but a directive for the preprocessor; therefore it assumes the entire
line as the directive and does not require a semicolon (;) at its end. If you append a semicolon character (;) at the
end, it will also be appended in all occurrences within the body of the program that the pre \processor replaces.
#define identifier value
For example:
This defines two new constants: PI and NEWLINE. Once they are defined, you can use them in the rest of the code
as if they were any other regular constant, for example:
In fact the only thing that the compiler preprocessor does when it encounters #define directives is to literally
replace any occurrence of their identifier (in the previous example, these were PIand NEWLINE) by the code to
which they have been defined (3.14159and '\n'respectively).
The #definedirective is not a C++ statement but a directive for the preprocessor; therefore it assumes the entire
line as the directive and does not require a semicolon (;) at its end. If you append a semicolon character (;) at the
end, it will also be appended in all occurrences within the body of the program that the pre \processor replaces.
No comments:
Post a Comment