static global variable

Go To Last Post
23 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

I do not understand difference between global and static global variable. I know global variable can be access anywhere in program where as local variable is only accessible in the function and static is storage class

 


#include <stdio.h>

int a = 0;

static int b= 0;
 
void foo()
{
   static int c = 0;
      printf("%d \n", c);
}
 
int main()
{
    foo();
    foo();
    
    printf("%d \n", a);

    printf("%d \n", b);
  
    return 0;
}

In  program, a is global variable and b is static global variable I don't understand how static global variable different then the global variable in c language 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This sums it up pretty good

 

https://www.geeksforgeeks.org/static-variables-in-c/

 

Static variables are useful such as remembering their "previous" value...they aren't destroyed when a function is finished & don't reinitialize to the initial value (they keep their last value).

If you don't make use of any of these aspects, you might not notice the difference.  However, that works the other way too--if you expect a reinitialization each time, not having reinitialization can cause the rocket to miss Mars. 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Sat. Apr 4, 2020 - 03:14 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

static limits the visibility of the variable or function to the current translation unit.

github.com/apcountryman/build-avr-gcc: a script for building avr-gcc

github.com/apcountryman/toolchain-avr-gcc: a CMake toolchain for cross compiling for the Atmel AVR family of microcontrollers

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Static declarations are only meaningful within functions.

Hence your 

static int b = 0;

is identical to

int b = 0;

if they're outside of any function. However, in your foo() function, add the following line:

c++;

between the declaration and the printf, and you'll see it will increment the value of c of the previous call.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

martinharnevie wrote:
Static declarations are only meaningful within functions.

No! That is not true!

 

As already noted, 'static' outside a function limits the scope of the variable to the containing file.

 

Similarly,  'static' applied to a function limits the scope of the function to the containing file.

 

muke12 wrote:
I do not understand difference between global and static global variable.

There is no such thing as a "static global" - a variable defined outside a function is either global or, if 'static', has file scope.

 

EDIT

 

muke12 wrote:
In  program, a is global variable and b is static global

No.

 

  • a is global
  • b is not global; it has file scope - ie, it is visible throughout the file, but not outside the file.

 

EDIT 2

 

A recent post on the uses & meanings of the 'static' keyword in 'C' :

 

https://www.avrfreaks.net/commen...

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Sat. Apr 4, 2020 - 08:57 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

 

  • a is global
  • b is not global; it has file scope - ie, it is visible throughout the file, but not outside the file.

Being in pedantic mode, I would say both a and b have file scope, the difference is a has external  linkage and b has internal linkage.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MrKendo wrote:
Being in pedantic mode

Yes, in pedantic mode, the 'C' language specification does not use the term 'global' at all.

 

both a and b have file scope

which is why, if we want a to be visible in a different file, we have to declare it in that file (ie, in that scope) scope as extern ...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Sat. Apr 4, 2020 - 09:58 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When not in pedantic mode, i would definitely refer to 'a' as global.

For 'b' I would probably say 'static global' like the OP, with the understanding that 'static global' menas 'throughout this file only'.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I wonder why the C Standards committee haven't taken any opportunity to sort out the utter mess around the static keyword.

 

For the static int b= 0; declaration at file scope they could have introduced borrowed one from Java or C++.  They could choose private or protected which describes it's effect much better that simply static.

 

C++ standards seem to change every few minutes but C standards are fairly; erm; static.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
if we want a to be visible in a different file, we have to declare it in that file (ie, in that scope) scope as extern ...

 

looking further into this, I found something I didn't know before!

 

extern can be used to "introduce" a symbol into any scope - it is not just limited to the file scope.

 

For instance, we could have:

void my_function( void )
{
    extern int foo;

    :
    :
    // code here can see 'foo'

}

So the symbol foo is visible within that function - but not outside that function.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I can never remember what all the rules are. How about this.

int foo = 1;

int main(void)
{
    int foo = 3;
    {
        extern int foo;
        printf("foo %d\n", foo);
    }
    printf("foo %d\n", foo);
}

