int main Vs void main Vs int main (void) Vs main

Today, while reading source of some application on web, I come across the usage of two – three types of declaration for starting the programming code. Some use void main and some use int main and in some code’s, I have also seen int main(void). The question which come in my are:

Which one is vaild?

The answer will surprise every programmer but the true statement is all are valid but in different programming environments. I will just explain it in details here that why all are valid. But first why this question pop up. From quite months I am writing articles about C on my web blogs. I learn C language in 1997 before C99 standardization. Then moved to other languages to learn gone through C++, MySQL, VB, PHP,  JavaScript, JQuery, HTML but due to some technical problem, I have to upgrade my C knowledge and thus while learning, started writing Language C concepts and my views on my Blog. After learning many chapter, I started to see some of  source codes and manuals of Open Source C Codes, Windows C and Borland C and come to know the different types of main functions and after that search web about my questions. I have read many Articles, Books and Recommendations provided by Authors and also taken the opinion about which one is valid and every one has different views from other. Now, let go through One by One main function types and see why they are valid.


For understanding why main() is valid we have to see the History of C. The initial development of C occurred at AT&T Bell Labs between 1969 and 1973; according to Dennis Ritchie, the most creative period occurred in 1972. The origin of C is closely tied to the development of the Unix operating system, originally implemented in assembly language on a PDP-7 by Dennis Ritchie and Ken Thompson, incorporating several ideas from colleagues. Eventually they decided to port the operating system to a PDP-11. By 1973, with the addition of struct types, the C language had become powerful enough that most of the Unix kernel was rewritten in C. This was one of the first operating system kernels implemented in a language other than assembly. (Earlier instances include the Multics system (written in PL/I), and MCP (Master Control Program) for the Burroughs B5000 written in ALGOL in 1961.).
In 1978, Brian Kernighan and Dennis Ritchie published the first edition of The C Programming Language.] This book, known to C programmers as “K&R”, served for many years as an informal specification of the language. The version of C that it describes is commonly referred to as K&R C.
In 1983, the American National Standards Institute (ANSI) formed a committee, X3J11, to establish a standard specification of C. In 1989, the standard was ratified as ANSI X3.159-1989 “Programming Language C”. This version of the language is often referred to as ANSI C, Standard C, or sometimes C89.
In 1990, the ANSI C standard (with formatting changes) was adopted by the International Organization for Standardization (ISO) as ISO/IEC 9899:1990, which is sometimes called C90. Therefore, the terms “C89″ and “C90″ refer to the same programming language.
Even after the publication of the C90 and C99 standard, for many years K&R C was still considered the “lowest common denominator” to which C programmers restricted themselves when maximum portability was desired, since many older compilers were still in use, and because carefully written K&R C code can be legal Standard C as well.
Book contains many code’s, which also become the source of medium for many to learn. In book all the examples use main() as the starting point of the program. Standard Committee has decided that there compiler’s must support the basis described by the book but also started to deprecate the main() as starting point and started using int main() as the starting point. But due to support of all old code’s, the main() remains the valid. Since, then many compiler’s are created but they only issue a warning for using main(). The code will be compiled as without int main or void main. Thus, development of C and Book of K&R, are the base of main() and it is valid till present year November, 2011.
After reaching the point that main() is valid, I tired to compile the hello world program on Turbo C, Turbo C++ and GCC, all the produced the same result but GCC issued a warning of Standard C++ but compile’s the source code. After that I tried to run the code many code’s and to my surprise all the code’s are valid as only warning is issued by GCC only.

int main()

int main() is defined by C Standard Committee, reason given by many programmers and others. The reason is connected with the popularity of Language C. After the Development of C, the interest in programming Language’s has grown very high. Many people want to learn C rapidly and wanted to start writing code’s. The problem which pop’s out is that C Language is very near to the Assembly Language and thus people write different types of code’s and the error can only be shown when the output is displayed and another is it is impossible for a tutor who is teaching 100 students makes it difficult to check 100 students code instantly.
Now, the solution is needed to over come this situation and thus it is implied that the main () program should start with int main() and on termination would set the integer value to see the successful termination of the program. Thus, return 0; is introduced. Which means that the execution is successful and complete and thus terminate the program. Now, process is little improved with the operating system can store the returned value so that calling processes can query to find out whether an executable was successful or not. Applications usually return 0 for success and a range of non-zero error codes if unsuccessful.
So, we can say that the program can return a value to the operating system to indicate whether its execution was successful or not. Numerous shells, like bash, take advantage of the value returned by programs in examples like the following:
prog1 && prog2
The above statement will execute prog1, then it will only continue to execute prog2 if prog1 completed successfully (by returning a value of 0, or some other number which is interpreted as a successful exit).
int main() has also increased the learning process of C as the learner can now look self whether the program written is the successful or not. I am of the view that the introduction of int main() is also one of the major factor for popularity of Language C and one and learn self with little guidance and in modern time’s without teacher also.

int main(void)

The standard committee has also introduced that int main(int argc, char *agrv[]), which means that series of functions pass kind of additional information to the invoked program. However, main is still defined to take two parameters for these functions; the environment information is assigned to an external object extern char **environ. If someone doesn’t need or want to ignore the additional passing of arguments to the program then one can use int main(void). As this will ignore the additional parameters if provided.

void main()

Void means 0 in C. Thus, Some programmers interpret that void main means that return type value is always 0 but they are wrong. void main() only using in Microsoft Windows base compiler’s provided by Microsoft such as Visual C++ or Visual C#. There is a very good article available why void main() means the Wrong thing.

Which one to use?

Use int main(int argc, char *agrv[]), if you want to implement that required additional parameters required and else int main(void)
About these ads
Tagged , , ,

Get every new post delivered to your Inbox.

Join 517 other followers

%d bloggers like this: