From f949c77f59b0f2898799c18f39758bd09f093bd6 Mon Sep 17 00:00:00 2001 From: Hari <3788945+science-enthusiast@users.noreply.github.com> Date: Thu, 9 Mar 2023 13:57:00 +0100 Subject: [PATCH] Update const-cpp.md More clarity about extern const in C and C++: declaration, definition etc. --- docs/cpp/const-cpp.md | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/docs/cpp/const-cpp.md b/docs/cpp/const-cpp.md index 1f1fbde94aa..fc2837dbf0c 100644 --- a/docs/cpp/const-cpp.md +++ b/docs/cpp/const-cpp.md @@ -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; @@ -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;