signed int i = -1;
但是,我试过这个:
signed int i = -2;unsigned int i = -2;int i = -2;
并且所有3个案例用printf打印出-2(“%d”,i);.为什么?
解决方法 由于您确认使用以下方式打印:printf("%d",i);
在未签名的情况下,这是undefined behavior.这将在draft C99 standard第7.19.6.1节中介绍fprintf函数,该函数还包含格式说明符的printf,它在第9段中说明:
If a conversion specification is invalID,the behavior is undefined.248)[…]
3.4.3节中定义的标准未定义行为如下:
behavior,upon use of a nonportable or erroneous program construct or of erroneous data,
for which this International Standard imposes no requirements
并进一步说明:
Possible undefined behavior ranges from ignoring the situation completely with unpredictable results,to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message),to terminating a translation or execution (with the issuance of a diagnostic message).
最后,我们可以看到int与signed int相同.我们可以通过转到6.7.2类型说明符来看到这一点,在第2段中它将int分组如下:
int,signed,or signed int
后来在第5段中说:
总结Each of the comma-separated sets designates the same type,except that for bit-fIEld[…]
以上是内存溢出为你收集整理的我什么时候可以逃脱未通过签名声明int?全部内容,希望文章能够帮你解决我什么时候可以逃脱未通过签名声明int?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)