Thread: what is difference between global variable and static variable in the context of life

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    66

    what is difference between global variable and static variable in the context of life

    I am not clear about the life time of global variable and static variable I think both initialize only once and stay alive for life time of program

    what is difference between global variable and static variable in the context of life time

    Edit : static may be local or global. What is difference between local static and global static variable. I think the difference is only with the scope not in life time
    Last edited by Player777; 04-13-2020 at 12:40 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,165
    Code:
    int a;
    static int b;
    void foo ( ) {
        static int c;
    }
    All have a lifetime the same as the program.
    All are default initialised to 0, unless you provide some other initialiser.

    Then
    a is visible to the entire program.
    b is visible to all the functions in the current source file that appear after the definition of b.
    c is visible only to the function in which it is defined.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    66
    Quote Originally Posted by Salem View Post
    Code:
    int a;
    static int b;
    void foo ( ) {
        static int c;
    }

    .
    Thank you salem

    modified version

    Code:
    int a; //Global variable
    
    
    static int b; // Static Global variable
    void foo ( ) {
        static int c; //Static local variable
    }
    
    
    int main (){
         foo();
    	 
    	 return 0;
    	 }
    All have a lifetime the same as the program.
    All are default initialised to 0, unless you provide some other initialiser.

    Then
    a is visible to the entire program.
    b is visible to all the functions in the current source file that appear after the definition of b.
    c is visible only to the function in which it is defined

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static global variable and static extern global variable
    By gunjansethi in forum C Programming
    Replies: 8
    Last Post: 01-12-2011, 01:00 AM
  2. Replies: 8
    Last Post: 09-27-2010, 04:11 PM
  3. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  4. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  5. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 08:46 AM

Tags for this Thread