Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Preprocessor Directives | Getting Started
C++ Introduction

Preprocessor DirectivesPreprocessor Directives

To add external files to your program you have to use preprocessor directives. These are the commands that guide the preprocessor, a tool that transforms code before compilation. The syntax for most preprocessing directives is:

  • #: symbol that indicates that it is a preprocessing directive;
  • directive: specific preprocessing directive;
  • parameters: the associated value or argument for that directive.

The command that adds external files to your program is called #include.

Note

Standard files are attached using angle brackets < >, but you can also create your own files and connect them to your project similarly, using double quotes " ".

How #include works

Look at the code below and try to run it.

cpp

main.cpp

You get an error of a missing }. This is done on purpose to show how the #include works. We can create a separate file containing only the } symbol and include it in the main.cpp file using the #include directive.

cpp

main.cpp

h

header.h

The issue has been resolved, and you should no longer encounter an error. The reason for this resolution lies in the nature of the #include directive, which essentially just copies and pastes the content of a file at the point where it is called.

What is preprocessor directive to add external files?

Select the correct answer

Everything was clear?

Section 1. Chapter 5
course content

Course Content

C++ Introduction

Preprocessor DirectivesPreprocessor Directives

To add external files to your program you have to use preprocessor directives. These are the commands that guide the preprocessor, a tool that transforms code before compilation. The syntax for most preprocessing directives is:

  • #: symbol that indicates that it is a preprocessing directive;
  • directive: specific preprocessing directive;
  • parameters: the associated value or argument for that directive.

The command that adds external files to your program is called #include.

Note

Standard files are attached using angle brackets < >, but you can also create your own files and connect them to your project similarly, using double quotes " ".

How #include works

Look at the code below and try to run it.

cpp

main.cpp

You get an error of a missing }. This is done on purpose to show how the #include works. We can create a separate file containing only the } symbol and include it in the main.cpp file using the #include directive.

cpp

main.cpp

h

header.h

The issue has been resolved, and you should no longer encounter an error. The reason for this resolution lies in the nature of the #include directive, which essentially just copies and pastes the content of a file at the point where it is called.

What is preprocessor directive to add external files?

Select the correct answer

Everything was clear?

Section 1. Chapter 5
some-alt