有没有任何C实现有一个无用的单位`int`位域?
发布时间:2020-12-16 09:58:08 所属栏目:百科 来源:网络整理
导读: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).
6.7.2.1p9 of n1570说:
我是否正确理解这表明结构中的单个成员{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("%in",f.bit); return 0; } 编译它会发出: $gcc -std=c11 -pedantic-errors -Wextra -Werror=all test.c test.c: In function ‘main’: test.c:12:10: warning: overflow in implicit constant conversion [-Woverflow] f.bit = 1; ^ 运行它发出: $./a.out -1 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |