C 和 C++ 中的静态和外部全局变量

2022-01-05 00:00:00 static c global-variables c++ extern

我制作了 2 个项目,第一个用 C 语言,第二个用 C++,都具有相同的行为.

C 项目:

header.h

int varGlobal=7;

ma??in.c

#include #include #include "header.h"空函数(int i){静态 int a=0;一个++;int t=i;i=varGlobal;varGlobal=t;printf("调用#%d:
i=%d
varGlobal=%d

",a,i,varGlobal,t);}int main() {功能(4);功能(6);功能(12);返回0;}

C++ 项目:

header.h

int varGlobal=7;

ma??in.cpp

#include #include "header.h"使用命名空间标准;空函数(int i){静态 int a=0;int t=i;一个++;i=varGlobal;varGlobal=t;cout<<"呼叫#"<<a<<":"<<endl<<"i="<<i<<endl<<"varGlobal="<<;varGlobal<<endl<<endl;}int main() {功能(4);功能(6);功能(12);返回0;}

我读到全局变量在 C 中默认为 extern,在 C++ 中默认为 static;那么为什么 C++ 代码有效?

我的意思是 int varGlobal=7; 与 static int varGlobal=7; 相同,如果它是静态的,那么它只能在它声明的文件中使用,对吗?

解决方案

全局变量在 C 和 C++ 上默认不是 extern 也不是 static.当您将变量声明为 static 时,您将其限制为当前源文件.如果您将其声明为 extern,则表示该变量存在,但在其他地方定义,如果您没有在其他地方定义它(没有 extern 关键字)) 你会得到一个链接错误(找不到符号).

当您有更多包含该标题的源文件时,您的代码将中断,在链接时您将多次引用 varGlobal.如果您将其声明为 static,那么它将适用于多个源(我的意思是,它将编译和链接),但每个源都有自己的 varGlobal.

你可以在 C++ 中做而你不能在 C 中做的是在头文件中将变量声明为 const,如下所示:

const int varGlobal = 7;

并包含在多个来源中,而不会在链接时破坏内容.这个想法是替换旧的 C 风格 #define 为常量.

如果你需要一个在多个源上可见的全局变量而不是const,在header中声明为extern,然后定义它,这次不用extern关键字, 在源文件上:

多个文件包含的标题:

extern int varGlobal;

在您的源文件之一中:

int varGlobal = 7;

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:
i=%d
varGlobal=%d

",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?

解决方案

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;

相关文章