How to define global variables in C language

How to define global variables in C language

To define a global variable, you need to define the variable outside the main function.
For example:

/*
Example: how to declare global variable in C language
*/

#include <stdio.h>
#include <stdlib.h>

int n = 7; // Define a global variable
void display()
{
    int n = 12;
    printf("In the display function: n = %d\n", n);
}

int main(void) {
    display();
    printf("In the main function: n = %d\n", n);

    {
        n = 11; // modify global variable
        printf("in code block n = %d\n", n);
    }
    printf("after code block n = %d\n", n);
    return 0;
}
‮S‬ource:www.lautturi.com

output:

In the display function: = 12
In the main function: n = 7
in code block n = 11
after code block n = 11
Created Time:2017-08-29 04:36:15  Author:lautturi