Conditional compilation you can include or exclude

Info icon This preview shows pages 10–12. Sign up to view the full content.

View Full Document Right Arrow Icon
Conditional compilation. You can include or exclude parts of the program according to various conditions. Line control. If you use a program to combine or rearrange source files into an inter- mediate file which is then compiled, you can use line control to inform the compiler where each source line originally came from. Diagnostics. You can detect problems at compile time and issue errors or warnings. There are a few more, less useful, features. Except for expansion of predefined macros, all these operations are triggered with pre- processing directives . Preprocessing directives are lines in your program that start with
Image of page 10

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

View Full Document Right Arrow Icon
Chapter 2: Header Files 7 # ’. Whitespace is allowed before and after the ‘ # ’. The ‘ # ’ is followed by an identifier, the directive name . It specifies the operation to perform. Directives are commonly referred to as ‘ # name ’ where name is the directive name. For example, ‘ #define ’ is the directive that defines a macro. The ‘ # ’ which begins a directive cannot come from a macro expansion. Also, the directive name is not macro expanded. Thus, if foo is defined as a macro expanding to define , that does not make ‘ #foo ’ a valid preprocessing directive. The set of valid directive names is fixed. Programs cannot define new preprocessing directives. Some directives require arguments; these make up the rest of the directive line and must be separated from the directive name by whitespace. For example, ‘ #define ’ must be followed by a macro name and the intended expansion of the macro. A preprocessing directive cannot cover more than one line. The line may, however, be continued with backslash-newline, or by a block comment which extends past the end of the line. In either case, when the directive is processed, the continuations have already been merged with the first line to make one long line. 2 Header Files A header file is a file containing C declarations and macro definitions (see Chapter 3 [Macros], page 13 ) to be shared between several source files. You request the use of a header file in your program by including it, with the C preprocessing directive ‘ #include ’. Header files serve two purposes. System header files declare the interfaces to parts of the operating system. You include them in your program to supply the definitions and declarations you need to invoke system calls and libraries. Your own header files contain declarations for interfaces between the source files of your program. Each time you have a group of related declarations and macro definitions all or most of which are needed in several different source files, it is a good idea to create a header file for them. Including a header file produces the same results as copying the header file into each source file that needs it. Such copying would be time-consuming and error-prone. With a header file, the related declarations appear in only one place. If they need to be changed, they can be changed in one place, and programs that include the header file will automat- ically use the new version when next recompiled. The header file eliminates the labor of
Image of page 11
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern