Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Termination function in C

  1. Jul 26, 2006 #1

    I was just wondering if there is a simple command in C that terminates the program.

    So say I am getting input from the user, and if they type '0' I want the program to terminate, I would use this in an if statement to close the program.

  2. jcsd
  3. Jul 26, 2006 #2
    As far as I know, no such function exists, at least not in stdio.h. You can still pretty easily check if input is "0." If you're getting a char, see if "the_gotten_char=getchar() == '0'". If you're reading a string, see if "atoi(input_string) == 0".

    If you mean to just press the 0 button (not input "0"), then I don't know...
  4. Jul 26, 2006 #3


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

  5. Jul 26, 2006 #4
    exit(1)...but you could always do

    while (true)
    { if(key==0) break;
    Last edited: Jul 26, 2006
  6. Jul 27, 2006 #5
    exit(1) or assert (for assert #include <assert.h>) should do the trick
  7. Jul 27, 2006 #6
    thx everyone!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook