Thread: the main

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    the main

    I've seen this coding convention in certain books and i was wondering. Even if it does work correctly isn't kinda wrong
    Code:
    int main(void)
    .......
    .......
    .....
    return 0;
    if main returns an integer value to the processor to let it know it has compiled succesful, why then do you need void just to say that it does not accept any arguments? doesn't the compiler/processor knows this by default.


    while i'm at it:

    if "0" is supposed to say it is compiled succesfully and any other integer can be any type of error. why use that convention seeing as how in C if !0 is seen as true. basically as long as its not zero it is true/fine. seems counter intutive to me and confusing. Its used to represent one thing and then it turns around and is used to represent another.


    I know I'm a newbie so take it easy on me with your responses.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The 0 does not tell the compiler or processor that it compiled correctly, it tells the OS that it executed and exited normally. There are times when you need to tell the OS that it did not execute and exit normally.

    The reason you put void in the () on function calls that don't have any parameters is so that the compiler knows that any call with parameters is wrong.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    The argument still stands if some value not equal to 0 be returned should, like the bool value, represent true and logically a succesful exit.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    But its not a simple boolean yes or no. The value returned actually means something to the OS. What each value means is unknown to me, however if you wanted to say single to the OS to reboot and you knew that if you return -345 it would do so, then you could return -345 and the OS would reboot.

    0 and 1 just happen to be the most command and standard returns.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    I realized this but I'm glad you posted. By allowing an int return value you can have many exit conditions with a variety of meanings.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Rofl, I love those questions. Usally its me doing it to other people and not this way around

    Edit: To clarify its usally the OS, but any program that invokes another can (and should) use the exit conditions.

  7. #7
    Registered User
    Join Date
    Dec 2003
    Posts
    14
    By allowing an int return value you can have many exit conditions with a variety of meanings.
    There are. I believe they are OS specific. In MS DOS a return of 0 means "Program exited successfully", 1, 2, and 3 (and probably many more) are different error codes which I am unclear of the meaning. Look it up specifically to whichever OS your programs will be running on. Although without some sort of log these return values are useless, as they don't make the OS behave in a different manner.



    Function Prototype:

    return data type function name(argument data types and variables passed to function);

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    For an example see this list of what MsiExec.exe returns.

    http://msdn.microsoft.com/library/en...rror_codes.asp

    Sample batch file for doing a silent install on a remote machine and saving the result to a log file:
    Code:
    \\PathtoUpdate\update.exe /q:a /r:n
    
    ECHO Computer: %COMPUTERNAME% >> %LOGFILE%
    ECHO Update Result: %ERRORLEVEL% >> %LOGFILE%
    %ERRORLEVEL% holds the result of the last program to run.

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    44
    In C, you can return any int value you like from main. However, the only ones which have defined behaviour on all operating systems are:

    0,
    EXIT_SUCCESS, and
    EXIT_FAILURE.

    (Those two macros ae defined in stdlib.h)

    0 and EXIT_SUCCESS mean, well, success. EXIT_FAILURE means failure.

    The C implimentation is obliged to translate these values into whatever values the underlying OS uses for success and failure.

    Returning any other value from main is implimentation defined behaviour... it's outside of the scope of C to say what it's meaning is.

    Ian Woods

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    If you run your program from a batch file, by returning a different value you can set up your batch file to conditionally execute alternate batch code:
    Code:
    @echo off
        programname %1 %2
    if errorlevel 4 goto IllegalTimeStamp
    if errorlevel 3 goto DataIsInError
    if errorlevel 2 goto NoFileFound
    if errorlevel 1 goto BadPath
    if errorlevel 0 goto GoodExit
    echo An unknown error occurred --  %errorlevel%
    goto ExitBatch
    :IllegalTimeStamp
    There is a small program to enhance batch files in older Windows O/S called choice.com with allows batch files to ask for input and branch accordingly. A 32-bit clone has been written to add the capability to newer 32-bit Windows. Check it out at http://wildeware.com/util/util.htm. It's freeware and does not touch the registry at all.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why To Use int main()
    By year2038bug in forum C Programming
    Replies: 2
    Last Post: 08-19-2005, 01:28 PM
  2. passing params to main()
    By mike11 in forum C++ Programming
    Replies: 14
    Last Post: 06-21-2005, 12:36 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Int Main or Void Main?
    By FromHolland in forum C++ Programming
    Replies: 9
    Last Post: 06-12-2003, 04:29 PM
  5. int main (), main (), main (void), int main (void) HELP!!!
    By SmokingMonkey in forum C++ Programming
    Replies: 7
    Last Post: 05-31-2003, 09:46 PM