gpt4 book ai didi

我们可以在 c 中对枚举变量进行类型转换吗

转载 作者:太空狗 更新时间:2023-10-29 16:09:02 26 4
gpt4 key购买 nike

我在面试中被问到这个问题:

Is it possible to typecast a enum variable to some other type?

我真的不知道怎么回答。

最佳答案

是的。在 C 语言中,enum 类型只是隐藏在 int 中。将它们强制转换为您想要的任何内容。

enum cardsuit {
Clubs = 1,
Diamonds,
Hearts,
Spades
};
enum cardsuit trump = Diamonds;
int d = (int)trump; /* 'd' would be 2 */

关于我们可以在 c 中对枚举变量进行类型转换吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8012647/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com