Why the return type of main() is either void or int? why not float?

This is because main "returns" to the operating system.  The value returned is often a pass/fail response (0 or 1) or it can return an error number. From long ago before C and C++, error codes were integers and so OSes expected error codes as integers and all languages since then have their entry point from the OS expect a return of an integer.  If main returns void this is not correct for C (and maybe C++) - the ISO C standard states main should return int.  It can return anything you want but doing so is not expected and likely to produce undesirable results.



All Answers

Answers by Expert:

Ask Experts


Bill A


I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.


I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

BS Computer Engineering

©2016 About.com. All rights reserved.