Is there a termination function in C?

  • Thread starter Thread starter rocketboy
  • Start date Start date
  • Tags Tags
    Function
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
5 replies · 3K views
rocketboy
Messages
243
Reaction score
1
Hey,

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.

Thanks!
 
Physics news on Phys.org
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...
 
exit(1)...but you could always do

main...
{
while (true)
{ if(key==0) break;
}
}
 
Last edited:
exit(1) or assert (for assert #include <assert.h>) should do the trick