[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: gcc not compiling



On Mon, Oct 31, 2005 at 11:05:41AM -0800, Brian D. McGrew wrote:
> Never protoctype main as a void function or else you can't get a return
> value back to the operating system.

Sure you can:

	#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
	
	Hello World!
	[ddm archonis ~]
	$ echo $?
	14
	
As written, the program returns the value of the last expression
executed.  In this case, printf() printed 14 characters, and the
program returns that number.

However, it should be noted that this behavior is not defined by the
standard, and can't be depended upon.  But if you don't want that, no
problem:

	#include <stdio.h>
	void main(void)
	{
		Printf("\nHello World!\n");
		exit(0);
	}
	
	gcc -o foo foo.c
	foo.c: In function `main':
	foo.c:2: warning: return type of 'main' is not `int'
	[ddm archonis ~]
	$ ./foo
		
	Hello World!
	[ddm archonis ~]
	$ echo $?
	0
	
You can return whatever value you like by calling exit().

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

Attachment: pgprCxOAycjco.pgp
Description: PGP signature


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]