Getting started with C - global variables

Keywords: C

I. global variables

  1. Variables defined outside functions are global variables
  2. Global variables have global lifetime and scope
  3. They are independent of any function and can be used inside any function
#include <stdio.h>

int f(void);


// global variable
int gALL = 12;


int main(int argc, char const *argv[])
{
    printf("in %s gALL=%d\n", __func__,gALL);
    f();

    printf("agn in %s gALL=%d\n", __func__,gALL);

    return 0;
}

int f(void)
{
    printf("in %s gALL=%d\n", __func__,gALL);
    gALL += 8;
    printf("agn in %s gALL=%d\n", __func__,gALL);
    return gALL;
}


// in main gALL=12
// in f gALL=12
// agn in f gALL=20
// agn in main gALL=20

2. Global variable initialization

  1. Global variables that are not initialized will get a value of 0
  2. The pointer will get a NULL value
  3. Global variables can only be initialized with values known at compile time
  4. Their initialization occurs before the main function

3. Hidden global variables

If there is a variable with the same name as the global variable inside the function, the global variable is hidden.

#include <stdio.h>

int f(void);


// global variable
int gALL = 12;


int main(int argc, char const *argv[])
{
    printf("in %s gALL=%d\n", __func__,gALL);
    f();

    printf("agn in %s gALL=%d\n", __func__,gALL);

    return 0;
}

int f(void)
{
    // If a variable with the same name is defined here, the global one will be hidden.
    int gALL = 1;
    printf("in %s gALL=%d\n", __func__,gALL);
    gALL += 8;
    printf("agn in %s gALL=%d\n", __func__,gALL);
    return gALL;
}
// in main gALL=12
// in f gALL=1
// agn in f gALL=9
// agn in main gALL=12

IV. static local variables

  1. Adding a static modifier to the definition of a local variable becomes a static local variable
  2. When the function leaves, the static local variable continues to exist and maintains its value
  3. The initialization of static local variables will only be done when entering this function for the first time, and the value left last time will be maintained when entering the function later.
#include <stdio.h>

int f(void);

int main(int argc, char const *argv[])
{
    f();
    f();

    return 0;
}

int f(void)
{
    static int all = 1;
    printf("in %s all=%d\n", __func__,all);
    all += 9;
    printf("agn in %s all=%d\n", __func__,all);
    return all;
}
// in f all=1
// agn in f all=10
// in f all=10
// agn in f all=19

Matters needing attention

  1. Static local variables are actually special global variables
  2. They are in the same memory area
  3. Static local variables have global lifetime and local scope within the function
  4. static here means local scope (accessible locally)

V. postscript: function returning pointer

  1. It is dangerous to return the address of a local variable
  2. It is safe to return the address of a global or static local variable
  3. It is safe to return the memory of malloc in the function, but it is easy to cause problems.
  4. The best way to do this is to return the passed in pointer

The following is dangerous.

#include <stdio.h>

int* f(void);
void g(void);

int main(int argc, char const *argv[])
{
    int *p = f();
    printf("*p = %d\n", *p);
    g();
    printf("*p = %d\n", *p);

    return 0;
}

int* f(void)
{
    int i = 12;

    // It is dangerous to return the address of a local variable
    return &i;
}

void g(void)
{
    int k = 24;
    printf("k = %d\n", k);
}

// *p = 12
// k = 24
// *p = 24

tips

  1. Do not use global variables to pass parameters and results between functions
  2. Try to avoid using global variables
  3. Functions that use global and static local variables are thread unsafe

Posted by CodeWriter on Tue, 29 Oct 2019 13:53:48 -0700