添加布尔值
bool comapare(int val)
{
if(val>5)
{
return true;
}
return false;
}
int myFunct(int x,int y)
{
int count = 0;
count = (int)compare(x) + (int)compare(y);
return count;
}
我想添加如上所述的布尔值。对其进行类型转换是最好的方法吗?任何评论。
bool comapare(int val)
{
if(val>5)
{
return true;
}
return false;
}
int myFunct(int x,int y)
{
int count = 0;
count = (int)compare(x) + (int)compare(y);
return count;
}
I want to add bool values as above. Is typecasting it as such the best approach to do so. Any comments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无需强制转换值。您可以只写 由于
整数提升,操作数将提升为
int
类型,结果也将具有int
类型。由于计数不能为负值,因此最好将其声明为无符号整数类型,例如
size_t
或至少unsigned int
。另外,比较函数可以写得更简单。
在 C 语言中,类型
bool
是整数类型_Bool
的 typedef 名称。There is no need to cast the values. You could just write
The operands will be promoted to the type
int
due to the integer promotions and the result also will have the typeint
.And as the count can not have a negative value it is better to declare it as having an unsigned integer type as for example
size_t
or at leastunsigned int
.Also the function compare can be written simpler
In C the type
bool
is a typedef name for the integer type_Bool
.我明白你为什么这样做,但读起来很混乱。顺便说一句,不需要演员阵容,
效果很好,但我会这样做
。意图更清晰。
I see why you are doing it but its confusing to read. BTW the cast is not needed
works fine, but I would do
The intent is much clearer.