int, as it appears before the function name main, specifies the data type of the return value of the function. The main function should always return an int.
In environments that have an operating system (OS), the OS starts your program running. The integer returned from main provides a way for your program to communicate a value to the OS indicating whether the program succeeded, failed, or generated some numeric result. The OS (or a batch file or script that was used in the OS to run your program) can look at this integer value and make some decisions about it. While the integer returned can be any value, there is a set of conventions that most (but not all) programs use:
A zero value is returned to indicate that no error occurred. (The cstdlib C++ header and the stdlib.h C header file both define an EXIT_SUCCESS symbol for this purpose. While its value is implementation-defined, in practice it is typically 0.)
A non-zero value is returned to indicate an error occurred and/or to provide a specific result value.
Negative values are often used to indicate that an error occurred. For example, -1 might be used to indicate that an error occurred, or a specific negative value might indicate that a specific type of error occurred.
Positive values are sometimes used to indicate that an error occurred. For example, 1 might be used to indicate that an error occurred, or a specific positive value might indicate that a specific type of error occurred. (The cstdlib C++ header and the stdlib.h C header file both define an EXIT_FAILURE symbol for this purpose. While its value is implementation-defined, in practice it is typically 1.)
Positive values are sometimes used to provide a result. For example, a word counting program might return a positive value representing the number of words counted. A text file comparison program might return the number of differences encountered.
Again, the meaning of the value returned is just a convention. The C program and whatever batch file, script, etc. in the OS that is starting the program need to agree on what the meaning of the return value is.
It is up to your main function to choose what integer value should be returned to the OS. (In environments without an OS, what happens to the returned integer is implementation-specific, so check your documentation for details.) You should document for the users of your program what specific values might be returned by main and what they mean.
|Date||Dec 21, 2021 12:00:00 AM|