gpt4 book ai didi

c - C中这个表达式的返回类型是什么?

转载 作者:太空宇宙 更新时间:2023-11-04 06:13:44 24 4
gpt4 key购买 nike

If i is an int variable and f is a float variable, what is the type of the conditional expression (i > 0 ? i : f)?

考虑 i 的任意值,因为我只想知道表达式的类型。我不知道我们如何检查这个表达式的类型,因为如果我们将结果存储在 int 变量中,我们得到的输出是整数,如果结果存储在 float 变量,输出为 float

问题取自 K. N. King 的书。

最佳答案

6.5.15 条件运算符,第 4 段:

If both the second and third operands have arithmetic type, the result type that would be determined by the usual arithmetic conversions, were they applied to those two operands, is the type of the result.

然后在6.3.1.8 Usual arithmetic conversions, paragraph 1:

Otherwise, if the corresponding real type of either operand is float, the other operand is converted, without change of type domain, to a type whose corresponding real type is float.

因此,表达式的类型是float

关于c - C中这个表达式的返回类型是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50915008/

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