Displaying dummy prompt using the exit() and atexit() C functions

 

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: none

To do: Displaying dummy prompt using the exit() and atexit() C functions

To show: How to use exit() and atexit() C functions in programming

 

 

// Demonstrates the exit() and atexit() functions

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

 

#define DELAY 100000000

 

// function prototypes

void cleanup(void);

void delay(void);

 

void main(void)

{

int i, reply;

// register the function to be called at exit

atexit(cleanup);

puts("Enter 1 to exit, any other to continue.");

/* scanf("%d", &reply); */

scanf_s("%d", &reply, 1);

 

if(reply == 1)

{

printf("You just want to exit?\n");

exit(EXIT_SUCCESS);

}

 

// pretend to do some work

for(reply = 0; reply < 5; reply++)

{

printf("Working on step #%d, please wait!", reply);

delay();

for(i=0;i<20;i++)

{

printf(".");

delay();

}

printf("Assumed done!\n");

}

}// end of main

 

// function definition...

void cleanup(void)

{

puts("\nI'm in cleanup(), ready to exit\n");

printf("Please wait...\n");

delay();

}

 

// function definition

void delay(void)

{

long x;

for(x = 0; x < DELAY; x++)

;

}

 

Output example:

 

Enter 1 to exit, any other to continue.

3

Working on step #0, please wait!....................Assumed done!

Working on step #1, please wait!....................Assumed done!

Working on step #2, please wait!....................Assumed done!

Working on step #3, please wait!....................Assumed done!

Working on step #4, please wait!....................Assumed done!

I'm in cleanup(), ready to exit

Please wait...

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index