It prints

foo 1
foo 3

 

Hopefuly would never want to do this in real code!

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

muke12 wrote:
I don't understand how static global variable different then the global variable in c language

Your a and b differ in that a could be accessed by any file in your project but b can only be accessed in this file where it appears.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

muke12 wrote:
I don't understand how static global variable different then the global variable in c language

 

"Static global variable" is a misnomer. As long as we are talking about variables declared at file level (not `static` class members in C++) a `static` variable is local to the given implementation file (it is formally called "a translation unit"). It is not really "global" at all.

 

If your project consists of multiple implementation files, you can have a `static` variable of the same name in each implementation file. All these variables are separate independent variables. They don't interfere with each other. They know nothing about each other. And that is exactly what static variables are for.

 

And this is not the case with global (i.e. non-static) variables. For global variables, the same name refers to the same single variable for the whole program. That's why they are called "global". That is exactly what global variables are for.

 

Declaring file-level variables as `static` whenever possible can have huge optimization benefits, i.e. it can help the compiler to generate more efficient code. For example, if your project consist of only one implementation file, all your file-level variables should be declared `static`.

Dessine-moi un mouton

Last Edited: Mon. Apr 6, 2020 - 03:23 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

extern can be used to "introduce" a symbol into any scope - it is not just limited to the file scope.

 

This is in no way different from placing function declarations (i.e. prototypes) inside other functions. For some reason, many people find this possibility surprising.

Dessine-moi un mouton

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MrKendo wrote:
 I would probably say 'static global' like the OP, with the understanding that 'static global' menas 'throughout this file only'.

But, as  AndreyT says, that goes entirely against the meaning of the word, "global".

 

The word "global" mean "everywhere" - so anything that's not visible throughout the entire project should not be called "global".

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AndreyT wrote:
This is in no way different from placing function declarations (i.e. prototypes) inside other functions

That's why I said "symbol" - to allow for both variables and functions.

 

For some reason, many people find this possibility surprising

As noted, I hadn't realised it before.

 

I don't recall ever having seen it used.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

MrKendo wrote:

 I would probably say 'static global' like the OP, with the understanding that 'static global' menas 'throughout this file only'.

 

But, as  AndreyT says, that goes entirely against the meaning of the word, "global".

 

The word "global" mean "everywhere" - so anything that's not visible throughout the entire project should not be called "global".

Well, the term 'global', as others have said, is not a term that is used within the C standard. So we are aleady talking about colloquial tems.

Most people would understand the term 'global' as meaning what would technicaly be called 'file scope object with external linkage'. That's the non static case. Fair enough.

But what colloquial term should be used for the static case, what technically would be called 'file scope object with internal linkage' ?

I'm just suggesting 'static global' is a reasonable colloquial term, people who aren't language lawyers will probably understand what you mean ie. 'everywhere within the current translation unit'.

But I'm open to using other colloquial terms.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MrKendo wrote:
we are aleady talking about colloquial tems.

I think the use and meaning of "global" is a widely accepted idiom across all types of programming:

 

https://en.wikipedia.org/wiki/Global_variable

 

Most people would understand the term 'global' as meaning what would technicaly be called 'file scope object with external linkage'.

Yes - as above

 

But what colloquial term should be used for the static case

Clearly not "global" - because it isn't global !

 

"File local" would seem more logical - as the symbol is local to the (entire) file.  Or translation unit, if you're being pedantic.

 

EDIT

 

And "static global" would cause confusion in contexts where dynamic allocation is common - where "static" is used in contrast to "dynamic" (allocation)

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Mon. Apr 6, 2020 - 08:31 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This discussion is getting so SO.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

One reasonable definition of global

 

" of, relating to, or applying to a whole"

 

The whole can be the whole of the file, or it can be the whole of the entrie program. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As noted, It is widely (globally?) accepted to mean the entire program.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So you're using "global" in the sense that Americans speak of the "World (sic) Series" ?

 

https://en.wikipedia.org/wiki/World_Series

 

laugh

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...