c中的函数可以返回long int吗?

Can long int be returned by a function in c?

假设我们想通过一个函数return一个长整数。 怎么做? 有效吗?

long int function()
{
  long int b;
  b=1000000000;
  return b;
} 

是的,有效。

只要您重新调整适当的 long int 值(我们可以看到,您正在做 #)并将其捕获到另一个 long int(你要保重),应该没问题的。


#) 根据 C11 标准,LONG_MAX 等于 +21474836471000000000 小于该标准。章节§5.2.4.2.1,供参考。

C 标准规定 long int 的最小大小为 32 位,以保证可以表示 [−2147483647, +2147483647] 范围内的值的方式存储。 (请注意,一个 2s 的补码表示会得到 −2147483648)。所以1,000,000,000赋值给一个long int总是定义的

返回 long int 的值副本也是明确定义的。

请注意,如果您没有初始化 b(即如果您省略了语句b=1000000000;),那么您的程序的行为将未定义。

函数可以 return 几乎任何类型:

6.9.1

The return type of a function shall be void or a complete object type other than array type.

就是这样。

c中的函数可以返回long int吗?