How can I add a time delay before the exit command in C/C++?

  • Context: C/C++ 
  • Thread starter Thread starter Akshay_Anti
  • Start date Start date
  • Tags Tags
    Delay
Click For Summary

Discussion Overview

The discussion revolves around how to implement a time delay before executing the exit command in C/C++ programs. Participants explore various methods to achieve this, including displaying messages before exiting.

Discussion Character

  • Exploratory
  • Technical explanation

Main Points Raised

  • One participant asks about adding a delay before the exit(0) command to allow for a message display.
  • Another participant suggests using a sleep command to introduce a delay.
  • A different approach is proposed where the program could prompt the user to press the enter key before exiting, effectively waiting for user input.

Areas of Agreement / Disagreement

Participants present multiple approaches to the problem, but there is no consensus on a single solution or method to implement the delay.

Contextual Notes

Some methods mentioned may depend on specific libraries or system calls, and the effectiveness of each approach could vary based on the programming environment.

Akshay_Anti
Messages
62
Reaction score
0
Hello! I was writing a basic program and was wondering about this problem:-

when i give exit(0); the compiler exits as soon as that part of code is reached. Is there any piece of code such that after it reaches that part of code, it waits for some time before executing exit command? This can be used in case when upon exit, a message is to be displayed.

Thank you in advance
 
Technology news on Phys.org
thanks! :)
 
In your program you could include code just before the exit(0) to display a message to indicate the user should press the enter key, then wait for user input.
 

Similar threads

Replies
14
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 10 ·
Replies
10
Views
6K