Strange return codes from old but good C program
andrew clarke
mail at ozzmosis.com
Tue May 19 11:04:59 UTC 2015
On Mon 2015-05-18 13:55:07 UTC+0200, Polytropon (freebsd at edvax.de) wrote:
> On Sun, 17 May 2015 19:45:09 +0000 (UTC), Will Parsons wrote:
> > I don't have the actual C standard, but Harbison & Steele's "C - A
> > Reference Manual" (which I think can be relied on) states "If the end
> > of the body of *main* is reached without returning, it is treated as
> > if *return 0;* were executed".
After a bit of Googling, my understanding is that this is true for C99
(and probably C++98) but the earlier C89 (ANSI X3.159-1989) standard
had no implicit return 0 for main().
See below for test results.
Ian may want to recompile with -std=c99 for main() to implicitly
return 0. Or he could just add "return 0" at the end, probably. I
haven't really looked at the code.
$ rm -f *.o ssystem
$ make -f unixl.mak CC=gcc CFLAGS=-std=c99
> In that case, no random return codes should appear. It also
> doesn't meet the little test I've wrote (which again matches
> with the initially described problem).
>
> I have written (haha) the following "test case":
>
> % cat returntest.c
> main() {
> int i, j;
> for(i = 0, j = 0; i < 100; i++)
> j += i;
> }
>
> There are two "error" in it: main() doesn't have a return
> type assigned, so per standard (int) will be assumed. And
> there is no return statement.
>
> Compiler is system's gcc (older system, obviously):
>
> % cc -Wall -o returntest returntest.c
> returntest.c:1: warning: return type defaults to 'int'
> returntest.c: In function 'main':
> returntest.c:5: warning: control reaches end of non-void function
>
> This is what we expect.
>
> But the program can be run, and we see:
>
> % ./returntest ; echo $?
> 99
Clang 3.4.1 on FreeBSD 10.1 amd64:
$ clang -Wall -o returntest returntest.c
returntest.c:1:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main()
^~~~
1 warning generated.
$ ./returntest ; echo $?
0
=====================================================================
GNU C 4.8.4_3 on FreeBSD 10.1 amd64:
$ gcc48 -Wall -o returntest returntest.c
returntest.c:1:1: warning: return type defaults to 'int' [-Wreturn-type]
main()
^
returntest.c: In function 'main':
returntest.c:6:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
$ ./returntest ; echo $?
99
=====================================================================
GNU C 4.8.4_3 on FreeBSD 10.1 amd64 using -std=c99:
$ gcc48 -std=c99 -Wall -o returntest returntest.c
returntest.c:1:1: warning: return type defaults to 'int' [enabled by default]
main()
^
$ ./returntest ; echo $?
0
=====================================================================
GNU C 4.8.2 on Ubuntu 14.04 x86_64:
$ gcc -Wall -o returntest returntest.c
returntest.c:1:1: warning: return type defaults to ‘int’ [-Wreturn-type]
main()
^
returntest.c: In function ‘main’:
returntest.c:6:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
$ ./returntest ; echo $?
99
=====================================================================
GNU C 4.8.2 on Ubuntu 14.04 x86_64 using -std=c99:
$ gcc -std=c99 -Wall -o returntest returntest.c
returntest.c:1:1: warning: return type defaults to ‘int’ [enabled by default]
main()
^
$ ./returntest ; echo $?
0
Regards
Andrew
More information about the freebsd-questions
mailing list