Preprocessor Directives- Computer Programming - Lecture Notes, Study notes of Computer Engineering and Programming

Preprocessor Directives, Conditional Compilation, Preprocessor Operators, Macros, Standard Predefined Macros, Turning debugging code off and on, Prevent multiple definitions in header files. As you can see in this file, how descriptive above mentioned points are explained in this lecture of computer programming. VU is one of best university for computer science in our country.

Typology: Study notes

2011/2012

Uploaded on 11/06/2012

ahsen
ahsen 🇵🇰

4.6

(88)

84 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 5
Preprocessor.....................................................................................................2
Preprocessor directives: #ifdef and #ifndef....................................................2
Prevent multiple definitions in header files.....................................................2
Turning debugging code off and on ............................................................... 2
Some Preprocessor directives...........................................................................3
#define.................................................................................................... 3
Example: macro #defines ..............................................................................3
#error......................................................................................................3
#include .................................................................................................. 3
Conditional Compilation - #if, #else, #elif, and #endif........................................ 4
#ifdef and #ifndef ....................................................................................4
#undef.....................................................................................................4
#line........................................................................................................4
#pragma .................................................................................................4
The # and ## Preprocessor Operators ..............................................................5
Macros ..............................................................................................................5
Standard Predefined Macros .........................................................................5
__FILE__ ................................................................................................6
__LINE__................................................................................................6
__DATE__ ..............................................................................................6
__TIME__ ...............................................................................................6
__STDC__.............................................................................................. 6
__STDC_VERSION__............................................................................ 7
__STDC_HOSTED__.............................................................................7
__cplusplus.............................................................................................7
Summary...........................................................................................................7
Tips ...................................................................................................................7
pf3
pf4
pf5
pf8

Partial preview of the text

Download Preprocessor Directives- Computer Programming - Lecture Notes and more Study notes Computer Engineering and Programming in PDF only on Docsity!

 - Chapter 
  • Preprocessor .....................................................................................................
    • Preprocessor directives: #ifdef and #ifndef ....................................................
    • Prevent multiple definitions in header files.....................................................
    • Turning debugging code off and on ...............................................................
  • Some Preprocessor directives........................................................................... - #define....................................................................................................
    • Example: macro #defines ..............................................................................
      • #error ......................................................................................................
      • #include ..................................................................................................
  • Conditional Compilation - #if, #else, #elif, and #endif........................................ - #ifdef and #ifndef .................................................................................... - #undef..................................................................................................... - #line ........................................................................................................ - #pragma .................................................................................................
  • The # and ## Preprocessor Operators ..............................................................
  • Macros ..............................................................................................................
    • Standard Predefined Macros .........................................................................
      • FILE ................................................................................................
      • LINE................................................................................................
      • DATE ..............................................................................................
      • TIME ...............................................................................................
      • STDC..............................................................................................
      • STDC_VERSION............................................................................
      • STDC_HOSTED .............................................................................
      • __cplusplus.............................................................................................
  • Summary...........................................................................................................
  • Tips ...................................................................................................................

Preprocessor

The preprocessor is a program that runs prior to compilation and potentially modifies a source code file. It may add code in response to the #include directive, conditionally include code in response to #if, #ifdef, #ifndef directives or define constants using the #define directive. As defined by the ANSI standard, the C preprocessor contains the following directives:

#if #ifdef #ifndef #else #elif #include #define #undef #line #error #pragma

Preprocessor directives: #ifdef and #ifndef

The #ifdef (if defined) and #ifndef (if not defined) preprocessor commands are used to test if a preprocessor variable has been "defined".

Prevent multiple definitions in header files

When there are definitions in a header file that can not be made twice, the code below should be used. A header file may be included twice because more than one other “include file” includes it, or an included file includes it and the source file includes it again.

To prevent bad effects from a double include, it is common to surround the body in the include file with the following:

#ifndef MYHEADERFILE_H #define MYHEADERFILE_H

... // This will be seen by the compiler only once #endif / MYHEADERFILE_H /

Turning debugging code off and on

Debugging code is necessary in programs; however, it is not usually appropriate to leave it in the delivered code. The preprocessor #ifdef command can surround the debugging code. If DEBUG is defined as below (probably in an include file) all debugging statement surrounded by the #ifdef DEBUG statement will be active. However, if it isn't defined, none of the statements will make it through the preprocessor.

#define DEBUG

