在Powershell中运行代码时,函数“return”(C)不会出现(Windows 11)

kfgdxczn  于 2024-01-06  发布在  Shell
关注(0)|答案(1)|浏览(106)

我试图创建一个函数,它必须返回一个整数,即使我的程序没有任何错误,在PowerShell中运行代码时也不会出现任何错误。这里有一个例子。有人知道为什么吗?

#include <stdio.h>

int main()
{
    return 1;
}

字符串
enter image description here
是不是我应该更改某种控制台的配置设置?

g6ll5ycj

g6ll5ycj1#

正如一些评论所指出的,C中的return不向控制台输出任何东西。它只是从函数main返回值1。
main返回的整数表示程序的exit status。您可以在运行程序后在终端中输入以下内容来检查它:

# In POSIX shell:
echo $?

# In PowerShell:
echo $LASTEXITCODE

字符串
要让程序向终端写入内容,可以使用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"
}

相关问题