int main() or void main() 
Author Message
 int main() or void main()

hai,
just wondering about the mainline
of C program, i ve seen some programmers
write C code as "int main()" as well as
"void main()" and i am wondering why is that?
can anyone give me good answer to this?
also they often put "return 0" at the end
of "int main()" why?
please explain! can u give me the sample case
which one to use?
thank you

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Fri, 29 Nov 2002 03:00:00 GMT  
 int main() or void main()

(Purpen) wrote in comp.lang.c:

Quote:
> Unless the code was written in Embedded C which makes the return value
> redundant.

> Purpen :)

> Flame suit on...




> >comp.lang.c:

> >> hai,
> >> just wondering about the mainline
> >> of C program, i ve seen some programmers
> >> write C code as "int main()" as well as
> >> "void main()" and i am wondering why is that?
> >> can anyone give me good answer to this?
> >> also they often put "return 0" at the end
> >> of "int main()" why?
> >> please explain! can u give me the sample case
> >> which one to use?
> >> thank you

> >int main() is defined and required by the ANSI/ISO International
> >Standard for C.  Those who write void main() are either careless,
> >sloppy programmers who do not care about portability, or do not really
> >know the language but just know what works with a particular compiler
> >they are familiar with.

> >For more information see these two articles:

> >http://home.att.net/~jackklein/ctips01.html#int_main
> >http://home.att.net/~jackklein/ctips01.html#return

> >Jack Klein

I am most likely far more familiar with using C in what the standard
calls a "freestanding" environment than most, since I've been
programming embedded systems in C and other languages for twenty
years.

I specifically do not go into this level of detail because it is only
likely to add to the confusion of a newcomer to C who asks this
question.

BTW, the simple fact that the code runs in an embedded system does NOT
mean that main() never returns, nor that it never returns a value.

Jack Klein
--
Home: http://jackklein.home.att.net



Fri, 29 Nov 2002 03:00:00 GMT  
 int main() or void main()
Ok Point taken. I always try to obey the rule that int main() is
correct.

When I first started using Turbo C years ago, even the example code
had void main(void), what chance have beginners got with that sort of
stuff around.

I realise that the return value may still exist on embedded systems,
but the point was that it is not always needed, but with 20yrs
experience of this I don't need to tell you that.

Thanks for being so understanding of an argumentative sod... (pardon
my spelling ! I'm Welsh)

Purpen



Quote:
>I am most likely far more familiar with using C in what the standard
>calls a "freestanding" environment than most, since I've been
>programming embedded systems in C and other languages for twenty
>years.

>I specifically do not go into this level of detail because it is only
>likely to add to the confusion of a newcomer to C who asks this
>question.

>BTW, the simple fact that the code runs in an embedded system does NOT
>mean that main() never returns, nor that it never returns a value.

>Jack Klein
>--
>Home: http://jackklein.home.att.net



Fri, 29 Nov 2002 03:00:00 GMT  
 int main() or void main()

Quote:

>Unless the code was written in Embedded C which makes the return value
>redundant.

Not necessarily; the requirements related to the startup function in a
freestanding environment are entirely up to that environment. If
the requirements say that you have to return the value 42 from the
startup function long beeblebrox(double) then that's what you will do.

There are systems in which the main OS is started from within a monitor
program.  It's possible for that OS to shut down and return to the monitor,
giving it a termination status.

For example, if you shut down the operating system on a Sun Sparcstation it's
possible to return to that monitor with a ``program terminated successfully''
type message, the ``program'' being Solaris, BSD or Linux or what have you. ;)
Presumably, such a program can terminate unsuccessfully or abnormally as well
and there is a way for it to indicate that to the monitor.

--
#exclude <windows.h>



Fri, 29 Nov 2002 03:00:00 GMT  
 int main() or void main()
Ok Point taken. I always try to obey the rule that int main() is
correct.

When I first started using Turbo C years ago, even the example code
had void main(void), what chance have beginners got with that sort of
stuff around.

I realise that the return value may still exist on embedded systems,
but the point was that it is not always needed, but with 20yrs
experience of this I don't need to tell you that.

Thanks for being so understanding of an argumentative sod... (pardon
my spelling ! I'm Welsh)

Purpen



Quote:
>I am most likely far more familiar with using C in what the standard
>calls a "freestanding" environment than most, since I've been
>programming embedded systems in C and other languages for twenty
>years.

>I specifically do not go into this level of detail because it is only
>likely to add to the confusion of a newcomer to C who asks this
>question.

>BTW, the simple fact that the code runs in an embedded system does NOT
>mean that main() never returns, nor that it never returns a value.

>Jack Klein
>--
>Home: http://jackklein.home.att.net



Fri, 29 Nov 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. int main() OR void main()...

2. main() vs int main() vs int main(void)

3. what's the difference between void main(void) and int main(void)

4. newbie question....int main, void main, main

5. void main(void) or int main() ??

6. int main() vs int main(void)

7. int main vs. void main

8. void main() or int main()?

9. void main or int main?

10. void main() <-> int main()

11. void main() or int main()?

12. void main() v/s int main()

 

 
Powered by phpBB® Forum Software