c 程序如何在退出时打印 exit() return 值

How can a c program can print exit() return value while it exits itself

我正在尝试从 exit() 系统调用中打印出 return 代码。但是由于程序退出,它没有机会打印出它会被杀死的东西。我的问题是如何在程序中打印 return 代码。

y = syscall(NR_mycall); //makes an exit in this second
printf("%s\n", strerror(y));

果然没有输出! :U(

如果您从程序的多个位置调用 exit(),并且您想知道在这些退出调用中使用了哪个退出代码,那么不要使用 exit,而是使用 setjmpmain 中并在代码中使用 longjmp 现在有 exit 并在 longjmp 调用中传递 error/exit 代码。现在 main 将知道使用了哪个 exit/error 代码。

您无法打印退出状态。我的建议是在执行将打印它的文件后,使用 echo $? 检查终端中的退出状态。或者转到 http://bashrcgenerator.com/ 并为自己制作个人提示并添加 'exit status' 以便您每次都能在提示中看到退出状态。