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

bookExploring C++ Preprocessor Directives

メニューを表示するにはスワイプしてください

Before we can actually write any meaningful program, we have to learn how to add these standard libraries to our project.

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:

directive.h

directive.h

copy
1
#directive parameters

The command that adds external files to your program is called #include, and this is the command we will use the most for now.

include.h

include.h

copy
1
#include <name>

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. One of the brackets is missing. Try to run this code and see what happens.

main.cpp

main.cpp

copy
1234
int main() { return 0;

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.

main.cpp

main.cpp

header.h

header.h

copy
1234
int main() { #include <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.

question mark

What is preprocessor directive to add external files?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  4

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  4
some-alt