Skip to content

Update const-cpp.md #4457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions docs/cpp/const-cpp.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ int main()

## C and C++ `const` differences

When you declare a variable as **`const`** in a C source code file, you do so as:
When you define a **`const`** variable in a C source code file, you do so as:

```C
const int i = 2;
Expand All @@ -159,13 +159,18 @@ You can then use this variable in another module as follows:
extern const int i;
```

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

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

If you wish to declare an **`extern`** variable in a C++ source code file for use in a C source code file, use:
```cpp
extern const int i;
```

If you wish to define an **`extern`** variable in a C++ source code file for use in a C source code file, use:

```cpp
extern "C" const int x=10;
Expand Down