简体   繁体   中英

How to exit a console app in C using switch case?

For example, if I made a simple switch-case how would I make the console app close depending on the input? I don't want to use to use break; and skip the loop, I want to close the console altogether.

char choice; 

printf("Run random function \n");
printf("Exit \n");

choice = getchar();
fflush(stdin);

switch(choice)
{

            case '1':
                 //randomFunction();

            case '2':
                 //I want this case to exit the console the console
}

Simply use exit (EXIT_SUCCESS);

Ref - exit

You could call exit() passing the return value as a parameter, this return value can be checked by the calling process / batch file:

exit(EXIT_SUCCESS); // = 0 = (standard) success

or

exit(EXIT_FAILURE); // = 1 = (standard) failure

or

exit(123); // return a specific value

Documentation for MS-Visual Stuidio

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