... #ifdef DEBUG ... // debugging output #endif

in an implementation-defined manner, which generally means searching the current directory. (If the file is not found, the search is repeated as if the name had been enclosed in angle brackets.)

Conditional Compilation - #if, #else, #elif, and

#endif

Several directives control the selective compilation of portions of the program code, viz, #if, #else, #elif, and #endif.

The general form of #if is: #if constant_expression statement sequence #endif

#else works much like the C keyword else. #elif means "else if" and establishes an if- else-if compilation chain.

Amongst other things, #if provides an alternative method of "commenting out" code. For example, in #if 0 printf("#d",total); #endif

the compiler will ignore printf("#d",total);.

  • #ifdef and #ifndef

#ifdef means "if defined", and is terminated by an #endif. #ifndef means "if not defined".

  • #undef

#undef removes a previously defined definition.

  • #line

line changes the contents of LINE (which contains the line number of the currently compiled code) and FILE (which is a string which contains the name of the source file being compiled), both of which are predefined identifiers in the compiler.

  • #pragma

The #pragma directive is an implementation-defined directive which allows various instructions to be given to the compiler i.e. it allows a directive to be defined. The #pragma directive is the method specified by the C standard for providing additional information to the compiler, beyond what is conveyed in the language itself. Three forms of this directive (commonly known as pragmas) are specified by the 1999 C standard. A C compiler is free to attach any meaning it likes to other pragmas.

The # and ## Preprocessor Operators

The # and ## preprocessor operators are used when using a macro #define. The # operator turns the argument it precedes into a quoted string. For example, given:

#define mkstr(s) # s

the preprocessor turns the line printf(mkstr(I like C); into printf("I like C"); The ## operator concatenates two tokens. For example, given : #define concat(a, b) a ## b

int xy=10; printf("%d",concat(x, y); the preprocessor turns the last line into: printf("%d", xy);

Macros

A macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro. There are two kinds of macros. They differ mostly in what they look like when they are used. Object-like macros resemble data objects when used, function-like macros resemble function calls.

You may define any valid identifier as a macro, even if it is a C keyword. The preprocessor does not know anything about keywords. This can be useful if you wish to hide a keyword such as const from an older compiler that does not understand it. However, the preprocessor operator defined can never be defined as a macro, and C++'s named operators cannot be macros when you are compiling C++.

To define a macro that takes arguments, you use the #define command with a list of parameters in parentheses after the name of the macro. The parameters may be any valid C identifiers separated by commas at the top level (that is, commas that aren't within parentheses) and, optionally, by white-space characters. The left parenthesis must follow the macro name immediately, with no space in between.

For example, here's a macro that computes the maximum of two numeric values: #define min(X, Y) ((X)>(Y)? (X):(Y))

Standard Predefined Macros

The standard predefined macros are specified by the C and/or C++ language standards, so they are available with all compilers that implement those standards. Older compilers may not provide all of them. Their names all start with double underscores.

STDC_VERSION

This macro expands to the C Standard's version number, a long integer constant of the form yyyymmL where yyyy and mm are the year and month of the Standard version. This signifies which version of the C Standard the compiler conforms to.

This macro is not defined if the -traditional option is used, nor when compiling C++ or Objective-C.

STDC_HOSTED

This macro is defined, with value 1, if the compiler's target is a hosted environment. A hosted environment has the complete facilities of the standard C library available.

  • __cplusplus

This macro is defined when the C++ compiler is in use. You can use __cplusplus to test whether a header is compiled by a C compiler or a C++ compiler. This macro is similar to STDC_VERSION, in that it expands to a version number.

Tips

  • Do use the preprocessor directives as much as possible in your programme as it makes the programme more robust.
  • Using the #defined, #ifndef directives helps in Prevent multiple definitions in header files.
  • Conditional compilation with the use of preprocessor directives provides a very easy way for turning the debug code on and off.
  • A macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro.
  • Macros can be used for writing clear and easily comprehensible code.
  • Do remember that whenever the macro name is used, it is replaced by the contents of the macro.

Summary

The preprocessor is a program that runs prior to compilation and potentially modifies a source code file. It may add code in response to the #include directive, conditionally include code in response to #if, #ifdef, #ifndef directives or define constants using the #define directive.

A simple macro is a kind of abbreviation. It is a name which stands for a fragment of code. Some standard pre-defined Macros include FILE, LINE, DATE, TIME etc.