APPENDIXA - Appendix A The Preprocessor The preprocessor is...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 C++ A Beginner’s Guide by Herbert Schildt Appendix A The Preprocessor The preprocessor is that part of the compiler that performs various text manipulations on your program prior to the actual translation of your source code into object code. You can give text manipulation commands to the preprocessor. These commands are called preprocessor directives, and although not technically part of the C++ language, they expand the scope of its programming environment. The preprocessor is a holdover from C and is not as important to C++ as it is to C. Also, some preprocessor features have been rendered redundant by newer and better C++ language elements. However, since many programmers still use the preprocessor, and because it is still part of the C++ language environment, it is briefly discussed here. The C++ preprocessor contains the following directives: As is apparent, all preprocessor directives begin with a # sign. Each will be examined here in turn. #define #define is used to define an identifier and a character sequence that will be substituted for the identifier each time it is encountered in the source file. The identifier is called a macro name, and the replacement process is called macro substitution. The general form of the directive is #define macro-name character-sequence Notice that there is no semicolon in this statement. There can be any number of spaces between the identifier and the start of the character sequence, but once the sequence begins, it is terminated only by a newline. For example, if you wanted to use the word “UP” for the value 1 and the word “DOWN” for the value 0, you would declare these two #defines: #define UP 1 #define DOWN 0 These statements will cause the compiler to substitute a 1 or a 0 each time the name UP or DOWN is encountered in your source file. For example, the following will print 102 on the screen:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 C++ A Beginner’s Guide by Herbert Schildt It is important to understand that the macro substitution is simply the replacing of an identifier with its associated string. Therefore, if you want to define a standard message, you might write something like this: #define GETFILE "Enter File Name" // . .. cout << GETFILE; C++ will substitute the string “Enter File Name” when the macro name GETFILE is encountered. To the compiler, the cout statement will actually appear to be cout << "Enter File Name"; No text substitutions will occur if the macro name occurs within a quoted string. For example, #define GETFILE "Enter File Name" // . .. cout << "GETFILE is a macro name\n"; will not print Enter File Name is a macro name but rather will print GETFILE is a macro name If the string is longer than one line, you can continue it on the next by placing a backslash at the end of the line, as shown in this example: #define LONG_STRING "this is a very long \ string that is used as an example" It is common practice among C++ programmers to use capital letters for macro names. This convention
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/10/2011 for the course GEOL 4330 taught by Professor Chesnokov during the Spring '11 term at University of Houston - Downtown.

Page1 / 11

APPENDIXA - Appendix A The Preprocessor The preprocessor is...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online