ADVERTISEMENT
ADVERTISEMENT

Preprocessor Directives in C with Examples

Preprocessor directives are lines in C programs that are not executed as part of the program, but are instead handled by the preprocessor. They all begin with the # symbol.

Here are some examples of preprocessor directives:

Types of Preprocessor Directives in C

The three types of preprocessor commands are as follows −

  • File inclusion directives.

  • Macro substitution directives.

  • Compiler control directives.

File Inclusion Directives 

The #include directive tells the preprocessor to insert the contents of the specified file into the source code at the point where the directive appears. In this example, the contents of the stdio.h file will be inserted into the program.

Macro Substitution Directives

The #define directive creates a macro, which is a symbolic constant. In this example, the preprocessor will replace any instances of PI in the code with the value 3.14159.

Compiler Control Directives

The #ifdef directive is used to include code only if a specific macro is defined. In this example, the printf statement will be included in the program only if the DEBUG macro has been defined using the #define directive.

Check the example given below:

#include <stdio.h>
#define PI 3.14159
#ifdef DEBUG
  printf("Tech Skill Guru");
#endif

Header Files in C

In C, a header file is a file that contains declarations of functions, variables, and other constructs that can be used in your program. Header files are usually included using the #include preprocessor directive.

There are two types of header files: system header files and user-defined header files.

System header files are provided as part of the C library and are included using the #include directive with angle brackets (e.g. #include <stdio.h>). These files contain declarations for functions, variables, and other constructs that are part of the C language or provided with the compiler. Examples of system header files include stdio.h, string.h, and math.h.

User-defined header files are created by the programmer and are included using the #include directive with double quotes (e.g. #include "myfile.h"). These files can contain declarations for functions, variables, and other constructs that are specific to the program or shared across multiple programs. User-defined header files are usually located in the same directory as the source code.

Here is an example of how a user-defined header file can be used:

#include "myfile.h"

int main(void) 
{
  print_message();
  return 0;
}

In this example, the myfile.h file contains the declaration for the print_message function. The main.c file includes the myfile.h file using the #include directive, which allows it to use the print_message function. 

#include<stdio.h> is a preprocessor directive in C that tells the preprocessor to insert the contents of the stdio.h file into the source code at the point where the directive appears.

The #include directive is used to include header files in your program. A header file is a file that contains definitions of functions, variables, and other constructs that can be used in your program. The stdio.h file is a standard C library header file that contains declarations for the standard input/output functions, such as printf and scanf.

The < and > characters surrounding the file name indicate that the file is a system header file, which means that it is part of the C library and is provided with the compiler. If the file name is enclosed in double quotes (e.g. #include "myfile.h"), it is treated as a user-defined header file, which means that it is a file that you have created yourself and is located in the same directory as your source code.

By including the stdio.h file in your program, you can use the functions and other constructs that it declares without having to define them yourself. This makes it easier to write programs, because you don't have to reimplement commonly used functionality.

Here is an example of how the #include directive can be used:

#include <stdio.h>
int main() 
{
  printf("Hello, world!");
  return 0;
}

In this example, the #include directive includes the stdio.h file, which allows the program to use the printf function. The main function then calls printf to print the string "Hello, world!" to the console.


ADVERTISEMENT

ADVERTISEMENT