Logo

The Eduladder is a community of students, teachers, and programmers just interested to make you pass any exams.
Here we host videos for you.

Why use #include, int main,return 0 in C


Video description

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.)

This is why you commonly see a return 0; statement at the end of a main function. It is sending the integer value 0 back to the OS, indicating that the program was successful.

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 of upload

DateDec 21, 2021 12:00:00 AM

Total views

Visits183

Uploaded By

Muskan Kumari

Related questions

You may like
Why use #include, int main,return 0 in C