我正在尝试创建一个函数,它必须返回一个整数,尽管我的程序没有任何错误,但在 PowerShell 中运行代码时什么也没有出现。这是一个例子。有谁知道为什么吗?
#include <stdio.h>
int main()
{
return 1;
}
我是否应该更改某种控制台的配置设置?
正如一些评论所指出的,C 中的
return
不会向控制台输出任何内容。它只是从函数 main
返回值 1。
从
main
返回的整数表示程序的退出状态。运行程序后,您可以通过在终端中输入以下内容来检查它:
echo $?
要让程序向终端写入内容,您可以使用 C 标准库中的
printf
函数。
#include <stdio.h> // Include the standard "io" library
int main()
{
// The standard programmer greeting :)
printf("Hello, world!\n");
// Or, if you want to write an integer
printf("%d\n", 1); // "%d" is a format specifier
return 0; // 0 exit code means "OK"
}