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

8

I would like to know what is the importance of the function main and what is the purpose of the return of it that is an integer?

Here is a minimal example of the implementation of the main function:

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

What does this return entail in running my program?

    
asked by anonymous 21.04.2016 / 03:40

3 answers

11

Typically 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. p>

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

The return of the method is to know if everything went well during the execution of your program.

Return usage example:

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

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

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 e condition, if the 2 files were successfully compiled, the first echo will be called because it is part of the e condition, otherwise it will be called the second echo , because the condition or is only called if the e is false.

    
21.04.2016 / 03:55
4

In language C, the return value of the main function 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 return value of the main function 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
)

On Linux (and UNIX) the return value is left in the $? shell variable, which can also be tested immediately after the program 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 ++ it is allowed to omit the command return of the function main , in which case the value returned is 0.

In C, from the C99 version this omission is also allowed, and the behavior is the same as the C ++ language.

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

$ ./prog_teste
if ./prog_teste; then
    echo prog_teste executou sem erro
else
    echo houve error na execução de prog_teste
fi
    
20.09.2016 / 18:21
-2

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

Now 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 I've helped you.

    
21.04.2016 / 03:55