有没有任何C实现有一个无用的单位`int`位域?

有没有任何C实现有一个无用的单位`int`位域?,第1张

概述6.7.2.1p9 of n1570说: A member of a structure or union may have any complete object type other than a variably modified type.123) In addition, a member may be declared to consist of a specified number 6.7.2.1p9 of n1570说:

A member of a structure or union may have any complete object type other than a
variably modifIEd type.123) In addition,a member may be declared to consist of a
specifIEd number of bits (including a sign bit,if any). Such a member is called a
bit-fIEld ;124) its wIDth is preceded by a colon.

我是否正确理解这表明结构中的单个成员{int bit:1;可能是一个标志位?

如果是这种情况,则遵循这样的位字段可能存储在某些实现上的唯一值是0和-0,其中-0可能与0存储或陷阱表示无法区分.

是否有任何实际的实现只能将一个值分配给这样的位域?

解决方法 gcc 4.9.2怎么样?

/* gcc -std=c11 -pedantic-errors -Wextra -Werror=all test.c */#include <stdio.h>#include <string.h>struct foo {    int bit:1;};int main() {    struct foo f;    f.bit = 0;    f.bit = 1;    printf("%i\n",f.bit);    return 0;}

编译它会发出:

$gcc -std=c11 -pedantic-errors -Wextra -Werror=all test.ctest.c: In function ‘main’: test.c:12:10: warning: overflow inimplicit constant conversion [-Woverflow]  f.bit = 1;          ^

运行它发出:

$./a.out -1
总结

以上是内存溢出为你收集整理的有没有任何C实现有一个无用单位`int`位域?全部内容,希望文章能够帮你解决有没有任何C实现有一个无用的单位`int`位域?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1216776.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存