相关疑难解决方法(0)

如何使用extern在源文件之间共享变量?

我知道C中的全局变量有时会有extern关键字.什么是extern变量?宣言是什么样的?它的范围是什么?

这与跨源文件共享变量有关,但这是如何工作的?我在哪里用extern

c global-variables extern

942
推荐指数
13
解决办法
67万
查看次数

C中暂定定义背后的基本原理是什么?

考虑以下程序.这会给出任何编译错误吗?

#include <stdio.h>
int s=5;
int s;
int main(void)
{
     printf("%d",s);
}
Run Code Online (Sandbox Code Playgroud)

乍一看似乎编译器会给出变量重定义错误,但程序根据C标准完全有效.(请参见http://ideone.com/Xyo5SY上的现场演示).

暂定定义是没有存储类说明符且没有初始化程序的任何外部数据声明.

C99 6.9.2/2

对于具有文件范围而没有初始化程序且没有存储类指定程序或存储类指定程序静态的对象的标识声明构成了一个暂定的定义.如果翻译单元包含一个或多个用于标识符的暂定定义,并且翻译单元不包含该标识符的外部定义,则该行为就像翻译单元包含该标识符的文件范围声明一样,其复合类型为翻译单元的结尾,初始化程序等于0.

我的问题是,允许暂定定义的理由是什么?在C中有没有用过这个?为什么C允许暂定?

c variables definition language-lawyer

21
推荐指数
2
解决办法
566
查看次数

外部声明中的警告

#include<stdio.h>
#include<stdlib.h>
#define GREY 1
#define BLACK 0
#define WHITE 2
typedef struct node * graph;
typedef struct stack * snode;

graph cnode(int data);          //cnode is to create a node for graph
void cgraph(void);
struct node {
        int data, color;
        struct node *LEFT, *RIGHT, *TOP, *DOWN;
};//this structure defines a node of the graph

struct stack {
struct stack *priv;
struct cgraph *graph_node;
};// this is to define a structure which should hold node of a structure

    extern snode sroot; …
Run Code Online (Sandbox Code Playgroud)

c

12
推荐指数
3
解决办法
3万
查看次数