1. Participate in 'My Internship Experience' Contest to Win Cash Prizes + Certificates.

    Click for Contest Rules. Click to Submit Entry. [Last 2 Days To Go!]

Difference between int main and void main in C programming language?

Discussion in 'Computer Science | IT | Networking' started by Rahul Naraniya, Jul 19, 2014.

  1. Rahul Naraniya

    Rahul Naraniya Apprentice

    Engineering Discipline:
    IT
    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 #include<conio.h> when we use int main???
     
  2. Deepika Bansal

    Engineering Discipline:
    Computer Science
    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.
     
    • Like Like x 2
  3. Rahul Naraniya

    Rahul Naraniya Apprentice

    Engineering Discipline:
    IT
    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??
     
  4. beingab

    beingab Rookie

    Engineering Discipline:
    Computer Science
    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
     
  5. Rahul Naraniya

    Rahul Naraniya Apprentice

    Engineering Discipline:
    IT
    ohk... got it thanks... :)
     
  6. ravirajput

    ravirajput Newbie

    Engineering Discipline:
    Computer Science
    goood yr...
     
  7. SK Jindal

    SK Jindal Newbie

    Engineering Discipline:
    Electronics & Communications
    void implies no return type and int main means that main() will return an integer
     
  8. kothandaraman

    kothandaraman Newbie

    Engineering Discipline:
    Computer Science
    #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)
     
  9. kothandaraman

    kothandaraman Newbie

    Engineering Discipline:
    Computer Science
    above the program any doubt message me
     
  10. rahul69

    rahul69 Star

    Engineering Discipline:
    IT
    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 () )
    :)
     
    • Informative Informative x 1
Draft saved Draft deleted

Share This Page