473,320 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Is execution transferred immediately to calling function when return is encountered?

AmberJain
884 Expert 512MB
HELLO,

Wikipedia defines return statement as:
In computer programming, a return statement causes execution to leave the current subroutine and resume at the point the subroutine was called -- known as its return address.


Now there's a C program I have coded---->

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. void func(void)
  3. {
  4.    printf("HELLO in func()");
  5.    getch();
  6. }
  7.  
  8. int main(void)
  9. {
  10.    printf("HELLO 1 main()\n\n");
  11.    atexit(func);
  12.    printf("HELLO 2 main()\n\n");
  13.    getch();
  14.    return 0;
  15. }
  16.  
The confusion is that wikipedia (and other books) say that when a return statement is called from a function, the execution transfers immediately to the function associated with its return address. But there is a standard C function atexit() which (probably) contradicts this statement. It executes the function whose name is passed to atexit() as arguement whenever a return statement is encountered in a function currently executing.


I'm confused. Please tell me where am I wrong.
THANKS IN ADVANCE...............
Jul 27 '08 #1
2 1435
JosAH
11,448 Expert 8TB
It executes the function whose name is passed to atexit() as arguement whenever a return statement is encountered in a function currently executing.
No it doesn't. The atexit() function executes the registered function(s) when the
program attempts to exit only. First those functions are executed and only then
control is transfered back to the hosting OS.

Your return from your main() function attempts to exit the entire program and
therefore atexit() does its deeds.

kind regards,

Jos
Jul 27 '08 #2
AmberJain
884 Expert 512MB
No it doesn't. The atexit() function executes the registered function(s) when the
program attempts to exit only. First those functions are executed and only then
control is transfered back to the hosting OS.

Your return from your main() function attempts to exit the entire program and
therefore atexit() does its deeds.

kind regards,

Jos
THANKS.....................

I also found this link which tells more about atexit()--------->
LINK
Jul 27 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Greg Swindle | last post by:
Hello, I have a question about how prototyping relates to variables and their scope. Given the following code: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ var ParentObject = function() {...
1
by: Jesper | last post by:
Hi, Background. In an engineering application I consider using events to control the flow in the program under the following philosophy: When a value X change it fires a "i'm changed" event. A...
3
by: Robert Sorger | last post by:
I have a problem with thread execution after calling response.redirect() in a web form. Basically, I want the thread to continue to work (e.g. do some long database work) but want to redirect...
23
by: roman | last post by:
Hi, I would like to have two actions for one event. But I want the second action to trigger when the first one action completes. Is it possible to do this in javascript? I'm using the onclick...
40
by: kavi | last post by:
Hello friends, Could any one tell the way of calculating the speed of c program execution?
0
by: Saran | last post by:
Hi, I am having a method that handles the click event of a button on the page. This method will also send an email. Sending email comes after several lines of code in the method. I was testing...
18
by: Tom Cole | last post by:
I'm working on a small Ajax request library to simplify some tasks that I will be taking on shortly. For the most part everything works fine, however I seem to have some issues when running two...
17
by: blufox | last post by:
Hi All, Can i change the execution path of methods in my process at runtime? e.g a()->b()->c()->d()->e() Now, i want execution to be altered at runtime as -
3
by: Dmitri Fedoruk | last post by:
Hello everyone, I'm developing a mod_python application that is based on XML\XSLT transforming. I used 4Suite libraries for that, but as the speed was unacceptable for me, I switched to lxml....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.