CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C DETAILS
Question :
Is it possible to execute code even after the program exits the main() function?



Category C Interview Questions
Rating (0.0) By 0 users
Added on 7/19/2006
Views 2532
Rate it!
Answers:

The standard C library provides a function named atexit() that can be used to perform ?cleanup? operations when your program terminates. You can set up a set of functions you want to perform automatically when your program exits by passing function pointers to the atexit() function.



it is not at all possible to execute the code after exit the main.



 Posted by: sreelatha    

Contact sreelatha  Contact sreelatha

Yes, It is possible execute code even after the program exits the main() function.
Explanation :-
We can crate Child Process(Thread)and make the child process to sleep and by that time, we can make the Main thread to finish its execution and exit. ( In C main() act as the Main Thread ).After the main thread exits, the child thread can run after coming from sleep state.



 Posted by: Utkal    

Contact Utkal  Contact Utkal

the atexit() function is called before exiting the main() function and it is not at all possible to carry on execution after exiting the main() function.



 Posted by: Nilotpal    

Contact Nilotpal  Contact Nilotpal

Using atexit() we can execute code even after exit from main program.

I think the following program will help in understanding the concept.

Here, I am initializing the function that has to be executed after exit of the main program, printing some output and calling the return, which exits the function. Later i am printing some more lines of code (which i am sure that wont be called as the function is exited). Once the function exits, it will call the funcion which is initialized using atexit().

#include<stdio.h>

void myFunction_Onexit()
{
printf(" Welcome to user exit function. Here u can do anything ");
system("pause");
}
int main()
{

printf(" In main function before return ");
atexit(myFunction_Onexit);
system("pause");
return 0;
printf(" In main function after return ");
system("pause");
return 0;

}



 Posted by: Syed Baseer Ahmed    

Contact Syed Baseer Ahmed  Contact Syed Baseer Ahmed


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
What is a static function?


View Answer
How do you print an address?


View Answer
What is the difference between NULL and NUL?


View Answer
What is the stack?


View Answer
What is a null pointer?


View Answer
How can I convert a number to a string?


View Answer
What is the difference between #include <file> and #include ?file??


View Answer
What is Preprocessor?


View Answer
What is the difference between text and binary modes?


View Answer
What is the benefit of using const for declaring constants?


View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/6069/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

0.66