CrazyEngineers
  • 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 
    
    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, 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 is unrelated to int main(), and this header is not recommended for modern, portable code.

    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Deepika Bansal

    MemberJul 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.
    Are you sure? This action cannot be undone.
    Cancel
  • Rahul Naraniya

    MemberJul 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??
    Are you sure? This action cannot be undone.
    Cancel
  • beingab

    MemberJul 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
    Are you sure? This action cannot be undone.
    Cancel
  • Rahul Naraniya

    MemberJul 19, 2014

    ohk... got it thanks... 😀
    Are you sure? This action cannot be undone.
    Cancel
  • ravirajput

    MemberJan 25, 2015

    goood yr...
    Are you sure? This action cannot be undone.
    Cancel
  • SK Jindal

    MemberJan 25, 2015

    Rahul Naraniya
    can 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
    Are you sure? This action cannot be undone.
    Cancel
  • kothandaraman

    MemberJul 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)
    Are you sure? This action cannot be undone.
    Cancel
  • kothandaraman

    MemberJul 6, 2015

    above the program any doubt message me
    Are you sure? This action cannot be undone.
    Cancel
  • rahul69

    MemberJul 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 () )
    😀
    Are you sure? This action cannot be undone.
    Cancel
  • Gowtham369369

    MemberJul 9, 2017

    i have replaced void main() with int main() there is same result then what is the difference......?
    Are you sure? This action cannot be undone.
    Cancel
  • Walter Boyd

    MemberJul 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.
    Are you sure? This action cannot be undone.
    Cancel
  • Gowtham369369

    MemberJul 9, 2017

    Okay! then I will use only int main() so that there will be no confusion for me....
    Are you sure? This action cannot be undone.
    Cancel
  • Base Developer

    MemberAug 17, 2018

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


    Are you sure? This action cannot be undone.
    Cancel
  • GEETHA NANI

    MemberJan 14, 2019

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

    Are you sure? This action cannot be undone.
    Cancel
  • GEETHA NANI

    MemberJan 14, 2019

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

    Are you sure? This action cannot be undone.
    Cancel
  • Ankit Anand

    MemberJan 16, 2019

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

    Are you sure? This action cannot be undone.
    Cancel
  • AdhikariHarika Reshma

    MemberJan 17, 2019

    #-Link-Snipped-#

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

    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register