请解释代码的输出
Please explain this output of the code
#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);
}
输出的是n
的值,但是不知道怎么返回给num
。
scanf()
和 printf()
会影响函数的 return
值吗?
如果我添加像printf("%d",7)
这样的语句;在 while
之后输出是 71
,不知道 1
是怎么来的。
This is我的输出。仅供参考。
要求的文本:-
usr@usr:~/C_programs$ gcc -std=c99 return.c -o return
usr@usr:~/C_programs$ ./return
Enter a positive number : 45
45usr@usr:~/C_programs$ ./return
Enter a positive number : 89
89usr@usr:~/C_programs$
TL;DR 您的代码产生了未定义的行为。
您在 GetPositiveInt()
函数中缺少 return
语句。在没有对 return 值的显式 return
语句的情况下,在调用函数中收集函数调用的 return 值并使用它会导致 undefined behaviour.
参考:根据 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,仅在 main()
的情况下,添加 return
语句是 不是强制性的 。来自同一文档,第 5.1.2.2.3 章,用于 main()
函数
reaching the }
that terminates the main function returns a value of 0.
no idea how '1' comes.
因为您在方法中没有 return 任何东西 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" 说:
If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.
在以下函数中:
int GetPositiveInt(void)
{
int n;
do
{
printf("Enter a positive number : ");
scanf("%d",&n);
}while(n<=0);
}
该函数应 return
一个 int
值,而没有 return
语句。您可能想要 return n
的值。这可以通过添加合适的 return
语句来实现:
return n;
像这样:
int GetPositiveInt(void)
{
int n;
do
{
printf("Enter a positive number : ");
scanf("%d",&n);
}while(n<=0);
return n;
}
这是一个未定义行为的案例,因为您缺少 return statement.however 我可以解释为什么要出现 1。
printf函数returns it.For示例打印的字符数
printf("%d",printf("%d",7));
first printf 打印 7 和 returns 1 作为打印的字符数。所以输出是 71.
它为我打印 0
。
如其他答案中所述,在声明为 return 的函数中缺少 return
语句会被编译器发出警告信号,但生成的代码会暴露未定义的行为.这意味着该函数可以 return 任何值,编译器不能保证它的任何内容。
您可以将函数编辑的值 return 视为该函数的隐藏局部变量。由于缺少 return
语句,因此隐藏变量未初始化,其值是调用函数时内存中恰好存在的垃圾数据。
实际上,return
语句被翻译成处理器指令,将 returned 值复制到通用处理器的注册表之一(通常 %eax
x86
).调用函数(代码中的main()
)从该寄存器获取值并使用它。
因为您的函数 GetPositiveInt()
不包含 return
语句,编译器会跳过生成将正确值放入 %eax
的指令,函数完成其执行让 %eax
由于早期处理而碰巧存在的任何值。
#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);
}
输出的是n
的值,但是不知道怎么返回给num
。
scanf()
和 printf()
会影响函数的 return
值吗?
如果我添加像printf("%d",7)
这样的语句;在 while
之后输出是 71
,不知道 1
是怎么来的。
This is我的输出。仅供参考。
要求的文本:-
usr@usr:~/C_programs$ gcc -std=c99 return.c -o return
usr@usr:~/C_programs$ ./return
Enter a positive number : 45
45usr@usr:~/C_programs$ ./return
Enter a positive number : 89
89usr@usr:~/C_programs$
TL;DR 您的代码产生了未定义的行为。
您在 GetPositiveInt()
函数中缺少 return
语句。在没有对 return 值的显式 return
语句的情况下,在调用函数中收集函数调用的 return 值并使用它会导致 undefined behaviour.
参考:根据 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,仅在 main()
的情况下,添加 return
语句是 不是强制性的 。来自同一文档,第 5.1.2.2.3 章,用于 main()
函数
reaching the
}
that terminates the main function returns a value of 0.
no idea how '1' comes.
因为您在方法中没有 return 任何东西 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" 说:
If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.
在以下函数中:
int GetPositiveInt(void)
{
int n;
do
{
printf("Enter a positive number : ");
scanf("%d",&n);
}while(n<=0);
}
该函数应 return
一个 int
值,而没有 return
语句。您可能想要 return n
的值。这可以通过添加合适的 return
语句来实现:
return n;
像这样:
int GetPositiveInt(void)
{
int n;
do
{
printf("Enter a positive number : ");
scanf("%d",&n);
}while(n<=0);
return n;
}
这是一个未定义行为的案例,因为您缺少 return statement.however 我可以解释为什么要出现 1。
printf函数returns it.For示例打印的字符数
printf("%d",printf("%d",7));
first printf 打印 7 和 returns 1 作为打印的字符数。所以输出是 71.
它为我打印 0
。
如其他答案中所述,在声明为 return 的函数中缺少 return
语句会被编译器发出警告信号,但生成的代码会暴露未定义的行为.这意味着该函数可以 return 任何值,编译器不能保证它的任何内容。
您可以将函数编辑的值 return 视为该函数的隐藏局部变量。由于缺少 return
语句,因此隐藏变量未初始化,其值是调用函数时内存中恰好存在的垃圾数据。
实际上,return
语句被翻译成处理器指令,将 returned 值复制到通用处理器的注册表之一(通常 %eax
x86
).调用函数(代码中的main()
)从该寄存器获取值并使用它。
因为您的函数 GetPositiveInt()
不包含 return
语句,编译器会跳过生成将正确值放入 %eax
的指令,函数完成其执行让 %eax
由于早期处理而碰巧存在的任何值。