gpt4 book ai didi

const string plus boolean 在 C 中复数

转载 作者:太空狗 更新时间:2023-10-29 17:19:20 26 4
gpt4 key购买 nike

我很惊讶字符串加 boolean 值具有类似三元运算的效果:

int apple = 2;                                                                      
printf("apple%s\n", "s" + (apple <= 1));

如果apple <= 1 , 它将打印苹果。为什么会这样?

最佳答案

因为条件的计算结果为 0 或 1,并且字符串 "s" 在 0 终止符之前恰好包含一个字符。所以如果 bool 为 false,"s"+ bool 将评估为 "s" 的地址,并且对其后面的一个字符,地址如果为真,则为 0 终止符。

这是一个很酷的 hack,但永远不要认真使用这样的代码。

关于const string plus boolean 在 C 中复数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11278737/

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