Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   C (https://www.askmehelpdesk.com/forumdisplay.php?f=438)
-   -   C program execution sequence (https://www.askmehelpdesk.com/showthread.php?t=168247)

  • Jan 3, 2008, 02:40 AM
    jinish
    C program execution sequence
    Explain C program execution,what are happened during executing a C program?
  • Jan 5, 2008, 11:02 AM
    asterisk_man
    Please review your class notes and/or textbook for this answer. We do not do your homework for you.
  • Jan 6, 2008, 09:21 AM
    sivasayanth
    Quote:

    Originally Posted by jinish
    Explain C program execution,what are happened during executing a C program?

    I can give one hint or link

    I hope this link will help you

    Howstuffworks "How C Programming Works"

    Howstuffworks "How C Programming Works"
  • Feb 25, 2008, 11:52 PM
    slapshot_oi
    Quote:

    Originally Posted by jinish
    Explain C program execution,what are happened during executing a C program?

    Homework? You have no idea if this is jinish's homework, give him a break asterik_man, it's s simple question, that is what this whole forum is for anyway, questions.

    Memory is organized into four segments:

    Runtime Stack - This is used every time a function is called, main() included. Each record on the stack is called an activation record which stores the function's parameters, the return address of the caller function, and locally defined variables, in that order.
    Heap - A dynamically growing portion of memory, when you use malloc() or define an array, this is where they are stored. Advanced C programs make use of the heap, it's a pain to maintain though.
    Static area - Static and global variables are stored here throughout the life of the program.
    Text area - The assembly code of the program is stored here, this is for the operating system, the programmer can't access this portion of memory.

    Below is an example:
    [CODE=C]
    #include <stdio.h>

    int foo ( int arg ) ;

    int foo ( int arg ) {
    int ans = 10 ;
    arg += ans ;
    return ans ;
    }


    int main ( int argc, char* argv[] ) {
    int num = 5;
    foo ( num ) ;
    return 0 ;
    }
    [/CODE]

    UNIX calls main() and pushes it as an activation record on to the runtime stack. In main's activation record lies argc and argv, followed by a return address back to UNIX and the variable num. Then main() calls foo() which is also pushed on to the runtime stack as an activation record. When foo() returns it value back to main(), foo() is popped off the runtime stack and the record (activation record) of arguments passed and locally defined variables are lost forever, this is a call by value language at work. If you wish to avoid this, use pointers. Finally, when main() returns to UNIX--the return 0 is the error code--it too is popped off the runtime stack and your program is no longer running.

    This is a bad explanation if you can't visualize what I'm saying. My professor had us commit to memory what it all looks like, and I haven't forgot it.
  • Feb 21, 2011, 06:12 AM
    sumi_bose
    The compilation and execution process of C can be divided in to multiple steps:
    • Preprocessing - Using a Preprocessor program to convert C source code in expanded source code. "#includes" and "#defines" statements will be processed and replaced actually source codes in this step.
    • Compilation - Using a Compiler program to convert C expanded source to assembly source code.
    • Assembly - Using a Assembler program to convert assembly source code to object code.
    • Linking - Using a Linker program to convert object code to executable code. Multiple units of object codes are linked to together in this step.
    • Loading - Using a Loader program to load the executable code into CPU for execution.

  • All times are GMT -7. The time now is 09:30 AM.