Preprocessor Directives

A preprocessor directive is run before the rest of the program is compiled. Lines that begin with a pound sign are preprocessor directives. In this lesson we will look at two of the most common preprocessor directives, #include and #define.

The #include directive tells the preprocessor to read in another file and include it in our program. A file name must always follow the #include directive. This file is called the header file. The header file name must be placed between angled brackets or quotation marks. Header files all end with an .h extension.

All header files should be included before the main() function.

 #include <stdio.h>
/*
 * all the lines from
 * the stdio.h header file
 * will be inserted here
 */

int main(void){

 /*
  *call a function 
  *defined in the stdio.h
  *header file
  */ 

 printf("This statement made possible by the stdio.h header file.\n");

 return 0; 

}

Note that preprocessor directives never end with semicolons because they’re not true C commands. A preprocessor directive is an instruction to the C compiler itself.

The header file we have been using is the stdio.h file. Its file name is an abbreviation for standard input-output header file. It is used whenever we want to get input or print output.

As an example, let’s work a little with another header file, math.h. This file includes a function for getting the value of a number raised to the power of another number.

#include <stdio.h>
#include <math.h>

int main(void){

  int a = 42, b = 3;

  double x = 7.5, y = 2.0;

  /*
   * return the value of 
   * x to the power of y
   */
  printf("7.5^2 = %f\n", pow(x, y));

  return 0; 

}

Note that the pow() function returns a floating-point value.

Let’s look at one more function from the math.h header file. The sqrt() function returns the square root of the argument passed to it. Don’t pass the sqrt() function a negative number!

#include <stdio.h>
#include <math.h>

int main(void){

  int a = 16;
  double b = 16.0;

  printf("square root of 2 = %f\n", sqrt(2.0));
  printf("square root of 2 = %f\n", sqrt(2));

  printf("square root of %d = %f\n", a, sqrt(a));
  printf("square root of %f = %f\n", b, sqrt(b));

  return 0;

}

The #define preprocessor directive is used to establish the symbolic constants that are used in a program. If we were to take a look at the stdio.h header file’s source code, we would find a number of #define directives there.

Symbolic constants are used so that we can easily change these values in the future, and have them be implemented throughout the program. This is because a #define directive results in a global substitution of the constants throughout the program in a process referred to as macro substitution. Simply put, macro substitution is the preprocessor substituting one thing for another throughout the entire program.

#include <stdio.h>

#define QUOTE "The map is not the territory."

int main(void){

  printf("Here is a quote: %s\n", QUOTE); 

  return 0;

}

Note that symbolic constants are almost always written in all caps.

When a preprocessor directive is identified, the entire line is treated as a preprocessor line. For this reason, we should place each preprocessor directive on its own separate line.

One a symbolic constant has been defined, it can be used to help define another symbolic constant.

#include <stdio.h>

#define SMALL 6
#define MEDIUM SMALL+2
#define LARGE MEDIUM+2

int main(void){

  printf("Size small = %d\n", SMALL);
  printf("Size medium = %d\n", MEDIUM);
  printf("Size large = %d\n", LARGE);

  return 0;

}
Advertisements