Everything2
Near Matches
Ignore Exact
Full Text
Everything2

void main()

created by rookkey2

(thing) by rookkey2 (2.8 y) (print)   ?   (I like it!) Sun Nov 19 2000 at 4:39:41

Considered to be the wrong way to begin C and C++ programs. This is a violation according to ANSI/ISO standards. Programs do not just end suddenly; they must return a value.

Instead use:

int main()

And at the end of the main function, add:

return 0;

You will be forgiven.


(thing) by middlemarch (19.7 hr) (print)   ?   (I like it!) Wed Apr 28 2004 at 21:15:38

return 0; implies your program exited successfully. If you want to indicate that your program failed to the program that invoked it, you need to return something else. But what?

Different OSes answered this in different ways. In VMS, it's actually 0 that means failure and 1 that means success. In Unix and most other systems, 0 means success and any non-0 value means failure. What can a portability-conscious programmer do?

Use the standard header <stdlib.h>, and get in the habit of saying exit(EXIT_SUCCESS); and exit(EXIT_FAILURE); instead of 'return'ing. This is guaranteed to work no matter where your program is compiled next.

exit(foo); will work from any subroutine with the same semantics as if it were called from main(). This is useful if you want to have error-handling subroutines end the program if something's gone wrong, and only allow main() to kill itself if everything's peachy. Which is common practice anyway.

For more advanced users, the subroutine atexit(); (also part of C99 and also prototyped in stdlib.h) will register subroutines to be called automatically when your program calls exit();. This makes invoking free(); and other cleanup subroutines fairly brainless, assuming they should be invoked whenever the program ends. Calling free(); on memory you haven't allocated is a bad idea.

Of course, using exit(); alone doesn't allow you to write a recursive main(). If you think you need a main() that calls itself, you should probably rethink the design of your program. If you really need a self-calling main(), you'll need to 'return' from main() until you decide to actually end your program, at which point exit(); is still a good choice.


printable version
chaos

int main(int argc, char *argv[]) main() C: The Real Basics C++: how big is nothing?
The Jesus Incident The Teach Yourself to be a Dummy in 24 Hours Bible The C++ Programming Language Freakshow Detroit Style
void * Gas, grass or ass, nobody rides for free ISO bastard sword
ANSI argv polyandry Windows source code
stdlib.h sexual delinquency recursive string.h
Ocean State Free Net Vatican offers free world-wide advertisement foo Subroutine
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
Drink up!
How can a good Buddhist work in advertising?
SPEWS
George W. Bush's address to the UN General Assembly: September 12, 2002
the knife edge of snowcrash
Doctor Faustus
Euclid's algorithm
Does porn increase the self-perceived value of pussy?
Thomas Shadwell
Darryl Strawberry
Ashurbanipal
integer information sample
Please Don't Bury Me
The Idiot's Guide to the Transmutation of Morals
New Writeups
XWiz
Trism(review)
artman2003
Briefcase Full of Souls - Part I(fiction)
Dreamvirus
Alan Ladd(person)
waverider37
Harold Holt(person)
The Debutante
Until death do us part(fiction)
Ysardo
a brother to a sister(personal)
antigravpussy
your warm whispers(personal)
Clarke
Multiculturalism(idea)
aneurin
Earl of Landaff(person)
Heitah
Pseudocide(idea)
XWiz
Google Knol(lede)
Mythi
July 24, 2008(personal)
locke baron
The fall of Earth(fiction)
BookReader
Fear the Cold(dream)
Pavlovna
Kathleen MacInnes(person)
This affordable entertainment brought to you by The Everything Development Company