简体   繁体   English

键入C函数的返回值是否可以接受?

[英]Is it acceptable to type-cast the return value of a C function?

A colleague of mine claims that type-casting the return value of a function is undefined in C, however I cannot find anything to support his claim. 我的一位同事声称在C中输入函数的返回值是未定义的,但我找不到任何支持他的主张的内容。

I know that type-casting a function pointer is bad, see here: Casting a function pointer to another type 我知道类型转换函数指针是坏的,请参见此处: 将函数指针转换为另一种类型

But I want to confirm that the following code would be acceptable and well defined: 但我想确认以下代码是可以接受的并且定义明确:

// Some function
int ret = foo(bar);

// Some variable with some different type
unsigned int a;

// Cast function result to match variable type
a = (unsigned int) foo(bar);

Other than what's answered in the comments, you can back your argument with some article like this , that repeatedly talks about casting return values. 除了什么在评论回答,您可以与一些物品像回到你的说法 ,大约铸造返回值是多次会谈。

One trivial example that we use every now and then is to cast the return value of the round() function. 我们偶尔使用的一个简单示例是强制转换round()函数的返回值。

This is so trivial that you can even see it mentioned in the C Standard, page 567 of this pdf . 这是微不足道的,你甚至可以在本标准的第567页C标准中看到它。

In the above conversions from floating to integer, the use of (cast)x can be replaced with (cast)round(x), (cast)rint(x), (cast)nearbyint(x), (cast)trunc(x), (cast)ceil(x), or (cast)floor(x) 在从浮动到整数的上述转换中,(cast)x的使用可以替换为(cast)round(x),(cast)rint(x),(cast)nearbyint(x),(cast)trunc(x) ),(cast)ceil(x)或(cast)floor(x)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM