global and static global variable

Thread Starter

@vajra

Joined May 2, 2018
154
I have declared variable a global variable in first program and static global variable in second program. What is difference between global and static global variable ? Where they both are different in scope or life time of variable ?
C:
#include <stdio.h>
 
/* global variable declaration */
int a = 20;
 
int main () {

  /* local variable declaration in main function */
  int b = 10;

  printf ("value of a = %d\n",  a); 
  printf ("value of b = %d\n",  b);

  return 0;
}
C:
#include <stdio.h>
 
/* global variable declaration */
static int a = 20;
 
int main () {

  /* local variable declaration in main function */
  int b = 10;

  printf ("value of a = %d\n",  a); 
  printf ("value of b = %d\n",  b);

  return 0;
}
 

WBahn

Joined Mar 31, 2012
29,979
Try doing a Google search for something like, oh, global and static global variable, and see if that addresses your need.
 

nsaspook

Joined Aug 27, 2009
13,086
Sure your not thinking of the const qualifier?
Most likely.
Constness means you won't change the value, not that the value won't be changed.

We can modify the const qualifier with volatile (const volatile int temp) to tell the optimizing compiler code can't modify it but an external process can. (for read-only hardware like a status register)
 
Last edited:

Papabravo

Joined Feb 24, 2006
21,159
Static tells the compiler that the variable can't change.
This is nonsense! The static keyword tells the compiler that the scope of the variable is the file in which it is contained. It has nothing to do with being changed or not.
The combination of static and global is actually a conflict. The resolution may be implementation dependent. IMHO the global keyword would nullify the static keyword, as if it was not even there.
 

dl324

Joined Mar 30, 2015
16,846
Sure your not thinking of the const qualifier?
This is nonsense! The static keyword tells the compiler that the scope of the variable is the file in which it is contained. It has nothing to do with being changed or not.
Thank you for being so gracious in the manner in which you pointed out my mistake.

I only took a 1 week course on C and it was a long time ago.
 
Last edited:

Thread Starter

@vajra

Joined May 2, 2018
154
variable may be two two type , Global variable is variable that may be declared outside of main function and local variable is variable that may be declared inside of main function

static may be global and local variable

I think they are only different in scope

Global VariableStatic Global variable
Life timestay alive for life time of programstay alive for life time of program
scope Used in every where in the file
Memory location stack
 

Papabravo

Joined Feb 24, 2006
21,159
variable may be two two type , Global variable is variable that may be declared outside of main function and local variable is variable that may be declared inside of main function

static may be global and local variable

I think they are only different in scope

Global VariableStatic Global variable
Life timestay alive for life time of programstay alive for life time of program
scopeUsed in every where in the file
Memory locationstack
A static local variable is another example of contradiction in declaration. If a variable is local to a function, then it is created on the stack when the function is called and it is released when the function exits. IMHO, the compiler will treat it as if the static keyword was whitespace.
 

nsaspook

Joined Aug 27, 2009
13,086
A static local variable is another example of contradiction in declaration. If a variable is local to a function, then it is created on the stack when the function is called and it is released when the function exits. IMHO, the compiler will treat it as if the static keyword was whitespace.
The static variable declaration in a function allocates it to persistent heap. It's local in scope but maintains it's value when the function exits.
 

bogosort

Joined Sep 24, 2011
696
There is no such thing as a "global" variable in C, which is clear since global is not a valid qualifier. In C, identifiers have an associated duration (lifetime) and linkage (scope). When people say "global variable", they usually mean that the identifier has external linkage, i.e., program-wide visibility across multiple source files. The precise term for this is "external linkage".

Worse still, the static keyword is overloaded, allowing it to be used to qualify duration (static extent, the object lives for the entirety of the program), as well as scope (internal linkage, no visibility outside of the source file). DMR must have been drunk when he decided that would be a good idea.

In the OP's first example, the identifier "a" has been declared outside any block of code, which gives it static duration (lives for the lifetime of the program) and external linkage (can be seen by other source files). In the second example, the identifier "a" has been qualified as static, which doesn't change its duration but does give it internal linkage: the scope of the "a" identifier is valid only within the source file.
 

Papabravo

Joined Feb 24, 2006
21,159
The static variable declaration in a function allocates it to persistent heap. It's local in scope but maintains it's value when the function exits.
If the scope is the file in which it is contained, does that imply that the persistent heap is available to other functions declared in the same file. What about a reuse of the identifier inside another function. Is that the same variable or a separate one? Is this part of the standard or is this implementation dependent?
Which language we are talking about makes a difference, and there are implementation dependencies.
 
Last edited:

nsaspook

