odi*_*n19 -1 c function return-value
#include <stdio.h>
#include <stdlib.h>
int f(int x) {
return x;
}
int main ( int argc,char * argv[]) {
int a=4;
f(a);
printf("PASSED!\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当你打电话f(a)而没有把它分配给任何东西时会发生什么?
当您调用具有返回值的函数而不将其分配给任何变量时会发生什么?
不需要使用或分配函数的返回值.它被忽略(通常是安静的).
该功能仍然执行,其副作用仍然存在.
考虑3个函数:int scanf(),int f()和int printf(),它们的返回值都被忽略但函数仍然被执行.
int a=4;
scanf("%d", &a);
f(a);
printf("PASSED!\n");
Run Code Online (Sandbox Code Playgroud)
特别是在强健代码中忽略返回值是不好的scanf().
显式忽略函数的结果有时表示(void)安静该警告.
(void) f(a);
Run Code Online (Sandbox Code Playgroud)