请解释一下这段代码的输出

时间:2022-03-20 17:26:12
#include<stdio.h>
int GetPositiveInt(void);

int main(void)
{
    int num = GetPositiveInt();
    printf("%d",num);
    return 0;
}

int GetPositiveInt(void)
{
    int n;
    do
    {
        printf("Enter a positive number : ");
        scanf("%d",&n);
    }while(n<=0);

}

The output is the value of n but I don't know how it is returned to num. Does scanf() and printf() affect the return value of a function?

输出是n的值,但我不知道它是如何返回num的。 scanf()和printf()是否会影响函数的返回值?

If I add a statement like printf("%d",7); after while then the output is 71,no idea how 1 comes.

如果我添加一个像printf(“%d”,7)的语句;之后输出为71,不知道1是怎么来的。

This is my output. This is for reference only .

这是我的输出。这仅供参考。

Text as requested :-

要求的文字: -

usr@usr:~/C_programs$ gcc -std=c99 return.c -o return

usr @ usr:〜/ C_programs $ gcc -std = c99 return.c -o return

usr@usr:~/C_programs$ ./return

Enter a positive number : 45

输入正数:45

45usr@usr:~/C_programs$ ./return

Enter a positive number : 89

输入正数:89

89usr@usr:~/C_programs$

5 个解决方案

#1


In the following function:

在以下功能中:

int GetPositiveInt(void)
{
  int n;
  do
  {
    printf("Enter a positive number : ");
    scanf("%d",&n);
  }while(n<=0);

}

the function is expected to return an int value, whereas there is no return statement. You probably wanted to return the value of n. This can be achieved by adding the suitable return statement:

该函数应返回一个int值,而没有return语句。您可能想要返回n的值。这可以通过添加合适的return语句来实现:

 return n;

Like this:

int GetPositiveInt(void)
{
  int n;
  do
  {
    printf("Enter a positive number : ");
    scanf("%d",&n);
  }while(n<=0);

  return n;
}

#2


TL;DR Your code produces undeifined behavior.

TL; DR您的代码会产生未定义的行为。

You're missing a return statement in GetPositiveInt() function. Without having an explicit return statement to return a value, collecting the return value of the function call in the caller function and using that leads to undefined behaviour.

你在GetPositiveInt()函数中缺少一个return语句。如果没有显式的return语句来返回值,则在调用函数中收集函数调用的返回值并使用它会导致未定义的行为。

Ref: As per the C11 standard document, chapter §6.9.1,

参考:根据C11标准文件,章节§6.9.1,

If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

如果到达终止函数的},并且调用者使用函数调用的值,则行为是未定义的。


FWIW, only in case of main() , addition of return statement is not mandatory. From same document, chapter 5.1.2.2.3, for main() function

FWIW,仅在main()的情况下,添加return语句不是强制性的。对于main()函数,从同一文档的第5.1.2.2.3章开始

reaching the } that terminates the main function returns a value of 0.

到达终止main函数的}返回值0。

#3


no idea how '1' comes.

不知道'1'是怎么来的。

Because you did not return anything in your method GetPositiveInt - and the behavior is undefined:

因为您没有在方法GetPositiveInt中返回任何内容 - 并且行为未定义:

int GetPositiveInt(void)
{
    int n;
    do
    {
        printf("Enter a positive number : ");
        scanf("%d",&n);
    }while(n<=0);

    return n; //<---Add this line

}

C99 6.9.1/12 "Function definitions" says:

C99 6.9.1 / 12“功能定义”说:

If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

如果到达终止函数的},并且调用者使用函数调用的值,则行为是未定义的。

#4


It prints 0 for me.

它为我打印0。

As explained in the other answers, the lack of the return statement in a function that is declared to return something is signaled by the compiler with a warning but the generated code exposes undefined behaviour. This means the function can return any value, the compiler cannot guarantee anything about it.

正如在其他答案中所解释的那样,在声明返回某些内容的函数中缺少return语句由编译器发出警告信号,但生成的代码会暴露未定义的行为。这意味着函数可以返回任何值,编译器无法保证它的任何内容。

You can think of the value returned by a function as a hidden local variable of that function. Since the return statement is missing, the hidden variable is not initialized and its value is whatever garbage data happened to be in the memory at the time when the function was called.

您可以将函数返回的值视为该函数的隐藏局部变量。由于缺少return语句,隐藏变量未初始化,其值是在调用函数时发生在内存中的任何垃圾数据。

In practice, the return statement is translated into a processor instruction that copies the returned value into one of the general-purpose processor's registries (usually %eax on x86). The calling function (main() in your code) gets the values from that register and uses it.

实际上,return语句被转换为处理器指令,该指令将返回的值复制到通用处理器的注册表之一(通常是x86上的%eax)。调用函数(代码中的main())从该寄存器中获取值并使用它。

Because your function GetPositiveInt() does not contain a return statement, the compiler skips generating the instruction that puts the correct value in %eax and the function completes its execution letting in %eax whatever value happened to be there as a result of earlier processing.

