ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

C有像C这样的一个定义规则吗?

2019-10-06 07:16:48  阅读:200  来源: 互联网

标签:c-3 variable-declaration c language-lawyer global-variables


最近,我发现有些情况绝对会违反C的ODR,但会在C编译器中编译好.

例如,这个奇怪的场景(和我一起):

来源1

int var_global=-3;

来源2

#include <stdio.h>
#include <conio.h>

unsigned int var_global;

int main() {    

 printf("%d \n",var_global);
 getch();
 return 0;

}

我的打印结果是-3(即使在Source 2中var_global是无符号的)并且没有关于var_global重新定义的错误.

我知道C与C有不同的规则,但我认为它不是那么不同.

我有谷歌和阅读了很多结果,但没有官方结果like this的C.

所以问题是:

C有像C这样的一个定义规则吗?

和:

什么叫官方?

我需要它与C的规则进行比较,这样我才能更深入地理解这两种语言.

p / s:我使用Visual Studio 2010编译上面的代码.

解决方法:

我认为您正在寻找的是C11标准的章节§6.2.7,兼容型和复合型,(强调我的)

All declarations that refer to the same object or function shall have compatible type;
otherwise, the behavior is undefined.

并且与兼容类型有关,

Two types have compatible type if their types are the same.

在您的情况下,int和unsigned int不是兼容的类型.因此undefined behavior.

只是为了在源2中添加一点清晰度,unsigned int var_global;是一个声明,它与其他声明(和定义)不匹配,因此,这是UB.

那说,一个声明就好

 printf("%d \n",var_global);

将始终认为%d的参数为int类型.如果类型和格式说明符不匹配,您将再次调用undefined behavior.

编辑:

编辑后,答案是,使用-fno-common来获得所需的错误. (我相信,缺少外部是你所困扰的).

引自在线GCC手册,

-fno-common

In C code, controls the placement of uninitialized global variables. Unix C compilers have traditionally permitted multiple definitions of such variables in different compilation units by placing the variables in a common block. This is the behavior specified by -fcommon, and is the default for GCC on most targets. On the other hand, this behavior is not required by ISO C, and on some targets may carry a speed or code size penalty on variable references. The -fno-common option specifies that the compiler should place uninitialized global variables in the data section of the object file, rather than generating them as common blocks. This has the effect that if the same variable is declared (without extern) in two different compilations, you get a multiple-definition error when you link them. In this case, you must compile with -fcommon instead. Compiling with -fno-common is useful on targets for which it provides better performance, or if you wish to verify that the program will work on other systems that always treat uninitialized variable declarations this way.

我不知道在C标准中有任何提及“一个定义规则”的措辞,但沿着这条线,您可以查看附件§J.5.11,多个外部定义,

There may be more than one external definition for the identifier of an object, with or
without the explicit use of the keyword extern; if the definitions disagree, or more than
one is initialized, the behavior is undefined.

标签:c-3,variable-declaration,c,language-lawyer,global-variables
来源: https://codeday.me/bug/20191006/1859258.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有