gcc not compiling

Derek Martin code at pizzashack.org
Mon Oct 31 19:53:30 UTC 2005


On Mon, Oct 31, 2005 at 01:30:44PM -0600, STYMA, ROBERT E (ROBERT) wrote:
> >	#include <stdio.h>
> >	void main () {
> >		printf("\nHello World!\n");
> >	}                                                                                                      
> >	$ gcc -o foo foo.c
> >	foo.c: In function `main':
> >	foo.c:2: warning: return type of 'main' is not `int'
> >	$ ./foo
> 
> In the example, the compiler warning MAY have indicated that
> it was overriding the void with in.  

Perhaps, but I think the better and truer explanation is that it is
simply impossible for a program not to return some sort of return
value to the operating system... even if it isn't an intended one.
Generally the OS will see whatever value is in the appropriate
register at the end of execution and use that as the exit status,
whether or not that's what the coder intended.  The behavior isn't
defined by the standard, as we've said; but practically speaking, I
don't imagine it's very common to see any different behavior...  ;-)

> Since the original poster was new coder, best that they learn the
> approved syntax.

I don't disagree, but given that Dotan wrote the most basic form of
Hello World, I'm assuming he hasn't yet been introduced to the ideas
of function prototypes, or the C standards.  I've no doubt that he'll
learn those topics later though.  For the time being, it is probably
best that he follows the convention of his learning materials.

The trouble is, Brian offered a code correction involving more
complicated ideas than Dotan will have seen just yet, and then offered
an explanation for the correction which was patently false.  I think
if you're going to take the time to correct someone who is trying to
learn, you should yourself be sure to be correct.

In practice, for simple programs such as "Hello World", the only
practical reason to adhere to the standard with regard to main()'s
return value is to avoid compiler warnings.  There's really nothing
wrong with it, other than that it violates the standard.  But that is
itself not a crime; there are times when it is quite useful to violate
the standard, and even necessary, (ironically) to ensure maximum
portability.  :)

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/fedora-list/attachments/20051031/6ac0777b/attachment-0001.sig>


More information about the fedora-list mailing list