enum DIRECTION {EAST,WEST,norTH,SOUTH};int main(voID) { enum DIRECTION currentDirection = 10; printf("%d\n",currentDirection); return 0;}
输出:
10
Each enumerated type shall be compatible with char,a signed integer type,or an
unsigned integer type. The choice of type is implementation-defined,110) […]
脚注110说:
An implementation may delay the choice of which integer type until all enumeration constants have been seen.
该标准并未说明您不允许指定枚举声明中指定值之外的值,尽管在附录I中的常见警告中它确实提出了这样的警告,但并不要求:
A value is given to an object of an enumerated type other than by assignment of an
enumeration constant that is a member of that type,or an enumeration object that has
the same type,or the value of a function that returns the same enumerated type (6.7.2.2).
gcc不会产生警告,虽然使用-Wassign-enum标志或-Weverything标志,但它看起来与此类似:
warning: integer constant not in range of enumerated type ‘enum DIRECTION’ [-Wassign-enum]
并且您可以使用-Werror使其成为错误.
基思提出了两个有趣的观察:
>使用-Werror会使clang不符合,因为代码有效C.> enum DIRECTION currentDirection = 128;具有实现定义的行为,因为类型很可能是char.
总结以上是内存溢出为你收集整理的C中的枚举不会在无效输入上引发错误全部内容,希望文章能够帮你解决C中的枚举不会在无效输入上引发错误所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)