-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathgetting-started-with-cpp.html
More file actions
239 lines (200 loc) · 66.1 KB
/
getting-started-with-cpp.html
File metadata and controls
239 lines (200 loc) · 66.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>C++ | Getting started with C++</title>
<meta name="generator" content="VuePress 1.8.2">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/site.webmanifest">
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
<meta name="description" content="Hello World, Comments, The standard C++ compilation process, Function, Visibility of function prototypes and declarations, Preprocessor">
<meta property="og:site_name" content="DevTut">
<meta property="og:title" content="C++ | Getting started with C++">
<meta property="og:description" content="Hello World, Comments, The standard C++ compilation process, Function, Visibility of function prototypes and declarations, Preprocessor">
<meta property="og:type" content="article">
<meta property="og:url" content="/cpp/getting-started-with-cpp.html">
<meta property="og:image" content="/logo.png">
<meta name="twitter:title" content="C++ | Getting started with C++">
<meta name="twitter:description" content="Hello World, Comments, The standard C++ compilation process, Function, Visibility of function prototypes and declarations, Preprocessor">
<meta name="twitter:url" content="/cpp/getting-started-with-cpp.html">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="/logo.png">
<meta name="theme-color" content="#ffffff">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="msapplication-TileImage" content="/mstile-150x150.png">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="google-site-verification" content="76_rKXgwMVIjd-axJC_1zPV9OS4mEjvtgjYOWVkAdnQ">
<link rel="preload" href="/assets/css/0.styles.60619e34.css" as="style"><link rel="preload" href="/assets/js/app.1779e102.js" as="script"><link rel="preload" href="/assets/js/3.2cfa8016.js" as="script"><link rel="preload" href="/assets/js/660.ebbb5e71.js" as="script">
<link rel="stylesheet" href="/assets/css/0.styles.60619e34.css">
</head>
<body>
<div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">DevTut</span></a> <div class="links"><form id="search-form" role="search" class="algolia-search-wrapper search-box"><input id="algolia-search-input" class="search-query"></form> <nav class="nav-links can-hide"> <a href="https://github.com/devtut/generate" target="_blank" rel="noopener noreferrer" class="repo-link">
GitHub
<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"> <a href="https://github.com/devtut/generate" target="_blank" rel="noopener noreferrer" class="repo-link">
GitHub
<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav> <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>C++</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/cpp/" aria-current="page" class="sidebar-link">Disclaimer</a></li><li><a href="/cpp/getting-started-with-cpp.html" aria-current="page" class="active sidebar-link">Getting started with C++</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/cpp/getting-started-with-cpp.html#hello-world" class="sidebar-link">Hello World</a></li><li class="sidebar-sub-header"><a href="/cpp/getting-started-with-cpp.html#comments" class="sidebar-link">Comments</a></li><li class="sidebar-sub-header"><a href="/cpp/getting-started-with-cpp.html#the-standard-c-compilation-process" class="sidebar-link">The standard C++ compilation process</a></li><li class="sidebar-sub-header"><a href="/cpp/getting-started-with-cpp.html#function" class="sidebar-link">Function</a></li><li class="sidebar-sub-header"><a href="/cpp/getting-started-with-cpp.html#visibility-of-function-prototypes-and-declarations" class="sidebar-link">Visibility of function prototypes and declarations</a></li><li class="sidebar-sub-header"><a href="/cpp/getting-started-with-cpp.html#preprocessor" class="sidebar-link">Preprocessor</a></li></ul></li><li><a href="/cpp/literals.html" class="sidebar-link">Literals</a></li><li><a href="/cpp/operator-precedence.html" class="sidebar-link">operator precedence</a></li><li><a href="/cpp/floating-point-arithmetic.html" class="sidebar-link">Floating Point Arithmetic</a></li><li><a href="/cpp/bit-operators.html" class="sidebar-link">Bit Operators</a></li><li><a href="/cpp/bit-manipulation.html" class="sidebar-link">Bit Manipulation</a></li><li><a href="/cpp/bit-fields.html" class="sidebar-link">Bit fields</a></li><li><a href="/cpp/arrays.html" class="sidebar-link">Arrays</a></li><li><a href="/cpp/iterators.html" class="sidebar-link">Iterators</a></li><li><a href="/cpp/basic-input-output-in-c.html" class="sidebar-link">Basic input/output in c++</a></li><li><a href="/cpp/loops.html" class="sidebar-link">Loops</a></li><li><a href="/cpp/file-i-o.html" class="sidebar-link">File I/O</a></li><li><a href="/cpp/cpp-streams.html" class="sidebar-link">C++ Streams</a></li><li><a href="/cpp/stream-manipulators.html" class="sidebar-link">Stream manipulators</a></li><li><a href="/cpp/flow-control.html" class="sidebar-link">Flow Control</a></li><li><a href="/cpp/metaprogramming.html" class="sidebar-link">Metaprogramming</a></li><li><a href="/cpp/const-keyword.html" class="sidebar-link">const keyword</a></li><li><a href="/cpp/mutable-keyword.html" class="sidebar-link">mutable keyword</a></li><li><a href="/cpp/friend-keyword.html" class="sidebar-link">Friend keyword</a></li><li><a href="/cpp/type-keywords.html" class="sidebar-link">Type Keywords</a></li><li><a href="/cpp/basic-type-keywords.html" class="sidebar-link">Basic Type Keywords</a></li><li><a href="/cpp/variable-declaration-keywords.html" class="sidebar-link">Variable Declaration Keywords</a></li><li><a href="/cpp/keywords.html" class="sidebar-link">Keywords</a></li><li><a href="/cpp/returning-several-values-from-a-function.html" class="sidebar-link">Returning several values from a function</a></li><li><a href="/cpp/polymorphism.html" class="sidebar-link">Polymorphism</a></li><li><a href="/cpp/references.html" class="sidebar-link">References</a></li><li><a href="/cpp/value-and-reference-semantics.html" class="sidebar-link">Value and Reference Semantics</a></li><li><a href="/cpp/c-function-call-by-value-vs-call-by-reference.html" class="sidebar-link">C++ function "call by value" vs. "call by reference"</a></li><li><a href="/cpp/copying-vs-assignment.html" class="sidebar-link">Copying vs Assignment</a></li><li><a href="/cpp/pointers.html" class="sidebar-link">Pointers</a></li><li><a href="/cpp/pointers-to-members.html" class="sidebar-link">Pointers to members</a></li><li><a href="/cpp/the-this-pointer.html" class="sidebar-link">The This Pointer</a></li><li><a href="/cpp/smart-pointers.html" class="sidebar-link">Smart Pointers</a></li><li><a href="/cpp/classes-structures.html" class="sidebar-link">Classes/Structures</a></li><li><a href="/cpp/function-overloading.html" class="sidebar-link">Function Overloading</a></li><li><a href="/cpp/operator-overloading.html" class="sidebar-link">Operator Overloading</a></li><li><a href="/cpp/function-template-overloading.html" class="sidebar-link">Function Template Overloading</a></li><li><a href="/cpp/virtual-member-functions.html" class="sidebar-link">Virtual Member Functions</a></li><li><a href="/cpp/inline-functions.html" class="sidebar-link">Inline functions</a></li><li><a href="/cpp/special-member-functions.html" class="sidebar-link">Special Member Functions</a></li><li><a href="/cpp/non-static-member-functions.html" class="sidebar-link">Non-Static Member Functions</a></li><li><a href="/cpp/constant-class-member-functions.html" class="sidebar-link">Constant class member functions</a></li><li><a href="/cpp/c-containers.html" class="sidebar-link">C++ Containers</a></li><li><a href="/cpp/namespaces.html" class="sidebar-link">Namespaces</a></li><li><a href="/cpp/header-files.html" class="sidebar-link">Header Files</a></li><li><a href="/cpp/using-declaration.html" class="sidebar-link">Using declaration</a></li><li><a href="/cpp/std-string.html" class="sidebar-link">std::string</a></li><li><a href="/cpp/std-array.html" class="sidebar-link">std::array</a></li><li><a href="/cpp/std-vector.html" class="sidebar-link">std::vector</a></li><li><a href="/cpp/std-map.html" class="sidebar-link">std::map</a></li><li><a href="/cpp/std-optional.html" class="sidebar-link">std::optional</a></li><li><a href="/cpp/std-function-to-wrap-any-element-that-is-callable.html" class="sidebar-link">std::function: To wrap any element that is callable</a></li><li><a href="/cpp/std-forward-list.html" class="sidebar-link">std::forward_list</a></li><li><a href="/cpp/std-pair.html" class="sidebar-link">std::pair</a></li><li><a href="/cpp/std-atomics.html" class="sidebar-link">std::atomics</a></li><li><a href="/cpp/std-variant.html" class="sidebar-link">std::variant</a></li><li><a href="/cpp/std-iomanip.html" class="sidebar-link">std::iomanip</a></li><li><a href="/cpp/std-any.html" class="sidebar-link">std::any</a></li><li><a href="/cpp/std-set-and-std-multiset.html" class="sidebar-link">std::set and std::multiset</a></li><li><a href="/cpp/std-integer-sequence.html" class="sidebar-link">std::integer_sequence</a></li><li><a href="/cpp/using-std-unordered-map.html" class="sidebar-link">Using std::unordered_map</a></li><li><a href="/cpp/standard-library-algorithms.html" class="sidebar-link">Standard Library Algorithms</a></li><li><a href="/cpp/the-iso-c-standard.html" class="sidebar-link">The ISO C++ Standard</a></li><li><a href="/cpp/inline-variables.html" class="sidebar-link">Inline variables</a></li><li><a href="/cpp/random-number-generation.html" class="sidebar-link">Random number generation</a></li><li><a href="/cpp/date-and-time-using-chrono-header.html" class="sidebar-link">Date and time using header</a></li><li><a href="/cpp/sorting.html" class="sidebar-link">Sorting</a></li><li><a href="/cpp/enumeration.html" class="sidebar-link">Enumeration</a></li><li><a href="/cpp/iteration.html" class="sidebar-link">Iteration</a></li><li><a href="/cpp/regular-expressions.html" class="sidebar-link">Regular expressions</a></li><li><a href="/cpp/implementation-defined-behavior.html" class="sidebar-link">Implementation-defined behavior</a></li><li><a href="/cpp/exceptions.html" class="sidebar-link">Exceptions</a></li><li><a href="/cpp/lambdas.html" class="sidebar-link">Lambdas</a></li><li><a href="/cpp/value-categories.html" class="sidebar-link">Value Categories</a></li><li><a href="/cpp/preprocessor.html" class="sidebar-link">Preprocessor</a></li><li><a href="/cpp/data-structures-in-c.html" class="sidebar-link">Data Structures in C++</a></li><li><a href="/cpp/templates.html" class="sidebar-link">Templates</a></li><li><a href="/cpp/expression-templates.html" class="sidebar-link">Expression templates</a></li><li><a href="/cpp/curiously-recurring-template-pattern-crtp.html" class="sidebar-link">Curiously Recurring Template Pattern (CRTP)</a></li><li><a href="/cpp/threading.html" class="sidebar-link">Threading</a></li><li><a href="/cpp/thread-synchronization-structures.html" class="sidebar-link">Thread synchronization structures</a></li><li><a href="/cpp/the-rule-of-three-five-and-zero.html" class="sidebar-link">The Rule of Three, Five, And Zero</a></li><li><a href="/cpp/raii-resource-acquisition-is-initialization.html" class="sidebar-link">RAII: Resource Acquisition Is Initialization</a></li><li><a href="/cpp/rtti-run-time-type-information.html" class="sidebar-link">RTTI: Run-Time Type Information</a></li><li><a href="/cpp/mutexes.html" class="sidebar-link">Mutexes</a></li><li><a href="/cpp/recursive-mutex.html" class="sidebar-link">Recursive Mutex</a></li><li><a href="/cpp/semaphore.html" class="sidebar-link">Semaphore</a></li><li><a href="/cpp/futures-and-promises.html" class="sidebar-link">Futures and Promises</a></li><li><a href="/cpp/atomic-types.html" class="sidebar-link">Atomic Types</a></li><li><a href="/cpp/type-erasure.html" class="sidebar-link">Type Erasure</a></li><li><a href="/cpp/explicit-type-conversions.html" class="sidebar-link">Explicit type conversions</a></li><li><a href="/cpp/unnamed-types.html" class="sidebar-link">Unnamed types</a></li><li><a href="/cpp/type-traits.html" class="sidebar-link">Type Traits</a></li><li><a href="/cpp/return-type-covariance.html" class="sidebar-link">Return Type Covariance</a></li><li><a href="/cpp/layout-of-object-types.html" class="sidebar-link">Layout of object types</a></li><li><a href="/cpp/type-inference.html" class="sidebar-link">Type Inference</a></li><li><a href="/cpp/typedef-and-type-aliases.html" class="sidebar-link">Typedef and type aliases</a></li><li><a href="/cpp/type-deduction.html" class="sidebar-link">type deduction</a></li><li><a href="/cpp/trailing-return-type.html" class="sidebar-link">Trailing return type</a></li><li><a href="/cpp/alignment.html" class="sidebar-link">Alignment</a></li><li><a href="/cpp/perfect-forwarding.html" class="sidebar-link">Perfect Forwarding</a></li><li><a href="/cpp/decltype.html" class="sidebar-link">decltype</a></li><li><a href="/cpp/sfinae-substitution-failure-is-not-an-error.html" class="sidebar-link">SFINAE (Substitution Failure Is Not An Error)</a></li><li><a href="/cpp/undefined-behavior.html" class="sidebar-link">Undefined Behavior</a></li><li><a href="/cpp/overload-resolution.html" class="sidebar-link">Overload resolution</a></li><li><a href="/cpp/move-semantics.html" class="sidebar-link">Move Semantics</a></li><li><a href="/cpp/pimpl-idiom.html" class="sidebar-link">Pimpl Idiom</a></li><li><a href="/cpp/auto.html" class="sidebar-link">auto</a></li><li><a href="/cpp/copy-elision.html" class="sidebar-link">Copy Elision</a></li><li><a href="/cpp/fold-expressions.html" class="sidebar-link">Fold Expressions</a></li><li><a href="/cpp/unions.html" class="sidebar-link">Unions</a></li><li><a href="/cpp/design-pattern-implementation-in-c.html" class="sidebar-link">Design pattern implementation in C++</a></li><li><a href="/cpp/singleton-design-pattern.html" class="sidebar-link">Singleton Design Pattern</a></li><li><a href="/cpp/user-defined-literals.html" class="sidebar-link">User-Defined Literals</a></li><li><a href="/cpp/memory-management.html" class="sidebar-link">Memory management</a></li><li><a href="/cpp/c-11-memory-model.html" class="sidebar-link">C++11 Memory Model</a></li><li><a href="/cpp/scopes.html" class="sidebar-link">Scopes</a></li><li><a href="/cpp/static-assert.html" class="sidebar-link">static_assert</a></li><li><a href="/cpp/constexpr.html" class="sidebar-link">constexpr</a></li><li><a href="/cpp/one-definition-rule-odr.html" class="sidebar-link">One Definition Rule (ODR)</a></li><li><a href="/cpp/unspecified-behavior.html" class="sidebar-link">Unspecified behavior</a></li><li><a href="/cpp/argument-dependent-name-lookup.html" class="sidebar-link">Argument Dependent Name Lookup</a></li><li><a href="/cpp/attributes.html" class="sidebar-link">Attributes</a></li><li><a href="/cpp/recursion-in-c.html" class="sidebar-link">Recursion in C++</a></li><li><a href="/cpp/arithmitic-metaprogramming.html" class="sidebar-link">Arithmitic Metaprogramming</a></li><li><a href="/cpp/callable-objects.html" class="sidebar-link">Callable Objects</a></li><li><a href="/cpp/client-server-examples.html" class="sidebar-link">Client server examples</a></li><li><a href="/cpp/const-correctness.html" class="sidebar-link">Const Correctness</a></li><li><a href="/cpp/parameter-packs.html" class="sidebar-link">Parameter packs</a></li><li><a href="/cpp/build-systems.html" class="sidebar-link">Build Systems</a></li><li><a href="/cpp/concurrency-with-openmp.html" class="sidebar-link">Concurrency With OpenMP</a></li><li><a href="/cpp/resource-management.html" class="sidebar-link">Resource Management</a></li><li><a href="/cpp/storage-class-specifiers.html" class="sidebar-link">Storage class specifiers</a></li><li><a href="/cpp/linkage-specifications.html" class="sidebar-link">Linkage specifications</a></li><li><a href="/cpp/digit-separators.html" class="sidebar-link">Digit separators</a></li><li><a href="/cpp/c-incompatibilities.html" class="sidebar-link">C incompatibilities</a></li><li><a href="/cpp/side-by-side-comparisons-of-classic-c-examples-solved-via-c-vs-c-11-vs-c-14-vs-c-17.html" class="sidebar-link">Side by Side Comparisons of classic C++ examples solved via C++ vs C++11 vs C++14 vs C++17</a></li><li><a href="/cpp/compiling-and-building.html" class="sidebar-link">Compiling and Building</a></li><li><a href="/cpp/common-compile-linker-errors-gcc.html" class="sidebar-link">Common compile/linker errors (GCC)</a></li><li><a href="/cpp/more-undefined-behaviors-in-c.html" class="sidebar-link">More undefined behaviors in C++</a></li><li><a href="/cpp/unit-testing-in-c.html" class="sidebar-link">Unit Testing in C++</a></li><li><a href="/cpp/c-debugging-and-debug-prevention-tools-techniques.html" class="sidebar-link">C++ Debugging and Debug-prevention Tools & Techniques</a></li><li><a href="/cpp/optimization-in-c.html" class="sidebar-link">Optimization in C++</a></li><li><a href="/cpp/optimization.html" class="sidebar-link">Optimization</a></li><li><a href="/cpp/profiling.html" class="sidebar-link">Profiling</a></li><li><a href="/cpp/refactoring-techniques.html" class="sidebar-link">Refactoring Techniques</a></li><li><a href="/cpp/internationalization-in-c.html" class="sidebar-link">Internationalization in C++</a></li><li><a href="/cpp/contributors.html" class="sidebar-link">The Contributors</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="getting-started-with-c"><a href="#getting-started-with-c" class="header-anchor">#</a> Getting started with C++</h1> <h2 id="hello-world"><a href="#hello-world" class="header-anchor">#</a> Hello World</h2> <p>This program prints <code>Hello World!</code> to the standard output stream:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iostream></span></span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
std<span class="token double-colon punctuation">::</span>cout <span class="token operator"><<</span> <span class="token string">"Hello World!"</span> <span class="token operator"><<</span> std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>See it <a href="http://coliru.stacked-crooked.com/a/ba766ad8ca2fae56" target="_blank" rel="noopener noreferrer">live on Coliru<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>.</p> <h3 id="analysis"><a href="#analysis" class="header-anchor">#</a> Analysis</h3> <p>Let's examine each part of this code in detail:</p> <li>
`#include <iostream>` is a **preprocessor directive** that includes the content of the standard C++ header file `iostream`.
`iostream` is a **standard library header file** that contains definitions of the standard input and output streams. These definitions are included in the `std` namespace, explained below.
The **standard input/output (I/O) streams** provide ways for programs to get input from and output to an external system -- usually the terminal.
</iostream></li> <li>
`int main() { ... }` defines a new [function](http://stackoverflow.com/documentation/c%2B%2B/206/getting-started-with-c-language/25460/function) named `main`. By convention, the `main` function is called upon execution of the program. There must be only one `main` function in a C++ program, and it must always return a number of the `int` type.
<p>Here, the `int` is what is called the function's [return type](http://stackoverflow.com/documentation/c%2B%2B/572/lambdas/1855/specifying-the-return-type#t=20170126184705835537).
The value returned by the `main` function is an **exit code.**</p>
By convention, a program exit code of `0` or `EXIT_SUCCESS` is interpreted as success by a system that executes the program. Any other return code is associated with an error.
If no `return` statement is present, the `main` function (and thus, the program itself) returns `0` by default. In this example, we don't need to explicitly write `return 0;`.
All other functions, except those that return the `void` type, must explicitly return a value according to their return type, or else must not return at all.
</li> <li>
`std::cout << "Hello World!" << std::endl;` prints "Hello World!" to the standard output stream:
<ul><li>
`std` is a [namespace](http://stackoverflow.com/documentation/c%2B%2B/495/namespaces#t=201701261847520776598), and `::` is the **scope resolution operator** that allows look-ups for objects by name within a namespace.
There are many namespaces. Here, we use `::` to show we want to use `cout` from the `std` namespace. For more information refer to [Scope Resolution Operator - Microsoft Documentation](https://msdn.microsoft.com/en-us/library/b451xz31.aspx/).
</li> <li>
`std::cout` is the **standard output stream** object, defined in `iostream`, and it prints to the standard output (`stdout`).
</li> <li>
`<<` is, **in this context**, the **stream insertion operator**, so called because it **inserts** an object into the **stream** object.
<p>The standard library defines the `<<` operator to perform data insertion for certain data types into output streams. `stream << content` inserts `content` into the stream and returns the same, but updated stream. This allows stream insertions to be chained:
`std::cout << "Foo" << " Bar";`
prints "FooBar" to the console.</p></li> <li>
`"Hello World!"` is a **character string literal**, or a "text literal." The stream insertion operator for character string literals is defined in file `iostream`.
</li> <li>
[`std::endl`](http://en.cppreference.com/w/cpp/io/manip/endl) is a special **I/O stream manipulator** object, also defined in file `iostream`. Inserting a manipulator into a stream changes the state of the stream.
The stream manipulator `std::endl` does two things: first it inserts the end-of-line character and then it flushes the stream buffer to force the text to show up on the console. This ensures that the data inserted into the stream actually appear on your console. (Stream data is usually stored in a buffer and then "flushed" in batches unless you force a flush immediately.)
An alternate method that avoids the flush is:
<div class="language-cpp extra-class"><pre class="language-cpp"><code>std<span class="token double-colon punctuation">::</span>cout <span class="token operator"><<</span> <span class="token string">"Hello World!\n"</span><span class="token punctuation">;</span>
</code></pre></div><p>where <code>\n</code> is the <strong>character escape sequence</strong> for the newline character.
</p></li> <li>
The semicolon (<code>;</code>) notifies the compiler that a statement has ended. All C++ statements and class definitions require an ending/terminating semicolon.
</li><p></p> <h2 id="comments"><a href="#comments" class="header-anchor">#</a> Comments</h2> <p>A <strong>comment</strong> is a way to put arbitrary text inside source code without having the C++ compiler interpret it with any functional meaning. Comments are used to give insight into the design or method of a program.</p> <p>There are two types of comments in C++:</p> <h3 id="single-line-comments"><a href="#single-line-comments" class="header-anchor">#</a> Single-Line Comments</h3> <p>The double forward-slash sequence <code>//</code> will mark all text until a newline as a comment:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token comment">// This is a single-line comment.</span>
<span class="token keyword">int</span> a<span class="token punctuation">;</span> <span class="token comment">// this also is a single-line comment</span>
<span class="token keyword">int</span> i<span class="token punctuation">;</span> <span class="token comment">// this is another single-line comment</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="c-style-block-comments"><a href="#c-style-block-comments" class="header-anchor">#</a> C-Style/Block Comments</h3> <p>The sequence <code>/*</code> is used to declare the start of the comment block and the sequence <code>*/</code> is used to declare the end of comment. All text between the start and end sequences is interpreted as a comment, even if the text is otherwise valid C++ syntax. These are sometimes called "C-style" comments, as this comment syntax is inherited from C++'s predecessor language, C:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token comment">/*
* This is a block comment.
*/</span>
<span class="token keyword">int</span> a<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>In any block comment, you can write anything you want. When the compiler encounters the symbol <code>*/</code>, it terminates the block comment:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token comment">/* A block comment with the symbol /*
Note that the compiler is not affected by the second /*
however, once the end-block-comment symbol is reached,
the comment ends.
*/</span>
<span class="token keyword">int</span> a<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>The above example is valid C++ (and C) code. However, having additional <code>/*</code> inside a block comment might result in a warning on some compilers.</p> <p>Block comments can also start and end <strong>within</strong> a single line. For example:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">void</span> <span class="token function">SomeFunction</span><span class="token punctuation">(</span><span class="token comment">/* argument 1 */</span> <span class="token keyword">int</span> a<span class="token punctuation">,</span> <span class="token comment">/* argument 2 */</span> <span class="token keyword">int</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="importance-of-comments"><a href="#importance-of-comments" class="header-anchor">#</a> Importance of Comments</h3> <p>As with all programming languages, comments provide several benefits:</p> <ul><li>Explicit documentation of code to make it easier to read/maintain</li> <li>Explanation of the purpose and functionality of code</li> <li>Details on the history or reasoning behind the code</li> <li>Placement of copyright/licenses, project notes, special thanks, contributor credits, etc. directly in the source code.</li></ul> <p>However, comments also have their downsides:</p> <ul><li>They must be maintained to reflect any changes in the code</li> <li>Excessive comments tend to make the code <strong>less</strong> readable</li></ul> <p>The need for comments can be reduced by writing clear, self-documenting code. A simple example is the use of explanatory names for variables, functions, and types. Factoring out logically related tasks into discrete functions goes hand-in-hand with this.</p> <h3 id="comment-markers-used-to-disable-code"><a href="#comment-markers-used-to-disable-code" class="header-anchor">#</a> Comment markers used to disable code</h3> <p>During development, comments can also be used to quickly disable portions of code without deleting it. This is often useful for testing or debugging purposes, but is not good style for anything other than temporary edits. This is often referred to as “commenting out”.</p> <p>Similarly, keeping old versions of a piece of code in a comment for reference purposes is frowned upon, as it clutters files while offering little value compared to exploring the code's history via a versioning system.</p> <h2 id="the-standard-c-compilation-process"><a href="#the-standard-c-compilation-process" class="header-anchor">#</a> The standard C++ compilation process</h2> <p>Executable C++ program code is usually produced by a compiler.</p> <p>A <strong>compiler</strong> is a program that translates code from a programming language into another form which is (more) directly executable for a computer. Using a compiler to translate code is called <strong>compilation.</strong></p> <p>C++ inherits the form of its compilation process from its "parent" language, C. Below is a list showing the four major steps of compilation in C++:</p> <ol><li>The C++ preprocessor copies the contents of any included header files into the source code file, generates macro code, and replaces symbolic constants defined using #define with their values.</li> <li>The expanded source code file produced by the C++ preprocessor is compiled into assembly language appropriate for the platform.</li> <li>The assembler code generated by the compiler is assembled into appropriate object code for the platform.</li> <li>The object code file generated by the assembler is linked together with the object code files for any library functions used to produce an executable file.</li></ol> <ul><li>Note: some compiled code is linked together, but not to create a final program. Usually, this "linked" code can also be packaged into a format that can be used by other programs. This "bundle of packaged, usable code" is what C++ programmers refer to as a <strong>library.</strong></li></ul> <p>Many C++ compilers may also merge or un-merge certain parts of the compilation process for ease or for additional analysis. Many C++ programmers will use different tools, but all of the tools will generally follow this generalized process when they are involved in the production of a program.</p> <p>The link below extends this discussion and provides a nice graphic to help.
[1]: <a href="http://faculty.cs.niu.edu/%7Emcmahon/CS241/Notes/compile.html" target="_blank" rel="noopener noreferrer">http://faculty.cs.niu.edu/~mcmahon/CS241/Notes/compile.html<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h2 id="function"><a href="#function" class="header-anchor">#</a> Function</h2> <p>A <strong>function</strong> is a unit of code that represents a sequence of statements.</p> <p>Functions can accept <strong>arguments</strong> or values and <strong>return</strong> a single value (or not). To use a function, a <strong>function call</strong> is used on argument values and the use of the function call itself is replaced with its return value.</p> <p>Every function has a <strong>type signature</strong> -- the types of its arguments and the type of its return type.</p> <p>Functions are inspired by the concepts of the procedure and the mathematical function.</p> <ul><li>Note: C++ functions are essentially procedures and do not follow the exact definition or rules of mathematical functions.</li></ul> <p>Functions are often meant to perform a specific task. and can be called from other parts of a program. A function must be declared and defined before it is called elsewhere in a program.</p> <ul><li>Note: popular function definitions may be hidden in other included files (often for convenience and reuse across many files). This is a common use of header files.</li></ul> <h3 id="function-declaration"><a href="#function-declaration" class="header-anchor">#</a> Function Declaration</h3> <p>A <strong>function declaration</strong> is declares the existence of a function with its name and type signature to the compiler. The syntax is as the following:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">int</span> <span class="token function">add2</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// The function is of the type (int) -> (int)</span>
</code></pre></div><p>In the example above, the <code>int add2(int i)</code> function declares the following to the compiler:</p> <ul><li>The <strong>return type</strong> is <code>int</code>.</li> <li>The <strong>name</strong> of the function is <code>add2</code>.
</li><li>The <strong>number of arguments</strong> to the function is 1:
<ul></ul></li> <li>The first argument is of the type <code>int</code>.</li> <li>The first argument will be referred to in the function's contents by the name <code>i</code>.</li></ul> <p>The argument name is optional; the declaration for the function could also be the following:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">int</span> <span class="token function">add2</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Omitting the function arguments' name is also permitted.</span>
</code></pre></div><p>Per the <strong>one-definition rule</strong>, a function with a certain type signature can only be declared or defined once in an entire C++ code base visible to the C++ compiler. In other words, functions with a specific type signature cannot be re-defined -- they must only be defined once. Thus, the following is not valid C++:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">int</span> <span class="token function">add2</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// The compiler will note that add2 is a function (int) -> int</span>
<span class="token keyword">int</span> <span class="token function">add2</span><span class="token punctuation">(</span><span class="token keyword">int</span> j<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// As add2 already has a definition of (int) -> int, the compiler</span>
<span class="token comment">// will regard this as an error.</span>
</code></pre></div><p>If a function returns nothing, its return type is written as <code>void</code>. If it takes no parameters, the parameter list should be empty.</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">void</span> <span class="token function">do_something</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// The function takes no parameters, and does not return anything.</span>
<span class="token comment">// Note that it can still affect variables it has access to.</span>
</code></pre></div><h3 id="function-call"><a href="#function-call" class="header-anchor">#</a> Function Call</h3> <p>A function can be called after it has been declared. For example, the following program calls <code>add2</code> with the value of <code>2</code> within the function of <code>main</code>:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iostream></span></span>
<span class="token keyword">int</span> <span class="token function">add2</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Declaration of add2</span>
<span class="token comment">// Note: add2 is still missing a DEFINITION.</span>
<span class="token comment">// Even though it doesn't appear directly in code,</span>
<span class="token comment">// add2's definition may be LINKED in from another object file.</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
std<span class="token double-colon punctuation">::</span>cout <span class="token operator"><<</span> <span class="token function">add2</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator"><<</span> <span class="token string">"\n"</span><span class="token punctuation">;</span> <span class="token comment">// add2(2) will be evaluated at this point,</span>
<span class="token comment">// and the result is printed.</span>
<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>Here, <code>add2(2)</code> is the syntax for a function call.</p> <h3 id="function-definition"><a href="#function-definition" class="header-anchor">#</a> Function Definition</h3> <p>A <strong>function definition</strong>* is similar to a declaration, except it also contains the code that is executed when the function is called within its body.</p> <p>An example of a function definition for <code>add2</code> might be:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">int</span> <span class="token function">add2</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token punctuation">)</span> <span class="token comment">// Data that is passed into (int i) will be referred to by the name i</span>
<span class="token punctuation">{</span> <span class="token comment">// while in the function's curly brackets or "scope."</span>
<span class="token keyword">int</span> j <span class="token operator">=</span> i <span class="token operator">+</span> <span class="token number">2</span><span class="token punctuation">;</span> <span class="token comment">// Definition of a variable j as the value of i+2.</span>
<span class="token keyword">return</span> j<span class="token punctuation">;</span> <span class="token comment">// Returning or, in essence, substitution of j for a function call to</span>
<span class="token comment">// add2.</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="function-overloading"><a href="#function-overloading" class="header-anchor">#</a> Function Overloading</h3> <p>You can create multiple functions with the same name but different parameters.</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">int</span> <span class="token function">add2</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token punctuation">)</span> <span class="token comment">// Code contained in this definition will be evaluated</span>
<span class="token punctuation">{</span> <span class="token comment">// when add2() is called with one parameter.</span>
<span class="token keyword">int</span> j <span class="token operator">=</span> i <span class="token operator">+</span> <span class="token number">2</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> j<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">int</span> <span class="token function">add2</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token punctuation">,</span> <span class="token keyword">int</span> j<span class="token punctuation">)</span> <span class="token comment">// However, when add2() is called with two parameters, the</span>
<span class="token punctuation">{</span> <span class="token comment">// code from the initial declaration will be overloaded,</span>
<span class="token keyword">int</span> k <span class="token operator">=</span> i <span class="token operator">+</span> j <span class="token operator">+</span> <span class="token number">2</span> <span class="token punctuation">;</span> <span class="token comment">// and the code in this declaration will be evaluated</span>
<span class="token keyword">return</span> k<span class="token punctuation">;</span> <span class="token comment">// instead.</span>
<span class="token punctuation">}</span>
</code></pre></div><p>Both functions are called by the same name <code>add2</code>, but the actual function that is called depends directly on the amount and type of the parameters in the call. In most cases, the C++ compiler can compute which function to call. In some cases, the type must be explicitly stated.</p> <h3 id="default-parameters"><a href="#default-parameters" class="header-anchor">#</a> Default Parameters</h3> <p>Default values for function parameters can only be specified in function declarations.</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">int</span> <span class="token function">multiply</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span> <span class="token keyword">int</span> b <span class="token operator">=</span> <span class="token number">7</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// b has default value of 7.</span>
<span class="token keyword">int</span> <span class="token function">multiply</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span> <span class="token keyword">int</span> b<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> a <span class="token operator">*</span> b<span class="token punctuation">;</span> <span class="token comment">// If multiply() is called with one parameter, the</span>
<span class="token punctuation">}</span> <span class="token comment">// value will be multiplied by the default, 7.</span>
</code></pre></div><p>In this example, <code>multiply()</code> can be called with one or two parameters. If only one parameter is given, <code>b</code> will have default value of 7. Default arguments must be placed in the latter arguments of the function. For example:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">int</span> <span class="token function">multiply</span><span class="token punctuation">(</span><span class="token keyword">int</span> a <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token keyword">int</span> b <span class="token operator">=</span> <span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// This is legal </span>
<span class="token keyword">int</span> <span class="token function">multiply</span><span class="token punctuation">(</span><span class="token keyword">int</span> a <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token keyword">int</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// This is illegal since int a is in the former</span>
</code></pre></div><h3 id="special-function-calls-operators"><a href="#special-function-calls-operators" class="header-anchor">#</a> Special Function Calls - Operators</h3> <p>There exist special function calls in C++ which have different syntax than <code>name_of_function(value1, value2, value3)</code>. The most common example is that of operators.</p> <p>Certain special character sequences that will be reduced to function calls by the compiler, such as <code>!</code>, <code>+</code>, <code>-</code>, <code>*</code>, <code>%</code>, and <code><<</code> and many more. These special characters are normally associated with non-programming usage or are used for aesthetics (e.g. the <code>+</code> character is commonly recognized as the addition symbol both within C++ programming as well as in elementary math).</p> <p>C++ handles these character sequences with a special syntax; but, in essence, each occurrence of an operator is reduced to a function call. For example, the following C++ expression:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token number">3</span><span class="token operator">+</span><span class="token number">3</span>
</code></pre></div><p>is equivalent to the following function call:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">operator</span><span class="token operator">+</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span>
</code></pre></div><p>All operator function names start with <code>operator</code>.</p> <p>While in C++'s immediate predecessor, C, operator function names cannot be assigned different meanings by providing additional definitions with different type signatures, in C++, this is valid. "Hiding" additional function definitions under one unique function name is referred to as <strong>operator overloading</strong> in C++, and is a relatively common, but not universal, convention in C++.</p> <h2 id="visibility-of-function-prototypes-and-declarations"><a href="#visibility-of-function-prototypes-and-declarations" class="header-anchor">#</a> Visibility of function prototypes and declarations</h2> <p>In C++, code must be declared or defined before usage. For example, the following produces a compile time error:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token function">foo</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// error: foo is called, but has not yet been declared</span>
<span class="token punctuation">}</span>
<span class="token keyword">void</span> <span class="token function">foo</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span> <span class="token comment">// this later definition is not known in main</span>
<span class="token punctuation">{</span>
<span class="token punctuation">}</span>
</code></pre></div><p>There are two ways to resolve this: putting either the definition or declaration of <code>foo()</code> before its usage in <code>main()</code>. Here is one example:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">void</span> <span class="token function">foo</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token comment">//Declare the foo function and body first</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token function">foo</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// OK: foo is completely defined beforehand, so it can be called here.</span>
<span class="token punctuation">}</span>
</code></pre></div><p>However it is also possible to "forward-declare" the function by putting only a "prototype" declaration before its usage and then defining the function body later:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">void</span> <span class="token function">foo</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Prototype declaration of foo, seen by main</span>
<span class="token comment">// Must specify return type, name, and argument list types</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token function">foo</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// OK: foo is known, called even though its body is not yet defined</span>
<span class="token punctuation">}</span>
<span class="token keyword">void</span> <span class="token function">foo</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span> <span class="token comment">//Must match the prototype</span>
<span class="token punctuation">{</span>
<span class="token comment">// Define body of foo here</span>
<span class="token punctuation">}</span>
</code></pre></div><p>The prototype must specify the return type (<code>void</code>), the name of the function (<code>foo</code>), and the argument list variable types (<code>int</code>), but the <a href="https://stackoverflow.com/questions/5234169/why-do-function-prototypes-include-parameter-names-when-theyre-not-required" target="_blank" rel="noopener noreferrer">names of the arguments are NOT required<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>.</p> <p>One common way to integrate this into the organization of source files is to make a header file containing all of the prototype declarations:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token comment">// foo.h</span>
<span class="token keyword">void</span> <span class="token function">foo</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// prototype declaration</span>
</code></pre></div><p>and then provide the full definition elsewhere:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token comment">// foo.cpp --> foo.o</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">"foo.h"</span> <span class="token comment">// foo's prototype declaration is "hidden" in here</span></span>
<span class="token keyword">void</span> <span class="token function">foo</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token comment">// foo's body definition</span>
</code></pre></div><p>and then, once compiled, link the corresponding object file <code>foo.o</code> into the compiled object file where it is used in the linking phase, <code>main.o</code>:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token comment">// main.cpp --> main.o</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">"foo.h"</span> <span class="token comment">// foo's prototype declaration is "hidden" in here</span></span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">foo</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">// foo is valid to call because its prototype declaration was beforehand.</span>
<span class="token comment">// the prototype and body definitions of foo are linked through the object files</span>
</code></pre></div><p>An “unresolved external symbol” error occurs when the function <strong>prototype</strong> and <strong>call</strong> exist, but the function <strong>body</strong> is not defined. These can be trickier to resolve as the compiler won't report the error until the final linking stage, and it doesn't know which line to jump to in the code to show the error.</p> <h2 id="preprocessor"><a href="#preprocessor" class="header-anchor">#</a> Preprocessor</h2> <p>The preprocessor is an important part of the <a href="http://stackoverflow.com/documentation/c%2B%2B/8209/the-c-compilation-process#t=201702151617103019866" target="_blank" rel="noopener noreferrer">compiler.<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p>It edits the source code, cutting some bits out, changing others, and adding other things.</p> <p>In source files, we can include preprocessor directives. These directives tells the preprocessor to perform specific actions. A directive starts with a # on a new line.
Example:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">ZERO</span> <span class="token expression"><span class="token number">0</span></span></span>
</code></pre></div><p>The first preprocessor directive you will meet is probably the</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><something></span></span>
</code></pre></div><p>directive. What it does is takes all of <code>something</code> and inserts it in your file where the directive was. The <a href="http://stackoverflow.com/documentation/c%2B%2B/206/getting-started-with-c-language/774/hello-world" target="_blank" rel="noopener noreferrer">hello world<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> program starts with the line</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iostream></span></span>
</code></pre></div><p>This line adds the <a href="http://stackoverflow.com/documentation/c%2B%2B/206/getting-started-with-c-language/25460/function" target="_blank" rel="noopener noreferrer">functions<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> and objects that let you use the standard input and output.</p> <p>The C language, which also uses the preprocessor, does not have as many <a href="http://stackoverflow.com/documentation/c%2B%2B/7211/header-files#t=20170215162302552333" target="_blank" rel="noopener noreferrer">header files<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> as the C++ language, but in C++ you can use all the C header files.</p> <p>The next important directive is probably the</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">something</span> <span class="token expression">something_else</span></span>
</code></pre></div><p>directive. This tells the preprocessor that as it goes along the file, it should replace every occurrence of <code>something</code> with <code>something_else</code>. It can also make things similar to functions, but that probably counts as advanced C++.</p> <p>The <code>something_else</code> is not needed, but if you define <code>something</code> as nothing, then outside preprocessor directives, all occurrences of <code>something</code> will vanish.</p> <p>This actually is useful, because of the <code>#if</code>,<code>#else</code> and <code>#ifdef</code> directives. The format for these would be the following:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">if</span> <span class="token expression">something<span class="token operator">==</span><span class="token boolean">true</span></span></span>
<span class="token comment">//code</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">else</span></span>
<span class="token comment">//more code</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifdef</span> <span class="token expression">thing_that_you_want_to_know_if_is_defined</span></span>
<span class="token comment">//code</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span>
</code></pre></div><p>These directives insert the code that is in the true bit, and deletes the false bits. this can be used to have bits of code that are only included on certain operating systems, without having to rewrite the whole code.</p> <h4 id="remarks"><a href="#remarks" class="header-anchor">#</a> Remarks</h4> <p>The 'Hello World' program is a common example that can be simply used to check compiler and library presence. It uses the C++ standard library, with <code>std::cout</code> from <code><iostream></code>, and has only one file to compile, minimizing the chance of possible user error during compilation.</p> <p>The process for compiling a C++ program inherently differs between compilers and operating systems. The topic <a href="https://stackoverflow.com/documentation/c%2B%2B/4708/compiling-and-building" target="_blank" rel="noopener noreferrer">Compiling and Building<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> contains the details about how to compile C++ code on different platforms for a variety of compilers.</p></ul></li></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/devtut/generate/edit/master/docs/cpp/getting-started-with-cpp.md" target="_blank" rel="noopener noreferrer">Edit this page on GitHub</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
←
<a href="/cpp/" class="prev router-link-active">
Disclaimer
</a></span> <span class="next"><a href="/cpp/literals.html">
Literals
</a>
→
</span></p></div> </main></div><div class="global-ui"><!----></div></div>
<script src="/assets/js/app.1779e102.js" defer></script><script src="/assets/js/3.2cfa8016.js" defer></script><script src="/assets/js/660.ebbb5e71.js" defer></script>
</body>
</html>