简体   繁体   English

APUE第7章中的hello world退出,代码为0

[英]hello world in APUE Chapter 7 exit with code 0

APUE chapter 7 says this snippet would exit with exit code other than 0 (13 in example). APUE第7章说,该代码段将以非0的退出代码退出(例如13)。 But it exit with code 0 on my computer: 但是它在我的计算机上以代码0退出:

#include    <stdio.h>

main()
{
    printf("hello, world\n");
}

Compile & Running environment: 编译运行环境:

Darwin cuixiaochens-MacBook-Pro.local 17.4.0 Darwin Kernel Version 17.4.0: Sun Dec 17 09:19:54 PST 2017; root:xnu-4570.41.2~1/RELEASE_X86_64 x86_64

Gcc version: Gcc版本:

♪ apue.3e git:(master) gcc -v Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 9.0.0 (clang-900.0.39.2) Target: x86_64-apple-darwin17.4.0 Thread model: posix InstalledDir: /Library/Developer/CommandLineTools/usr/bin

APUE is trying to illustrate a real phenomenon, and it's still possible to observe that phenomenon with a modified program. APUE试图说明一个真实的现象,但仍然可以使用经过修改的程序来观察该现象。

Normally, when a function declared to return a value (ie return type other than void ) returns by "falling off the end", its return value is "indeterminate," and a program that observes that value has "undefined behavior." 通常,当声明要返回值的函数(即void以外的其他返回类型)通过“下降”返回时,其返回值为“不确定”,并且观察到该值的程序具有“未定义的行为”。 A common way for that undefined behavior to manifest, is for the function's return value to be equal to the return value of the last call made within the function, because that value is still in the return-value register. 表现该未定义行为的一种常见方式是使函数的返回值等于函数内最后一次调用的返回值,因为该值仍在返回值寄存器中。 APUE was trying to demonstrate this by falling off the end of main , but since C99 there is a special rule for falling off the end of main which says that this behaves as-if main had properly returned 0. Many compilers apply this rule even in their C89 mode, since in C89 it was undefined, so they're allowed to do anything they like. APUE试图通过掉落main的末端来证明这一点,但是由于C99有一个掉落main末端的特殊规则,它说这就像main已经正确返回0一样。许多编译器甚至在他们的C89模式,因为在C89中它是未定义的,所以允许他们做任何喜欢的事情。

So, to observe the phenomenon APUE wants you to observe, you need to use a function other than main , and you also need to take steps to prevent interprocedural analysis, such as splitting the program into two translation units and then not using link-time optimization. 因此,要观察APUE希望您观察到的现象,您需要使用main以外的功能,并且还需要采取措施防止过程间分析,例如将程序分为两个翻译单元,然后不使用链接时间。优化。

/* file1.c */
#include <stdio.h>

int print_hello(void)
{
    printf("hello world\n");
}

/* file2.c */
#include <stdio.h>
extern int print_hello(void);

int main(void);
{
    int n = print_hello();
    printf("print_hello returned %d\n", n);
    return 0;
}

Compile each file separately with eg cc -c and then link them together as a third operation. cc -c分别编译每个文件,然后将它们链接在一起作为第三次操作。 On my computer, with my compiler, this prints 在我的计算机上,使用我的编译器,将打印

hello world
print_hello returned 12

Please do keep in mind that this program does have undefined behavior. 请记住,该程序确实具有未定义的行为。 On a computer with a different calling convention (eg a SPARC) it might print a garbage number; 在具有不同调用约定的计算机(例如SPARC)上,它可能会打印一个垃圾号码。 if I had allowed interprocedural optimization to happen, the compiler might have deduced that the only way the program doesn't have undefined behavior is if it's never run, and therefore emitted no code at all for it. 如果我允许进行过程间优化,则编译器可能推断出程序没有未定义行为的唯一方法是它永远不会运行,因此根本不会发出任何代码

(The program would not have undefined behavior if the value returned by print_hello was never used. This is another special case in the language, for the sake of backward compatibility with code written before void return types were a possibility.) (如果从不使用print_hello返回的值,则程序不会有未定义的行为。这是该语言的另一种特殊情况,为了向后兼容在可能使用void返回类型之前编写的代码。)

Tour APUE chapter is wrong. 游览APUE章是错误的。 The main function in the C language is guaranteed by the standard to return zero if there is no return statement( if we consider standard no older than 20 years) 如果没有返回语句,则标准保证C语言的主函数返回零(如果我们认为标准不超过20年)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM