How are int main() and void main() different?

Rahul Naraniya
@rahul-naraniya-8Qz704 โ€ข Oct 26, 2024

Many new C/C++ programmers get confused between the proper use of int main() and void main() functions. In this article we will understand these functions and look at sample code.

  1. int main(): This is the most standard way to write the main function in both C and C++. When a program finishes execution, it is customary for it to return a status code to the operating system. int main() signifies that the main function should return an integer value to the operating system. This value is usually 0, which means the program has successfully executed, but can be another value to signify an error. The exact meaning of non-zero return codes can depend on the program.

  2. void main(): This function doesn't return a value. It is considered non-standard in both C and C++, even though some compilers may allow it.

The C++ standard states that the main function should return an int, so int main() is the correct and standard way to define the main function in C++.

If you do not explicitly return a value from int main(), C++ automatically adds return 0; at the end for you.

In C, while void main() may be allowed by some compilers, it is not portable or standard-compliant.

The C standard also says that the main function should return an int. In practice, this means that while void main() might work on your compiler, it might not work on another.

int main() code example

Take a look at following C++ program-

#include <stdio.h>

int main() {
    printf("Hello, world!\n");
    return 0;
}

In this case, return 0; signifies that the program has finished execution successfully. If we don't include return 0;, the C++ compiler will implicitly add it, but in C, it's better to add it explicitly.

Including conio.h in code

Regarding #include<conio.h>, this is a header file used in old MS-DOS compilers to provide console input/output.

It's not standard, and it's not necessary or recommended for modern programming in C or C++. It's not related to the int main() function and shouldn't typically be used in portable code. Instead, for console input and output in a portable way, you should use stdio.h in C or iostream in C++.

Common Mistakes New C/C++ Programmers Make

A common mistake when beginners start learning C or C++ is to forget to return a value from int main().

In C++, this isn't a problem because the compiler will add return 0; implicitly.

However, in C, not returning a value when one is expected results in undefined behavior, which could lead to any result.

Another mistake is to use void main() because they've seen it in some non-standard code or outdated textbooks.

It's important to understand that void main() is not standard-compliant, and using it can lead to portability problems.

Summary

To summarize, you should always use int main() in C and C++, and you should return a value from main in C. Avoid using void main() because it's not standard.

The use of #include<conio.h> is unrelated to int main(), and this header is not recommended for modern, portable code.

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Deepika Bansal

    @deepika-jf1ysv • Jul 19, 2014

    Difference betwรจen int main and void main: See like any other function, main is also a function but with a special characteristic that the program execution always start from main. So the function main needs arguments and a return type. These int and void are its return type. Void means it will not return any value, which is also ok.
    But if want to know whether the program has terminated successfully or not, we need a return value which can be zero or a non zero value. Hence the functuon becomes int main () and is recommended over vpid main ().
    Hope its helps you.

  • Rahul Naraniya

    @rahul-naraniya-8Qz704 • Jul 19, 2014

    The same thing we can do by using getch() (for void main). then why int main is suggested?? is there any advantage of int main over void main??

  • beingab

    @beingab-l4GkXZ • Jul 19, 2014

    the standard statements are:
    int main(void)
    int main(int argc, char **argv)

    You can also use main() simply that means same as int main()..
    int main() returns an exit value to compiler and works on most compilers.
    And getch() as you mentioned has nothing to do with it. It gets character input on screen or else holds screen in other words

  • Rahul Naraniya

    @rahul-naraniya-8Qz704 • Jul 19, 2014

    ohk... got it thanks... ๐Ÿ˜€

  • ravirajput

    @ravirajput-oi56fg • Jan 25, 2015

    goood yr...

  • SK Jindal

    @sk-jindal-LfLs4A • Jan 25, 2015

    Rahul Naraniyacan anyone explain what is difference between int main and void main??? why it is suggested to use int main instead of void main?? and why we need [HASHTAG]#include[/HASHTAG]<conio.h> when we use int main???

    void implies no return type and int main means that main() will return an integer

  • kothandaraman

    @kothandaraman-v2rn8a • Jul 6, 2015

    #include<stdio.h>
    void main()
    {
    clrscr();
    add(5,4);
    getch();
    }
    int add(int a,int b)
    {
    int c;
    c=a+b;
    printf("Output: %d",c);
    return 0;
    }

    now see the guys when you use the function having some parameters than time return some a and b values so now use int main function
    if use void add(int a,int b) system throw the error. so i use int add(int a,int b)

  • kothandaraman

    @kothandaraman-v2rn8a • Jul 6, 2015

    above the program any doubt message me

  • rahul69

    @rahul69-97fAOs • Jul 6, 2015

    1. int main() is preferred over void main(), as per standards, and most new compilers supports int main() rather than void main().
    2. int main() can help to return the error code if your program fails due to some error.
    (Pre exception handling days, now you may use exception handling to handle issues,
    perhaps that's the reason in java we go as public static void main () )
    ๐Ÿ˜€

  • Gowtham369369

    @gowtham369369-1ddlff • Jul 9, 2017

    i have replaced void main() with int main() there is same result then what is the difference......?

  • Walter Boyd

    @walter-boyd-JqoS5G • Jul 9, 2017

    The difference is, in C, int main() can be called with any number of arguments, but int main(void)can only be called without any argument. Although it doesnโ€™t make any difference most of the times, using โ€œint main(void)โ€ is a recommended practice in C.

  • Gowtham369369

    @gowtham369369-1ddlff • Jul 9, 2017

    Okay! then I will use only int main() so that there will be no confusion for me....

  • Base Developer

    @base-7yTzaN • Aug 17, 2018

    How can I came to know that program is returning a value after using int main function


  • GEETHA NANI

    @geetha-82RUGM • Jan 14, 2019

    can anyone suggest me a video of difference between this void main and int main

  • GEETHA NANI

    @geetha-82RUGM • Jan 14, 2019

    can anyone suggest me a video of difference between this void main and int main

  • Ankit Anand

    @ankit-R3gnfJ • Jan 16, 2019

    The above program has compilation error because we must define the add method before main methodย 

  • AdhikariHarika Reshma

    @adhikariharika-fKRdEB • Jan 17, 2019

    #-Link-Snipped-#

    #-Link-Snipped-#ร‚ย hope the above link for the difference between INT main and void main help you.