简体   繁体   中英

does return 0 exit a program or exit a loop?

I have a question about return 0 in C:

void main()
{
    int i = 0;
    while(1)
    {
        switch(i)
        {
            case 1:
            case 2:
            case 3:
            case 4:
                i ++;
                break;
            case 5:
                return 0;
        }   
    }
}

will the return 0 exit the whole program or it will just exit from the while loop?

它将从main方法返回,在这种情况下,它将退出整个程序。

In your example, it will exit the main() function which will end the program. In general, a return statement will terminate a loop (that's normally break to just end the loop) by returning flow of control to a method caller; in the case of main() that is the operating system.

In your case,since return 0 is placed in main ,the program will exit. return will terminate the execution of the function and returns control to the calling function. When it is placed in main , it will exit the program. In order for main to return an int , use int main instead of void main . Also in C99 and above,you can't use void main .

From the C-Standard 6.8.6.4/2

A return statement terminates execution of the current function and returns control to its caller.

As in the OP's code the " current function " is main() " returning control to it's caller " implies the end of the program.

A return statement causes execution to leave the current subroutine and resume at the point in the code immediately after where the subroutine was called, known as its return address.

In your program main() is the subroutine here and as main() is the program starts execution in C, so the exit from your main() will also be the exit from the program.

int getvalue()
{
    return 10;           //exit subroutine getvalue()
}

int main()
{
    printf("Value : %d\n",getvalue());   // get the return value from getvalue() and continue next
    printf("Next step after return\n");   
    return 0;                         // exit the main() subroutine
}

Output:

value : 10    
Next step after return

Here the program will print the return value 10 from getvalue() and also print "Next step after return" and then the return 0 will cause the exit of the main() subroutine ie program.

There are three basic "flow control" statements in C:

  • return always leaves the currently executed function (optionally with a return value)
  • break leaves the currently executed loop
  • continue skips all remaining statements in the current loop and continues with the next iteration

In your case, return 0 will terminate the current function main() thus exiting the whole program (the shell will see the result code 0).

the return keyword executes termination and current function and return the control flow to where it is called. for ex:- if we write return in user defined function it will not end the program but return the control to main function and terminates the user defined function but in case if you have used return in main function it will terminate the main function which eventually end the program and return the control to operating system(caller).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM