Is there a termination function in C?

  • Thread starter Thread starter rocketboy
  • Start date Start date
  • Tags Tags
    Function
AI Thread Summary
In C programming, there is no direct command to terminate a program based on user input, but it can be achieved using conditional statements. To terminate the program when the user inputs '0', one can check the input using `getchar()` for character input or `atoi()` for string input. The `exit()` function can be used to terminate the program explicitly, with a return code indicating the exit status. A common approach involves using a loop to continuously check for the input condition and breaking out of the loop when '0' is detected, followed by calling `exit(1)` to end the program. Additionally, the `assert` function can be utilized for program termination under specific conditions.
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!
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top