1

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
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Ali Appleby
  • 339
  • 1
  • 4
  • 6
  • 1
    **exit(0);** would be your choice. – Gangadhar Aug 31 '13 at 16:09
  • `fflush(stdin);` is implementation dependent – Grijesh Chauhan Aug 31 '13 at 16:20
  • @GrijeshChauhan Much worse: it is undefined behavior! For example see [here](http://stackoverflow.com/questions/2979209/using-fflushstdin) – LorenzoDonati4Ukraine-OnStrike Aug 31 '13 at 16:43
  • @LorenzoDonati Its undefined on Unix based system but [some compilers provide the use of `fflush(stdin)` as an extension](http://stackoverflow.com/questions/17827603/scanf-wont-execute-for-second-time/17827635#17827635) – Grijesh Chauhan Aug 31 '13 at 16:55
  • @GrijeshChauhan I don't agree on the terminology: this is still undefined behavior according to the standard. The fact that some implementation makes it documented doesn't change its status vs. the standard. An implementation could also document **its** behavior when you dereference a dangling pointer, but this still UB according to the standard, in other words a program using that extension is, by definition, nonportable. – LorenzoDonati4Ukraine-OnStrike Aug 31 '13 at 17:05
  • @GrijeshChauhan The concept of UB is not related to Unix systems or to whatever environment/implementation you are using, it is a concept related to C language standards. See also [this](http://stackoverflow.com/questions/18420753/unspecified-undefined-and-implementation-defined-behavior-wiki-for-c/18420754#18420754) – LorenzoDonati4Ukraine-OnStrike Aug 31 '13 at 17:06
  • @LorenzoDonati: The behaviour of `fflush(stdin);` is undefined according to the C standard, but implementations are free to add extensions to the standard (else Linux is horribly, horribly non-standard). Microsoft has chosen to make `fflush(stdin)` defined; if you are using Microsoft's C runtime library, it is legitimate to consciously use the extension. – Jonathan Leffler Aug 31 '13 at 22:03
  • If you don't use `break`, when the `randomFunction();` returns, your program is going to exit (once you've got that part sorted out). Inside a `switch` statement, using `break` is normal, sane, expected; not using `break` is abnormal, insane, unexpected. – Jonathan Leffler Aug 31 '13 at 22:12
  • @JonathanLeffler Yes, I knew about extensions. I only argued about terminology. If it is a conscious decision, well that's ok (I'm not arguing against it at all), but I only wanted to highlight that relying on extensions make a program (according to the standard) nonportable, and I doubt that newbies use `fflush(stdin)` with such a consciousness. Anyway thanks for the tip about `fflush(stdin)` being defined by the MS C runtime (didn't know that). I guess this means that I could use it inside programs compiled with MINGW-GCC without invoking UB then. – LorenzoDonati4Ukraine-OnStrike Aug 31 '13 at 22:20
  • I had no idea using `fflush(stdin)` was so bad.. I read it in a book to use for windows/OSX and `fpurge(stdin)` for unix like systems. Thanks for the extra help. @LorenzoDonati I will now read the link you posted so I can find something better to use - possibly not using `getchar()` at all? – Ali Appleby Sep 01 '13 at 15:16

2 Answers2

1

Simply use exit (EXIT_SUCCESS);

Ref -exit

P0W
  • 46,614
  • 9
  • 72
  • 119
  • This works perfectly, thank you so much! Sorry for asking a random question, I tried searching for ages for but I only found results for c++ or c# – Ali Appleby Aug 31 '13 at 16:28
0

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

Edward Clements
  • 5,040
  • 2
  • 21
  • 27