因为您的函数GetPositiveInt()不包含return语句,所以编译器会跳过生成将正确值放在%eax中的指令,并且函数完成执行,让%eax因为早期处理而发生的任何值。

#5


This is a case of undefined behaviour because you are missing the return statement.however I can explain why 1 is coming.

这是一个未定义行为的情况,因为你错过了return语句。但是我可以解释为什么会出现1。

The printf function returns the number of characters printed by it.For example

printf函数返回由它打印的字符数。例如

 printf("%d",printf("%d",7));

first printf prints 7 and returns 1 as number of characters printed. So output is 71.

首先printf打印7并返回1作为打印的字符数。所以输出是71。

#1


In the following function:

在以下功能中:

int GetPositiveInt(void)
{
  int n;
  do
  {
    printf("Enter a positive number : ");
    scanf("%d",&n);
  }while(n<=0);

}

the function is expected to return an int value, whereas there is no return statement. You probably wanted to return the value of n. This can be achieved by adding the suitable return statement:

该函数应返回一个int值,而没有return语句。您可能想要返回n的值。这可以通过添加合适的return语句来实现:

 return n;

Like this:

int GetPositiveInt(void)
{
  int n;
  do
  {
    printf("Enter a positive number : ");
    scanf("%d",&n);
  }while(n<=0);

  return n;
}

#2


TL;DR Your code produces undeifined behavior.

TL; DR您的代码会产生未定义的行为。

You're missing a return statement in GetPositiveInt() function. Without having an explicit return statement to return a value, collecting the return value of the function call in the caller function and using that leads to undefined behaviour.

你在GetPositiveInt()函数中缺少一个return语句。如果没有显式的return语句来返回值,则在调用函数中收集函数调用的返回值并使用它会导致未定义的行为。

Ref: As per the C11 standard document, chapter §6.9.1,

参考:根据C11标准文件,章节§6.9.1,

If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

如果到达终止函数的},并且调用者使用函数调用的值,则行为是未定义的。


FWIW, only in case of main() , addition of return statement is not mandatory. From same document, chapter 5.1.2.2.3, for main() function

FWIW,仅在main()的情况下,添加return语句不是强制性的。对于main()函数,从同一文档的第5.1.2.2.3章开始

reaching the } that terminates the main function returns a value of 0.

到达终止main函数的}返回值0。

#3


no idea how '1' comes.

不知道'1'是怎么来的。

Because you did not return anything in your method GetPositiveInt - and the behavior is undefined:

因为您没有在方法GetPositiveInt中返回任何内容 - 并且行为未定义:

int GetPositiveInt(void)
{
    int n;
    do
    {
        printf("Enter a positive number : ");
        scanf("%d",&n);
    }while(n<=0);

    return n; //<---Add this line

}

C99 6.9.1/12 "Function definitions" says:

C99 6.9.1 / 12“功能定义”说:

If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

如果到达终止函数的},并且调用者使用函数调用的值,则行为是未定义的。

#4


It prints 0 for me.

它为我打印0。

As explained in the other answers, the lack of the return statement in a function that is declared to return something is signaled by the compiler with a warning but the generated code exposes undefined behaviour. This means the function can return any value, the compiler cannot guarantee anything about it.

正如在其他答案中所解释的那样,在声明返回某些内容的函数中缺少return语句由编译器发出警告信号,但生成的代码会暴露未定义的行为。这意味着函数可以返回任何值,编译器无法保证它的任何内容。

You can think of the value returned by a function as a hidden local variable of that function. Since the return statement is missing, the hidden variable is not initialized and its value is whatever garbage data happened to be in the memory at the time when the function was called.

您可以将函数返回的值视为该函数的隐藏局部变量。由于缺少return语句,隐藏变量未初始化,其值是在调用函数时发生在内存中的任何垃圾数据。

In practice, the return statement is translated into a processor instruction that copies the returned value into one of the general-purpose processor's registries (usually %eax on x86). The calling function (main() in your code) gets the values from that register and uses it.

实际上,return语句被转换为处理器指令,该指令将返回的值复制到通用处理器的注册表之一(通常是x86上的%eax)。调用函数(代码中的main())从该寄存器中获取值并使用它。

Because your function GetPositiveInt() does not contain a return statement, the compiler skips generating the instruction that puts the correct value in %eax and the function completes its execution letting in %eax whatever value happened to be there as a result of earlier processing.

因为您的函数GetPositiveInt()不包含return语句,所以编译器会跳过生成将正确值放在%eax中的指令,并且函数完成执行,让%eax因为早期处理而发生的任何值。

#5


This is a case of undefined behaviour because you are missing the return statement.however I can explain why 1 is coming.

这是一个未定义行为的情况,因为你错过了return语句。但是我可以解释为什么会出现1。

The printf function returns the number of characters printed by it.For example

printf函数返回由它打印的字符数。例如

 printf("%d",printf("%d",7));

first printf prints 7 and returns 1 as number of characters printed. So output is 71.

首先printf打印7并返回1作为打印的字符数。所以输出是71。