Global Variables in C

A Global variable in C is a variable that is declared outside the function or a block of code. Its scope is the whole program i.e. we can access the global variable anywhere in the C program after it is declared.

Example of Global Variable in C

C
// C program to demonstrate use of global variable
#include <stdio.h>

int x = 20; // global variable

void function1() { printf("Function 1: %d\n", x); }

void function2() { printf("Function 2: %d\n", x); }

int main()
{

    function1();
    function2();
    return 0;
}

Output
Function 1: 20
Function 2: 20

In the above code, both functions can use the global variable as global variables are accessible by all the functions.

Note: When we have same name for local and global variable, local variable will be given preference over the global variable by the compiler.

For accessing global variable in this case, we can use the method mention here.

C Variables

A variable in C language is the name associated with some memory location to store data of different types. There are many types of variables in C depending on the scope, storage class, lifetime, type of data they store, etc. A variable is the basic building block of a C program that can be used in expressions as a substitute in place of the value it stores.

Similar Reads

What is a variable in C?

A variable in C is a memory location with some name that helps store some form of data and retrieves it when required. We can store different types of data in the variable and reuse the same variable for storing some other data any number of times....

C Variable Syntax

The syntax to declare a variable in C specifies the name and the type of the variable....

How to use variables in C?

The below example demonstrates how the use variables in C language....

Rules for Naming Variables in C

You can assign any name to the variable as long as it follows the following rules:...

C Variable Types

The C variables can be classified into the following types:...

1. Local Variables in C

A Local variable in C is a variable that is declared inside a function or a block of code. Its scope is limited to the block or function in which it is declared....

2. Global Variables in C

A Global variable in C is a variable that is declared outside the function or a block of code. Its scope is the whole program i.e. we can access the global variable anywhere in the C program after it is declared....

3. Static Variables in C

A static variable in C is a variable that is defined using the static keyword. It can be defined only once in a C program and its scope depends upon the region where it is declared (can be global or local)....

4. Automatic Variable in C

All the local variables are automatic variables by default. They are also known as auto variables....

5. External Variables in C

External variables in C can be shared between multiple C files. We can declare an external variable using the extern keyword....

6. Register Variables in C

Register variables in C are those variables that are stored in the CPU register instead of the conventional storage place like RAM. Their scope is local and exists till the end of the block or a function....

Constant Variable in C

Till now we have only seen the variables whose values can be modified any number of times. But C language also provides us a way to make the value of a variable immutable. We can do that by defining the variable as constant....

FAQs on C Variables

Q1. What is the difference between variable declaration and definition in C?...