- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
-1)结果 1 (正确),但实际上它是 0 (假)在 Visual Studio 2017 中。 我认为(1 ? (int)1-6ren">
我认为((1 ? (int)1 : (unsigned int)2) > -1)
结果 1
(正确),但实际上它是 0
(假)在 Visual Studio 2017 中。
我认为(1 ? (int)1 : (unsigned int)2)
的值(value)应该是 (int)1
,因为 1 ?
是真的,1 > -1
会是真的。
不知道为什么这个表达式最后的结果是false。
当我尝试像 ((int)(1 ? (int)1 : (unsigned int)2) > -1)
那样转换时, 它返回 1
(正确)。
signed int test = -1;
signed int si = 1;
unsigned int ui = 2;
printf("%d\n", ((1 ? si : ui) > test));
return 0;
我希望输出为 1
, 但实际输出是 0
.
最佳答案
a 的类型? b : c
不依赖于 a
。它由 b
和 c
的类型无条件确定。完整的规则很复杂,但是对于算术操作数,类型由通常的算术转换决定。实际上,这两个操作数被转换为通用类型。对于 int
和 unsigned int
,结果类型为 unsigned int
。
条件运算符,? :
在 C 2018 标准的第 6.5.15 条中进行了描述。第 4 段说结果“转换为下面描述的类型”。
第 5 段描述了算术类型、结构和 union 的结果:
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. If both the operands have structure or union type, the result has that type. If both operands have void type, the result has void type.
算术类型是整数和浮点类型,根据 6.2.5 18。(这些包括实数和复数类型。)通常的算术转换在 6.3.1.8 1 中描述,它们是(在我的总结中,未引用) ):
long double
,则结果是long double
。double
,则结果是double
。float
,则结果是float
。int
或更窄(意味着更少的位数或相同数量的位数但有符号而不是无符号),结果是 int
。否则,如果两者都是 unsigned int
或更窄的值,则结果为 unsigned int
。否则,结果是更宽的类型。structure、union、void规则很明确:两个操作数必须有相同的类型,那就是结果。
第 6 段描述了指针的结果:
If both the second and third operands are pointers or one is a null pointer constant and the other is a pointer, the result type is a pointer to a type qualified with all the type qualifiers of the types referenced by both operands. Furthermore, if both operands are pointers to compatible types or to differently qualified versions of compatible types, the result type is a pointer to an appropriately qualified version of the composite type; if one operand is a null pointer constant, the result has the type of the other operand; otherwise, one operand is a pointer to void or a qualified version of void, in which case the result type is a pointer to an appropriately qualified version of void.
总而言之,就是说:
const
、volatile
、restrict
或 _Atomic
),请将它们包含在结果类型。关于c - "? :"发生了什么?我不知道返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54062382/
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 9年前关闭。 Improve this que
我有一堆 php 脚本计划在 CentOS 机器上的 cron 中每隔几分钟运行一次。我希望每个脚本在启动时自我检查它的前一个实例是否仍在运行,如果是则停止。 最佳答案 我这样做是为了管理任务并确保它
是否有 bash 命令、程序或 libusb 函数(尽管我没有找到)来指示 USB 设备的 OUT 或 IN 端点是什么? 例如,libusb_interface_descriptor(来自 libu
我如何知道 NSTextField 何时成为第一响应者(即当用户单击它来激活它时,但在他们开始输入之前)。我尝试了 controlTextDidBeginEditing 但直到用户键入第一个字符后才会
我怎么知道我的代码何时完成循环?完成后我还得再运行一些代码,但只有当我在那里写的所有东西都完成后它才能运行。 obj.data.forEach(function(collection) {
我正在使用音频标签,我希望它能计算播放了多少次。 我的代码是这样的: ; ; ; 然后在一个javascript文件中 Var n=0; function doing(onplaying)
我正在尝试向 Package-Explorer 的项目上下文菜单添加一个子菜单。但是,我找不到该菜单的 menuid。 所以我的问题是如何在 eclipse 中找到 menuid? 非常感谢您的帮助。
我有一个名为“下一步”的按钮,它存在于几个 asp.net 页面中。实际上它是在用户控件中。单击“下一步”时,它会调用 JavaScript 中的函数 CheckServicesAndStates。我
我正在尝试在 Visual Studio 中使用 C++ 以纳秒为单位计算耗时。我做了一些测试,结果总是以 00 结尾。这是否意味着我的处理器(Ryzen 7-1800X)不支持 ~1 纳秒的分辨率,
我有一个自定义 ListView ,其中包含一些元素和一个复选框。当我点击一个按钮时。我想知道已检查的元素的位置。下面是我的代码 public class Results extends ListAc
如何在使用 J2ME 编写的应用程序中获取网络运营商名称? 我最近正在尝试在 Nokia s40 上开发一个应用程序,它应该具有对特定网络运营商的独占访问权限。有没有这样的API或库? 最佳答案 没有
我使用服务器客户端组件,当在此组件的 TransferFile 事件中接收文件时,我使用警报消息组件。所以我希望,如果用户单击警报消息,程序将继续执行 TransferFile 事件中的代码,以在单击
如果我创建一个类A具有一些属性,例如 a, b, c我创建对象 A x1; A x2; A x3; ... A xN 。有没有办法在同一个类中创建一个方法来检索我创建的所有对象?我想创建类似 stat
我正在制作一个应用程序,其中包含相同布局的 81 个按钮。它们都被称为我创建的名为“Tile”的对象。问题是这些图 block 存储在数组中,因此我需要知道以 int 格式单击了哪个按钮才能调用图 b
UIProgressView有这个setProgress:animated: API。 有没有办法确切知道动画何时停止? 我的意思是这样的? [myProgress setProgress:0.8f
我正在使用两个 jQuery 队列,我希望其中一个队列在另一个队列完成后出队。我怎么知道第一个是否完成?我应该使用第三个队列吗?! 这是我所拥有的: var $q = $({}); $q.que
jQuery 中有没有一种方法可以知道是否至少有一个复选框已被选中? 我有一个包含很多复选框的表单,每个复选框都不同。 我需要一种 jQuery 的方式来表达这样的内容,这就是逻辑: If at le
给定 2 个选择 100 50 100 在这两种情况下,我都想在 .example 中获取数字,使用相同的选择器或者以某种方式知道 .no-text 和 之间的区别。带文字 执行
我在我的应用程序中使用 System.ComponentModel.BindingList 作为 DataGridView.DataSource。该列表非常大,需要几秒钟才能绘制到 DataGridV
我想知道用户在 Android 中选择的默认键盘。我知道我可以使用 InputMethodManager 访问已启用的输入法列表,但我想知道用户当前使用的是哪一个。 到目前为止,我已经尝试获取当前的输
我是一名优秀的程序员,十分优秀!