[英]What is the difference between printf to return?
Compute the sum of the two given integer values.计算两个给定 integer 值的总和。 If the two values are the same, then return triple their sum.
如果两个值相同,则返回它们总和的三倍。
int main() {
printf("%d\n", test(1, 2));
printf("%d", test(2, 2));
}
int test(int x, int y) {
int sum;
if (x == y) {
sum = (x + y) * 3;
printf("sum = %d\n", sum);
} else {
sum = (x + y);
printf("sum = %d", sum);
}
}
When I run this code, I get the wrong output, but when I use return
statements instead of the printf
, I get the right input.当我运行这段代码时,我得到了错误的 output,但是当我使用
return
语句而不是printf
时,我得到了正确的输入。 Why?为什么?
Since test
does not return a value (there is no return
statement), the attempt to reference the return value results in undefined behavior.由于
test
不返回值(没有return
语句),因此尝试引用返回值会导致未定义的行为。 It might be easier to understand what is happening in your program if you make a few minor modifications:如果您进行一些小的修改,可能会更容易理解程序中发生的事情:
#include <stdio.h>
int
test(int x, int y)
{
int sum = x + y;
if( x==y ){
sum *= 3;
}
printf("sum = %d XXX ", sum);
return sum;
}
int
main(void)
{
printf("** %d **\n", test(1, 2));
printf("** %d **\n", test(2, 2));
return 0;
}
The output of the above program is:上述程序的output为:
sum = 3 XXX ** 3 **
sum = 12 XXX ** 12 **
The test
in the argument list to printf
in main
is called before the printf
in main
can be called.在调用
main
中的printf
之前调用main
中printf
的参数列表中的test
。 The printf
inside the test
function executes and prints sum =... XXX
. test
printf
中的 printf 执行并打印sum =... XXX
。 The function returns a value which is passed as an argument to the printf
in main which prints **... **
. function 返回一个值,该值作为参数传递给 main 中的
printf
,打印**... **
。
Your program is just a little bit wrong.你的程序有点错误。 Keeping with the general structure of your code, here are two ways that you can rewrite it.
与您的代码的一般结构保持一致,您可以通过以下两种方式重写它。
#1: Instead of returning a value, the test
function prints a value. #1:
test
function 没有返回值,而是打印了一个值。 Since test
does not return a value, you must put the keyword void
before the function name.由于
test
不返回值,因此必须在 function 名称之前放置关键字void
。 To get the values to print, the main
function must call the test
function.要获取要打印的值,
main
function 必须调用test
function。
int main() {
/** calls the test function,
then the test function prints
the sum value **/
test(1,2);
test(2,2);
return 0;
}
void test(int x, int y) {
int sum;
if (x == y) {
sum = (x + y) * 3;
printf("sum = %d\n", sum); /** prints the sum value**/
} else {
sum = (x + y);
printf("sum = %d", sum); /** prints the sum value**/
}
}
#2 The test
function returns the sum
value. #2
test
function 返回sum
值。 Since sum
is an int
, you must put the keyword int
before the function name.由于
sum
是一个int
,您必须将关键字int
放在 function 名称之前。 The main
function gets the value of sum
by calling the test
function. Then main
prints this value. main
function通过调用test
function得到sum
的值,然后main
打印这个值。
int main() {
/** prints the value returned
by the test function **/
printf("%d\n", test(1,2));
printf("%d", test(2,2));
return 0;
}
int test(int x, int y) {
int sum;
if (x == y) {
sum = (x + y) * 3;
} else {
sum = (x + y);
}
/** returns the value of sum **/
return sum;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.