Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static and extern global variables in C and C++

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?

like image 396
Cristi Avatar asked Jun 15 '12 17:06

Cristi


People also ask

What is the difference between global static and extern?

static means a variable will be globally known only in this file. extern means a global variable defined in another file will also be known in this file, and is also used for accessing functions defined in other files.

What is static and global variable in C?

Static variables can be declared both inside and outside the main function while the global variables are always declared outside the main function.

What is static extern variable in C?

Static variables in C have the following two properties: They cannot be accessed from any other file. Thus, prefixes “ extern ” and “ static ” cannot be used in the same declaration. They maintain their value throughout the execution of the program independently of the scope in which they are defined.

Is extern a global variable in C?

External variables are also known as global variables. These variables are defined outside the function. These variables are available globally throughout the function execution.


2 Answers

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; 
like image 172
fbafelipe Avatar answered Sep 27 '22 17:09

fbafelipe


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.

like image 45
Mark Ransom Avatar answered Sep 27 '22 17:09

Mark Ransom