Skip to content

Commit f949c77

Browse files
Update const-cpp.md
More clarity about extern const in C and C++: declaration, definition etc.
1 parent 07f4092 commit f949c77

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

docs/cpp/const-cpp.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ int main()
147147
148148
## C and C++ `const` differences
149149
150-
When you declare a variable as **`const`** in a C source code file, you do so as:
150+
When you define a **`const`** variable in a C source code file, you do so as:
151151
152152
```C
153153
const int i = 2;
@@ -159,13 +159,18 @@ You can then use this variable in another module as follows:
159159
extern const int i;
160160
```
161161

162-
But to get the same behavior in C++, you must declare your **`const`** variable as:
162+
But to get the same behavior in C++, you must define your **`const`** variable as:
163163

164164
```cpp
165165
extern const int i = 2;
166166
```
167+
Similar to C, you can then use this variable in another module as follows:
167168

168-
If you wish to declare an **`extern`** variable in a C++ source code file for use in a C source code file, use:
169+
```cpp
170+
extern const int i;
171+
```
172+
173+
If you wish to define an **`extern`** variable in a C++ source code file for use in a C source code file, use:
169174

170175
```cpp
171176
extern "C" const int x=10;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy