Is there a termination function in C?

  • Thread starter Thread starter rocketboy
  • Start date Start date
  • Tags Tags
    Function
Click For Summary

Discussion Overview

The discussion revolves around the question of whether there is a specific command in the C programming language that can terminate a program. Participants explore various methods for achieving program termination based on user input, particularly focusing on the scenario where the user inputs '0'.

Discussion Character

  • Exploratory, Technical explanation, Debate/contested

Main Points Raised

  • One participant inquires about a simple command in C to terminate a program based on user input.
  • Another participant suggests that no direct termination function exists in stdio.h, but proposes checking for input '0' using methods like getchar() or atoi().
  • A third participant provides a link to the exit function documentation, indicating that it can be used for program termination.
  • One participant mentions using exit(1) as a method to terminate the program and suggests a loop structure to check for a key press.
  • Another participant mentions that exit(1) or assert can be used to terminate the program.

Areas of Agreement / Disagreement

Participants express varying opinions on the existence of a simple termination command, with some suggesting alternative methods. No consensus is reached on a single definitive approach.

Contextual Notes

Some methods proposed depend on specific input types and conditions, and the discussion does not resolve the best approach for all scenarios.

Who May Find This Useful

Readers interested in C programming, particularly those looking for ways to manage program termination based on user input.

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!
 
Technology 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...
 
http://www.cplusplus.com/ref/cstdlib/exit.html

- Warren
 
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
 
thx everyone!
 

Similar threads

Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
11
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
38
Views
5K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
5
Views
2K