Thread: Main() function with return type int returns value hence prefered but where value go?

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    3

    Unhappy Main() function with return type int returns value hence prefered but where value go?

    i have always read to prefer int main instead void as it returns value but what is difference between
    this
    insert
    Code:
    void main()
    {
    };
    //and this
    int main()
    {
    return 0
    }

    as output is blank for both
    where that 0 go?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    what is difference between this
    The difference is that the C++ standard requires main to be defined to return an int.
    where that 0 go?
    The return value, zero in this case, is returned to the operating system. The return value can be used by other processes to indicate success or failure of your program.

    Jim

  3. #3
    Registered User
    Join Date
    Jun 2016
    Posts
    3
    tHAnx but can you also tell why preference given to int over void? why success or failure matters to OS if it is running? I am an beginner in C++, so confused

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There IS no preference. The standard says main shall return int. Period.

    The return value is a way of signalling to other programs a status code indicating the status of the operation your program performed. The OS itself may also try to check the error code to see if there was an error to try to enhance the user experience. The return value is just a service to give the OS an insight into your program. Consider for example a library which starts a program to check out books. The return value of your program may indicate if the checkout was a success or not (and why not).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    The returned value is an exit status. How it's used depends on the operating system:

    Exit status - Wikipedia, the free encyclopedia

  6. #6
    Registered User
    Join Date
    Jun 2016
    Posts
    3
    tHnks, i have understood

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 12-14-2015, 02:43 PM
  2. Replies: 6
    Last Post: 10-24-2013, 08:27 AM
  3. return type of 'main' is not 'int'
    By Mini in forum C Programming
    Replies: 4
    Last Post: 08-09-2010, 01:30 AM
  4. return type of 'main' is not `int'
    By vsovereign in forum C Programming
    Replies: 11
    Last Post: 03-02-2010, 06:23 AM
  5. Replies: 6
    Last Post: 04-09-2006, 04:32 PM

Tags for this Thread