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; }Source: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