Getting started with C – global variables

Time:2020-2-15

1、 Global variable

  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 

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 

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

4、 Static local variable

  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. Initialization of static local variables will only be done when entering this function for the first time,The value of the last leave will be maintained when entering the function in the future
#include 

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)

5、 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 

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

Recommended Today

Data processing [Swiss Army knife pandas guide]: 5. Data type and missing value

Reference source: vitu.ai Let’s load the data first. import pandas as pd pd.set_option(‘max_rows’, 5) import numpy as np melbourne_data = pd.read_csv(‘melb_data.csv’) melbourne_data.head() data type The data type of a column in a dataframe or series is called a dtype. You can use the dtype property to get the type of a specific column: melbourne_data.Price.dtype Or, […]