Static and Global variables

This is a discussion on Static and Global variables within the C Programming forums, part of the General Programming Boards category; Hello to everyone. I found that a static or global variable will lose its "power" if you assign a value ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    283

    Static and Global variables

    Hello to everyone. I found that a static or global variable will lose its "power" if you assign a value after initialization. Here is an example

    Code:
    #include <stdio.h>
    int foo(int x , int y);
    //int c=1;
    
    	int main(void)
    	{
    		int i , j;
    		
    		for(i=0 , j=5; i<5; i++ , j++)
    		printf(" The addition of %d and %d is %d. \n \n" , i , j , foo(i,j));
    		
        return 0;
    }
    int foo(int x , int y)
    {
    	static int c=1;
    	
    	c=2;
    	
    	printf(" This is the call of the function : %d \n " , c++);
    	
    	return x+y ;
    }
    //
    For instance I have had trials on the above code that is why I have "//" on the third line.

    The output of c variable is 2 , 2 , 2 , 2 , 2

    not 2 , 3 , 4 , 5 , 6

    If I reject the line c=2;

    the output of c variable will be 1 , 2 , 3 , 4 , 5.

    My question is... what is happen? The assignment c=2 after initialization has the power to "destroy" a static nature of a variable as we can see?

    THank you in advance.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,421
    > My question is... what is happen? The assignment c=2 after initialization has the power to "destroy" a static nature of a variable as we can see?
    Just remove c=2

    Having c=2 makes whether the variable is static or not irrelevant.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    The static variable c can be looked at in one sense as a variable with duration similar to a global but with limited scope - just visible within the foo function. The initialization to 1 happens the first time through the function. Thereafter, the assignment of 2 happens every time you enter the function. It should be no surprise that the value is 2 every time you print its value. Even though the function increments the value each time, it is always set back to 2 when entering the function. Why would this be surprising given the behavior you expect with the increment step itself? You expect an increment to increase the value and that is the behavior you see when you comment out the assignment step. With the assignment in place would you suddenly expect nothing to happen to variable? Would you expect the assignment to not affect the value of c at all?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

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, 12:00 AM
  2. static global & global variables
    By aqeel in forum C Programming
    Replies: 1
    Last Post: 09-25-2009, 12:32 PM
  3. Calling non-static functions on static variables
    By pandu in forum C++ Programming
    Replies: 14
    Last Post: 06-19-2008, 03:07 AM
  4. static global
    By rajkumarmadhani in forum C Programming
    Replies: 19
    Last Post: 01-15-2008, 12:07 PM
  5. Replies: 2
    Last Post: 10-02-2004, 10:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21