43

I made 2 projects, the first one in C and the second one in C++, both work with same behavior.

C project:

header.h

int varGlobal=7;

main.c

#include <stdio.h>
#include <stdlib.h>
#include "header.h"

void function(int i)
{
    static int a=0;
    a++;
    int t=i;
    i=varGlobal;
    varGlobal=t;
    printf("Call #%d:\ni=%d\nvarGlobal=%d\n\n",a,i,varGlobal,t);
}

int main() {
    function(4);
    function(6);
    function(12);
    return 0;
}

C++ project:

header.h

int varGlobal=7;

main.cpp

#include <iostream>
#include "header.h"
using namespace std;

void function(int i)
{
    static int a=0;
    int t=i;
    a++;
    i=varGlobal;
    varGlobal=t;
    cout<<"Call #"<<a<<":"<<endl<<"i="<<i<<endl<<"varGlobal="<<varGlobal<<endl<<endl; 
}

int main() {
    function(4);
    function(6);
    function(12);
    return 0;
}

I read that global variables are extern by default and in C and static by default in C++; so why does the C++ code works?

I mean int varGlobal=7; is same as static int varGlobal=7; and if it's static then it can be used only in the file it was declared, right?

Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
Cristi
  • 1,195
  • 6
  • 17
  • 24

2 Answers2

101

Global variables are not extern nor static by default on C and C++. When you declare a variable as static, you are restricting it to the current source file. If you declare it as extern, you are saying that the variable exists, but are defined somewhere else, and if you don't have it defined elsewhere (without the extern keyword) you will get a link error (symbol not found).

Your code will break when you have more source files including that header, on link time you will have multiple references to varGlobal. If you declare it as static, then it will work with multiple sources (I mean, it will compile and link), but each source will have its own varGlobal.

What you can do in C++, that you can't in C, is to declare the variable as const on the header, like this:

const int varGlobal = 7;

And include in multiple sources, without breaking things at link time. The idea is to replace the old C style #define for constants.

If you need a global variable visible on multiple sources and not const, declare it as extern on the header, and then define it, this time without the extern keyword, on a source file:

Header included by multiple files:

extern int varGlobal;

In one of your source files:

int varGlobal = 7;
Enosh Cohen
  • 369
  • 2
  • 11
fbafelipe
  • 4,862
  • 2
  • 25
  • 40
  • 3
    As I just discovered, there *is* a difference between C and C++ but it only applies to **const** objects: http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fconst_cv_qualifier.htm – Mark Ransom Jun 15 '12 at 18:38
  • If you are going to put things in headers, some compilers/linkers will complain about multiply defined symbols. Example: `const int varGlobal=7;` Because you are declaring it `const` it can be treated as a global constant, but some compilers are not that smart. What does work, always, is to declare things `static const` in the header file, like so: `static const int varGlobal = 7;` That way even if the compiler is stupid, it won't add the symbol to the external symbols table and the linker will never see multiply defined symbols. – steveha Jun 18 '12 at 22:39
  • @steveha Declaring a constant as "static const" will cause annoying warnings on files that include the header, but do not use the constant (of course, depending on your compiler and warnings enabled). – fbafelipe Jun 19 '12 at 00:24
  • 1
    @fbafelipe That's right. I forgot about it because there is usually a way to disable the warnings and I always use it. For example, with GCC you can use `__attribute__ ((unused))` to disable that warning. So I don't actually declare things `static const`, I declare them `LIBCONST` where `LIBCONST` is a `#define` macro I have made that declares `static __attribute__ ((unused))` for GCC, and just `static` for other platforms that don't warn. – steveha Jun 19 '12 at 01:01
  • By the way: the best reason to do this, instead of just using `#define`, is that when you are debugging the debugger can display the value of a constant. With `#define` the constant is textually substituted in, so the name is gone and just the number is part of your program. So if you used `#define` to define a constant `FOO`, you can't examine the value of `FOO` from the debugger; you must look it up in the source code. But if you did `static const int FOO = 3;` in the header file, you can then examine `FOO` in the debugger and see that its value is 3. – steveha Jun 19 '12 at 19:37
  • Contrary to the opening line in this answer, in C, any variable or object declared outside of a function, and NOT declared static, is by default global and has extern scope. http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=%2Fcom.ibm.vacpp6m.doc%2Flanguage%2Fref%2Fclrc03extsc.htm – user2548100 Feb 04 '14 at 20:09
  • +1 for `If you declare it as static, then it will work with multiple sources but each source will have its own varGlobal` – Emadpres Jul 17 '14 at 10:58
  • So, a variable marked as extern should never be initialized with a value, let's say `extern int x = 10;` it is not possible? – lmiguelvargasf Apr 21 '16 at 16:03
  • 3
    @lmiguelvargasf If you want to initialize with a value, initialize in the c/cpp file, not the header. So in the header you have `extern int x;` and in the source file you have `int x = 10;` – fbafelipe Apr 22 '16 at 05:01
  • 2
    From C++11 on you usually use `constexpr int constGlobal = 7;` to replace an old style `#define constGlobal = 7`. It is guaranteed to be a compile time constant (and can be used as template parameter, etc.), but still type safe. – John Nov 21 '16 at 07:48
  • @fbafelipe relatively good answer, only to be clear regarding "declare" vs. define, change your 2nd sentence to read: " If you **declare** it as extern, you are saying that the variable exists, but is **defined** somewhere else, and if you don't have it **defined** elsewhere (without the extern keyword) you will get a link error (symbol not found)." Also, very good that you brought out the point about "... but each source will have its own varGlobal" if "static" is used. – Daniel Goldfarb May 02 '18 at 14:08
8

When you #include a header, it's exactly as if you put the code into the source file itself. In both cases the varGlobal variable is defined in the source so it will work no matter how it's declared.

Also as pointed out in the comments, C++ variables at file scope are not static in scope even though they will be assigned to static storage. If the variable were a class member for example, it would need to be accessible to other compilation units in the program by default and non-class members are no different.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • 3
    Also, the OP is incorrect in the assumption that globally declared variable are static by default in C++. They have external *linkage*, just as in C. Global variables have static *duration* in C and C++ though, so this can easily be confusing. – nos Jun 15 '12 at 17:57
  • So the rule appllies for *.c an *.cpp files but not for *.h files? Thank you! – Cristi Jun 15 '12 at 17:59
  • Another important thing: his code would break if he had multiple sources including that same header. – fbafelipe Jun 15 '12 at 18:21