Global Variables in C Language

https‮l.www//:‬autturi.com
Global Variables in C Language

How to define global variables in C

In C, global variables are defined outside the main function.
The scope of global variables is global, that is, all scopes can access global variables.

example

// declare global variable in c language

#include <stdio.h>
void display();

int a = 5;  //  global variable

int main()
{
    ++a;     
    display();	  
    display2();
    return 0;
}

void display()
{
    ++a;   
    printf("a = %d", a);
}

void display2()
{
    int a = 3; // local variable
    ++a;
    printf("a = %d", a);

    {
       extern int a; // When local variables conflict with global variables, use extern to reference global variables
        ++a;
        printf("a = %d", a);
    }
}
Created Time:2017-08-28 21:29:46  Author:lautturi