3

Is there any difference in C++ between global variable/const and global static variable/const? Declared in cpp file or a header file.

static const int x1 = someFunction(5);
const int x2 = someFunction(6);
static int x3 = someFunction(5);
int x4 = someFunction(6);

int main()
{
...
Random
  • 249
  • 1
  • 10
  • https://en.cppreference.com/w/cpp/language/storage_duration – Kevin Dec 29 '21 at 18:20
  • @cigien you closed mine and linked as a solution a question that doesn't have a solution. – Random Dec 29 '21 at 20:41
  • Duplicate closures indicate that a question is a duplicate. Whether there are solutions on the target is not really relevant (apart from the minimum requirement that there be at least one upvoted answer on the target). – cigien Dec 29 '21 at 21:07
  • @cigien Anoop Rana's answer here is much better. And Kevin's answer provides good info. – Random Dec 29 '21 at 21:11
  • Yes, that's true. I've found a better target though, and adjusted this question's target. That one addresses the question comprehensively. – cigien Dec 29 '21 at 21:15

2 Answers2

2

Case I: For const objects

Similarity

In both versions, the variables have internal linkage. That is, both x1 and x2 have internal linkage.

Difference

In case of static const int x1 the variable is explicitly static while in case of const int x2 the variable is implicitly static. But note that they both still have internal linkage.

Case II:For nonconst objects

Similarity

Both x3 and x4 are nonconst meaning we can modify them.

Difference

The variable x3 has internal linkage while the variable x4 has external linkage.

Jason
  • 36,170
  • 5
  • 26
  • 60
  • I've deleted my wrong answer because I missed that `const int x2` has internal linkage [because it's const](https://stackoverflow.com/questions/998425/why-does-const-imply-internal-linkage-in-c-when-it-doesnt-in-c). Just thought I'd mention it in a comment in case other people make that same mistake :) – Kevin Dec 29 '21 at 18:43
  • @Kevin Yes i noticed that mistake in your answer. – Jason Dec 29 '21 at 18:44
  • @Kevin I haven't seen your answer. Maybe you better undelete it? As I'm not only asking about const case, if there is a difference between const and non-const – Random Dec 29 '21 at 18:50
  • 1
    Sure, I'll undelete it – Kevin Dec 29 '21 at 18:51
  • 1
    @Random I have added the explanation for `nonconst` objects at the end of my answer. Check it out. – Jason Dec 29 '21 at 19:02
0

NOTE: This only applies when the variables aren't const. I've undeleted this answer at the OP's request

static int x1 has internal linkage, meaning it's only accessible by name from the translation unit it's defined in (this cpp file). If this line is in a header file, every file that includes the header will get its own copy of the variable.

int x2 has external linkage, meaning another translation unit (cpp file) could have extern int x2; in global scope and access the variable. If this line is in a header file, and multiple files include the header, you're violating the One Definition Rule since the variable is being defined once per include. You must instead use extern int x2; in the header, and then in a single cpp file have your int x2 = ...; to avoid multiple definitions.

See https://en.cppreference.com/w/cpp/language/storage_duration

Kevin
  • 6,993
  • 1
  • 15
  • 24