Preprocessor directives

  • Sentences that begin with a pound sign (#) are directives for the preprocessor.
  • They are not executable code lines but indications for the compiler.
  • The preprocessor is executed automatically by the compiler when we compile a program in C++ and is in charge of making the first verifications and digestions of the program’s code.
  • All these directives must be specified in a single line of code and they do not have to include an ending semicolon:
  • #define
  • the #define directive defines 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 referred to as a macro name and the replacement process as macro replacement.
  • The general form of the directive is
  • #define name value #define MAX_WIDTH 100 char str1[MAX_WIDTH]; char str2[MAX_WIDTH];
  • It defines two strings to store up to 100 characters
  • #define can also be used to generate macro functions:
  • #define getmax(a,b) a>b?a:b
  • int x=5, y;
  • y = getmax(x,2);
  • after the execution of this code y would contain 5
  • #undef
  • #undef fulfills the inverse functionality of #define. It eliminates from the list of defined constants the one that has the name passed as a parameter to #undef #define MAX_WIDTH 100 char str1[MAX_WIDTH] #undef MAX_WIDTH #define MAX_WIDTH 200 char str2[MAX_WIDTH];
  • #ifdef, #ifndef, #if, #endif, #else and #eIif These directives allow discarding part of the code of a program if a certain condition is not fulfilled
  • #ifdef allows that a section of a program is compiled only if the defined constant that is specified as the parameter has been defined, independently of its value.
  • In the case given below, the line char str [MAX_WIDTH]; is only considered by the compiler if the defined constant MAX_WIDTH has been previously defined, independently of its value. If it has not been defined, that line will not be included in the program.
  • #ifndef serves for the opposite: the code between the #ifndef directive and the #endif directive is only compiled if the constant name that is specified has not been defined previously.
  • For example: #ifndef MAX WIDTH
  • #define MAX_WIDTH 100
  • #endif
  • char str [MAX_WIDTH];
  • In this case, if when arriving at this piece of code the defined constant MAX_WIDTH has not yet been defined it would be defined with a value of 100. If it already existed it would maintain the value that it had (because the #define statement won't be executed).
  • The #if, #else and #elif (elif = else if) directives serve so that the portion of code that follows is compiled only if the specified condition is met. The condition can only serve to evaluate constant expressions.
  • For example:
    #if MAX_WIDTH>200
    #undef MAX_WIDTH 
    #define MAX_WIDTH 200
    #elsif MAX WIDTH<50 
    #undef MAX_WIDTH 
    #define MAX _WIDTH 50 
    #else
    #undef MAX_WIDTH 
    #define MAX_WIDTH 100 
    #endif
    char str [MAX_WIDTH];
    
  • Notice how the structure of chained directives #if, #elsif and #else finishes with #endif.
  • #include
  • When the preprocessor finds an #include directive it replaces it by the whole content of the specified file. There are two ways to specify a file to be included:
  • #include " file "
  • #include < file >
  • The only difference between both expressions is the directories in which the compiler is going to look for the file. In the first case where the file is specified between quotes, the file is looked for in the same directory that includes the file containing the directive. In case that it is not there, the compiler looks for the file in the default directories where it is configured to look for the standard header files.
  • If the file name is enclosed between angle-brackets <>the file is looked for directly where the compiler is configured to look for the standard header files.


Structure << Previous

Next >> Class and Objects

Our aim is to provide information to the knowledge seekers. 


comments powered by Disqus




Footer1