English 中文(简体)
《公约》第2条
原标题:Declaration Versus Definition in c
  • 时间:2011-11-08 17:49:11
  •  标签:
  • c

最近,在学习制定方案时,我注意到一些被认为有趣的东西。 我读过,像<代码>int i=0;”这样的声明是强行定义的唯一途径,而诸如<代码>extern int i;就意味着强迫声明。 发言如<代码>int i;取决于具体情况。 但是,如果将外部与初始化结合起来,如<代码>extern int i=13;,则会出现什么情况。 竞争者发出警告。 但这一规则是怎样的?

问题回答

This is a Coding style warning.
The argument for this is the code is valid, but extremely unidiomatic for C since "extern" is generally expected to mean that the declaration is not providing a definition of the object.

extern int i=13;  

声明和定义<代码>i 而:

extern int i;      

仅宣布变式<代码>i。

A specific bug 45977 has been raised on GCC on the same but it is still shows Unconfirmed Status.

bug报告指出,该守则与C标准一致。 讨论会详细讨论了这一问题。


For Standerdese Fans:
Relevant Section References are:
ansi c99 Standard 6.2.2: Linkage Of Identifiers and
ansi c99 Standard 6.9.2.4

When you declare a variable you just bind a name to it.

当你确定一个变量时,你将保留记忆。

When you declare a variable as extern you are telling the compiler "this is defined elsewhere and will be available on linking time", so it s OK to use it.

如果你想从不同的方案获得特定变量,则使用外部。 由于你在方案中没有这方面的定义,你的督导员给你错误。

在C中,定义只是一项用于分配储存的声明(无论是因为它有初步的准备者,还是因为它是作为定义使用的暂定定义)。 因此,你对声明所能做的一切(例如,它有<代码>extern的存储),你也可以对定义做事。

注:这与C++不同。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签