c program does not return 0 by default

I’m using pgcc 19.4 on linux.

$ pgcc --version

pgcc 19.4-0 LLVM 64-bit target on x86-64 Linux -tp sandybridge 
PGI Compilers and Tools
Copyright (c) 2019, NVIDIA CORPORATION.  All rights reserved.

I compiled the following program.

int main() {}

The main function doesn’t have an explicit return statement. When I run it, the return value I get is 32.

If I understand correctly, the C standard (C99 and C11) says it should return 0.

Compiling this as a C++ program returns 0 as expected.

Is this a bug in the C compiler or intentional or did I understand the C standard incorrectly?

Hi amarinph,

From From WG14/N1124 Committee Draft — May 6, 2005 ISO/IEC 9899:TC2

5.1.2.2.3 Program termination
If the return type of the main function is a type compatible with int, a return from the
initial call to the main function is equivalent to calling the exit function > with the value
returned by the main function as its argument reaching the } that terminates the
main function returns a value of 0.
> If the return type is not compatible with int, the
termination status returned to the host environment is unspecified.

Given this, I do agree that I don’t think we’re following the standard here and still using the C89 semantics where the return value would be undefined in this case. I filed a problem report (TPR#27284) and sent it to our compiler engineers for evaluations.

However, there’s a good chance we wont fix this. We’ll be deprecating the current pgcc compiler soon and replacing it with a new C compiler that’s currently under beta. You can try out the new compiler by using the “pgcc18” driver instead of “pgcc”. pgcc18 will return 0 when the user doesn’t specify the return code.

For example:

% cat test.c
#include <stdlib.h>
#include <stdio.h>
int main () {
    printf("Hello\n");
}

% pgcc test.c -V19.4 ; a.out ; echo $?
Hello
6
% pgcc18 test.c -V19.4 ; a.out ; echo $?
Hello
0

-Mat

Oh, nice. Thanks.

How stable is the beta and do you know how long will it be in beta?

In particular, if I’d like to compile some programs for which I have tests to check their outputs, is it relatively safe to completely replace pgcc with pgcc18?

In particular, if I’d like to compile some programs for which I have tests to check their outputs, is it relatively safe to completely replace pgcc with pgcc18?

pgcc18 is actually our C++ compiler with C mode enabled (i.e. the “–c” flag) so is stable. Not to say that you wont encounter issues, you might, but the compiler itself is stable. Of course if you do find issues, please let us know and we can work on addressing them.

Note that when we do make the switch, most likely later this year or early next, we’ll make pgcc18->pgcc, and then move the current pgcc to a legacy name. So you may need to again update makefiles, etc., with the different names later.

-Mat