Thread: what happen during the execution of code

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    20

    what happen during the execution of code

    I need help to understand program

    Code:
    #include<stdio.h>
    
    void function (void);
    
    
    int main (void)
    {
      int a = 5;
    
    
      printf ( "\n a : %d", a);  
      
      function();
         
        return 0;
    }
    
    
    void function (void)
    
    
    {
    	int b = 15;
    	b++;
    	printf ("\n b : %d", b);  
    }
    a : 5
    b : 16

    There are two variables a and b

    I do not understand what happen during the execution of code

    How long variable a will exit ?
    When we call function how long variable b will exit ?
    Where does it will store stack or heap ?

    any help would be appreciated

  2. #2
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    814
    How long variable a will exit ?
    Code:
    int main (void)
    {
        int a = 5; // 'a' starts existing here
        printf ( "\n a : %d", a);  
        
        function(); // 'b' starts existing and stops existing here
          
        return 0; // 'a' stops existing here
    }
    
    void function (void)
    {
        int b = 15; // 'b' starts existing here
        b++;
        printf ("\n b : %d", b);
    } // 'b' stops existing here
    Where does it will store stack or heap ?
    All on the stack.

    p.s. please format your code properly next time, i.e. with consistent indentation and no large gaps.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    534
    Quote Originally Posted by -Adrian View Post
    All on the stack.
    Depends on the compiler optimization. Could be on registers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code execution speed - which is fastest?
    By onelinecode in forum C Programming
    Replies: 2
    Last Post: 04-07-2015, 01:03 PM
  2. Code is right. Execution is wrong! HELP
    By exus69 in forum C Programming
    Replies: 2
    Last Post: 04-23-2012, 06:14 AM
  3. Replies: 10
    Last Post: 02-28-2012, 02:12 AM
  4. Replies: 3
    Last Post: 10-12-2009, 08:24 AM
  5. Arbitrary Code Execution
    By CrazyNorman in forum C++ Programming
    Replies: 3
    Last Post: 04-10-2007, 02:33 PM

Tags for this Thread