w3resource logo


C Programming Exercises

C Exercises: Set a function that will be executed on termination of a program

Secondary Nav

C Variable Type : Exercise-15 with Solution

Write a C program to set a function that will be executed on termination of a program.

Expected Output :

This is the message from main function.                                
 Here is the message returning from newFunctionTwo.                     
 Here is the message returning from newFunctionOne.

C Code:

#include<stdio.h>
#include<stdlib.h>

void newFunctionOne (void)
	{
		puts (" Here is the message returning from newFunctionOne.");
	}
	void newFunctionTwo (void)
	{
		puts (" Here is the message returning from newFunctionTwo.");
	}
	int main ()
	{
		atexit (newFunctionOne);
		atexit (newFunctionTwo);
		puts ("\n This is the message from main function.");
		return 0;
	}

Solution

#include<stdio.h>
#include<stdlib.h>

void newFunctionOne (void)
	{
		puts (" Here is the message returning from newFunctionOne.");
	}
	void newFunctionTwo (void)
	{
		puts (" Here is the message returning from newFunctionTwo.");
	}
	int main ()
	{
		atexit (newFunctionOne);
		atexit (newFunctionTwo);
		puts ("\n This is the message from main function.");
		return 0;
	}

Improve this sample solution and post your code through Disqus



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program