Joined Aug 27, 2009
13,086
If the scope is the file in which it is contained, does that imply that the persistent heap is available to other functions declared in the same file. What about a reuse of the identifier inside another function. Is that the same variable or a separate one? Is this part of the standard or is this implementation dependent?
This is a standard C language feature. The scope is the bounds of the specific function block like a local auto variable, not the file. It is a unique identifier only within those bounds.

xc8 example:
C:
// for static file scope of a function
static void led_blink(void);

void clear_2hz(void)
{
    INTCONbits.GIEH = 0;
    V.clock_2hz = 0;
    INTCONbits.GIEH = 1;
}

void clear_500hz(void)
{
    INTCONbits.GIEH = 0;
    V.clock_500hz = 0;
    INTCONbits.GIEH = 1;
}

uint32_t get_2hz(uint8_t mode)
{
    static uint32_t tmp = 0;

    if (mode)
        return tmp;

    INTCONbits.GIEH = 0;
    tmp = V.clock_2hz;
    INTCONbits.GIEH = 1;
    return tmp;
}

uint32_t get_500hz(uint8_t mode)
{
    static uint32_t tmp = 0;

    if (mode)
        return tmp;

    INTCONbits.GIEH = 0;
    tmp = V.clock_500hz;
    INTCONbits.GIEH = 1;
    return tmp;
}

// static file scope function
/*
 * link condition status server via blinking led
 * runs in timer 0 ISR @ 2Hz
 */
static void led_blink(void)
{
    // range checks
    if (V.num_blinks == 255) {
        LED1 = ON;
        V.clock_blinks = 0;
        V.blink_lock = 0;
        return;
    }
    if (!V.num_blinks || V.num_blinks > MAX_BLINKS) {
        LED1 = OFF;
        V.clock_blinks = 0;
        V.blink_lock = 0;
        return;
    }

    // time spacing and blink counter
    if (V.clock_blinks > BLINK_SPACE) {
        if ((BLINK_SPACE + (V.num_blinks << 1)) <= V.clock_blinks) {
            V.clock_blinks = 0;
            LED1 = OFF;
            V.blink_lock = 0;
        } else {
            LED1 = ~LED1;
        }
    }
}
The get functions return their local persistent tmp variable or tmp is updated and returned.
https://raw.githubusercontent.com/nsaspook/ihc_mon/re20a/ihc_vector.c
 
Last edited:

WBahn

Joined Mar 31, 2012
29,979
If the scope is the file in which it is contained, does that imply that the persistent heap is available to other functions declared in the same file. What about a reuse of the identifier inside another function. Is that the same variable or a separate one? Is this part of the standard or is this implementation dependent?
Which language we are talking about makes a difference, and there are implementation dependencies.
Notions such as 'heap' and 'stack' are not part of the language definition -- the words don't even appear anywhere in the standard. The language standard defines behaviors.

nasspook didn't say that static local variables have file scope, he said they have program duration. Very different things.

The 'static' keyword is, unfortunately, schizophrenic. When used to modify a global variable (variable defined outside of any function), it modifies the scope, making that variable visible only to functions within the translation unit (usually the file), but when used to modify a local variable (variable defined within a function), it modifies the lifetime, making it last as long as the program. The latter use is consistent with most notions of "static" variables. I have no idea what Ritchie was smoking when he thought it could possibly be a good idea to reuse the same keyword for a very different use -- it has caused nothing but confusion ever sense.
 

xox

Joined Sep 8, 2017
838
And of course, "const" is really just a polite request.

Code:
const int data = 0;
*((int*)&data) = 1234;
printf("Data: %d\n", data);
 

nsaspook

Joined Aug 27, 2009
13,086
Example:
And of course, "const" is really just a polite request.

Code:
const int data = 0;
*((int*)&data) = 1234;
printf("Data: %d\n", data);
That only works if the const is in ram (a compiler with a OS with MMU hardware might also enforce RO at the kernel level for user applications), for a controller it's in flash so your impolite request will be for nil.
 
Last edited:

WBahn

Joined Mar 31, 2012
29,979
Example:


That only works if the const is in ram (a compiler with a OS with MMU hardware might also enforce RO at the kernel level for user applications), for a controller it's in flash so your impolite request will be for nil.
It's still only a request. While the language standard only says that the compiler CAN honor it, not that it has to, there is nothing that says that the compiler is prohibited from enforcing the request.
 

nsaspook

Joined Aug 27, 2009
13,086
It's still only a request. While the language standard only says that the compiler CAN honor it, not that it has to, there is nothing that says that the compiler is prohibited from enforcing the request.
True but I wouldn't want to use a C compiler that won't allow at least some tricks that can be useful for OS debugging and testing without resorting to ASM. ;)
 
Top