例如:当我将浮点数转换为int时,我为什么要使用:
int i = (int) round(f);
而不是:
int i = int round(f);
答案 0 :(得分:1)
C中的int和(int)有什么区别?
int
是内置类型整数,(int)
是Type Casting。
答案 1 :(得分:1)
在C float中,数据值需要更多存储空间,因此当您将浮点值存储在整数变量中时,您应该键入cat来截断小数部分,然后再存储它。
即。 int i =(int)round(f);
答案 2 :(得分:0)
你真的试图编译后者吗?在你报告编译器的内容之前,我不会回答这个问题。
对于第一个版本:那是typecast(读取有关语义),告诉编译器将值转换为括号中的类型。否则,编译器可能(应该)抱怨为较小的分配更“强大”的类型。
请注意,“强大”在这里意味着类型可以包含更大范围的值;不一定更高的精度。
编辑:是的,你会得到后者的“预期表达”。因为那简直无效。所以,简单而完整的答案是:因为第二件事是无效的C而且甚至不会编译!
答案 3 :(得分:0)
使用// notifications are keyed by ID
var notifications = {};
// load notifications from storage if needed
// newList is the array of new notifications
// newNotificationFcn is an optional callback function, which
// takes the new notification as its only argument
function updateNotifications(newList, newNotificationFcn) {
var doNotify = (typeof newNotificationFcn === 'function');
newList.forEach(function(curr) {
if (!notifications.hasOwnProperty(curr.id) {
// this is a new notification; do something
if (doNotify) {
newNotificationFcn(curr);
}
notifications[curr.id] = curr;
}
});
// store away notifications as needed
}
是一个定义。
int i
将>>舍入函数的结果转换为整数,可能会将(int) round(f)
的结果截断为整数
答案 4 :(得分:-2)
按照惯例,在float上执行(int)是将float类型转换为int。我不太确定你为什么要做int _ = int _,尽管它也可以正常工作。但按照惯例,您使用
的语法进行类型转换的许多语言(有例外)id='hidePopup'
希望有所帮助!