What is the purpose of the return of the main function and the importance of this function?

Asked

Viewed 4,376 times

9

I would like to know the importance of the function main and what is the purpose of her return which is a whole?

See a minimum example of function implementation main:

int main()
{
    printf("StackOverFlow\n");
    return (0);
}

Where this return implies the execution of my program?

  • 1

    I have heard that this is a return to the operating system. "0" means that the program has run successfully ...

3 answers

13


Usually languages need a starting point. In java, c, c++, ada, pascal, assembly, etc, a method or a block (in the case of pascal) is used to know where the application will start.

In Assembly the code starts in the method _start (if it is Linux), the C, decides that the program should start on main and the pascal has a cloc of begin and end. to tell where the application will start.

The return of the method serves to know if everything happened well during the execution of your progamma.

Example of use of return:

$ gcc main.c -o compiled
$ echo $?

If the file is compiled successfully it will return 0, the other values are for different types of error.

Another method of use is:

$ gcc -fPIC -shared -O2 mylib.c -o libmy.so &&
$ gcc main.c -o compiled ./libmy.so &&
$ echo "compilado com sucesso!" ||
$ echo "Erro ao compilar!"

The symbol && refers to a condition e, if the 2 files have been compiled successfully, the first will be called echo, because it is part of the condition e, otherwise, will be called the second echo, for the condition or is only called if the e be false.

6

In the language C the function return value main is passed to the operating system, and can be tested by the parent process.

A concrete example: in Windows, when you work in a "DOS window", this function return value main is the value that receives the ERRORLEVEL environment variable.

c:\tmp> prog_teste.exe
if %ERRORLEVEL%==0 (
    echo prog_teste executou sem erro
) else (
    echo houve erro na execução de prog_teste
)

In Linux (and UNIX) the return value is left in the $?shell variable, which can also be tested right after the program’s execution

$ ./prog_teste
if [ $? = 0 ]; then
    echo prog_teste executou sem erro
else
    echo houve error na execução de prog_teste
fi

In C++ you are allowed to omit the command return of function main, and in this case the value returned is 0.

In C, from version C99 this omission is also allowed, and the behavior is equal to that of C++.

In the Linux shell (and UNIX) it is possible to test the "success" of a program without making the explicit comparison test with 0, just test the name of the program.

$ ./prog_teste
if ./prog_teste; then
    echo prog_teste executou sem erro
else
    echo houve error na execução de prog_teste
fi

-2

The experienced ones correct me, but I believe that it is the main class of the program, where the program will begin to run.

Already the Return, as we have int main(), we have the indication that this class will return an integer type, so Return(0). We could simply create void main(), indicating that there will be no return, ie empty (void).

I hope it helped you.

  • 1

    There are no classes in C, and even if it were java, it would be wrong to say that "main is a class". Classes only store data and methods, there is no way to place a sequence of instructions in a class, only in methods.

  • Good! I forgot that I was dealing with C... rsrsrs could say then that it is a type of indicator? indicate that block x is part of the main program and should start running from there, Wrong?

  • I don’t think indicator is the correct term, I think saying method only, is more direct.

  • Got it! Thanks for your help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.