Sti*_*bba -1 c printf function
#include<stdio.h>
int func(int x){
printf("Print\n");
return x;
}
void main(){
printf("The value of x is %d",func(50)); /* Print is printed first then the value of x */
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该程序的输出是:
Print
The value of x is 50
Run Code Online (Sandbox Code Playgroud)
所以我的问题是为什么功能打印Print之后它是打印The value of x is 50。为什么The value of x is不在之前打印,因为函数是在语句之后调用的。
让我们分析一下你的程序的流程。
main()正在被呼叫。
printf()正在被呼叫。
2.1 在计算 的参数时printf(),func(50)遇到函数调用
2.2func(50)函数开始执行。它打印Print并返回x[50]
最后,printf()inmain()看起来像printf("The value of x is %d",50);[50 是函数调用的返回值func(50)]
发生第二次打印,打印The value of x is 50
所以,你的输出屏幕看起来像
Print
The value of x is 50
Run Code Online (Sandbox Code Playgroud)
注意:正如评论中已经提到的,使用int main()而不是void main(). 否则,return 0就没有意义。