Assuming your variable is global and non static.

You need to declare it in a header file. We use extern keyword for this. As pointed out in comments, this keywords is not necessary, but most C users prefer to use it in headers, this is a convention.

stackoverflow.h:

#ifndef STACHOVERFLOW_H
#define STACHOVERFLOW_H

extern int my_var;

#ifndef

And you initialize it in source file. (Use of keyword extern is prohibited if you want to provide an initialization value).

stackoverflow.c

#include "stackoverflow.h"

int my_var = 50;

Do not put initialization value in a header, or you will get a linker error if the header is used at least twice.

Now you can use your variable in any other module by including the header.

main.c

#include <stdio.h>
#include "stackoverflow.h"

int main()
{
    printf("my_var = %d\n", my_var);
    return 0;
}

Including header "stackoverflow.h" in "stackoverflow.c" is a way to get sure definitions in source file match declarations in header file. This permit to have errors as soon as compilation instead of sometimes cryptic linker errors.

Edit: This is not at all the way to make a variable "private". You have to use a static variable to make it "private". See R Sahu's answer

Answer from jdarthenay on Stack Overflow
🌐
Quora
quora.com › What-is-a-global-declaration-section-in-the-C-programming-language
What is a global declaration section in the C programming language? - Quora
Answer (1 of 2): When you declare ... is called global declaration section. See the following example. int a; int main(){ … } void fun(){ … } Here a is global variable with intege......
Top answer
1 of 6
6

Assuming your variable is global and non static.

You need to declare it in a header file. We use extern keyword for this. As pointed out in comments, this keywords is not necessary, but most C users prefer to use it in headers, this is a convention.

stackoverflow.h:

#ifndef STACHOVERFLOW_H
#define STACHOVERFLOW_H

extern int my_var;

#ifndef

And you initialize it in source file. (Use of keyword extern is prohibited if you want to provide an initialization value).

stackoverflow.c

#include "stackoverflow.h"

int my_var = 50;

Do not put initialization value in a header, or you will get a linker error if the header is used at least twice.

Now you can use your variable in any other module by including the header.

main.c

#include <stdio.h>
#include "stackoverflow.h"

int main()
{
    printf("my_var = %d\n", my_var);
    return 0;
}

Including header "stackoverflow.h" in "stackoverflow.c" is a way to get sure definitions in source file match declarations in header file. This permit to have errors as soon as compilation instead of sometimes cryptic linker errors.

Edit: This is not at all the way to make a variable "private". You have to use a static variable to make it "private". See R Sahu's answer

2 of 6
5

which place is better to declare a global variable in c program

Answer: In source(*.c) file.

Assume the scenario like, I have declared a variable in a header file. I included this header in two different .c files. After the macro expansion step of compilation, these two files will have the global variable with the same name. So it will throw an error like multiple declarations of the variable during the linking time.

Conclusion:-

Keep all global variable declaration on .c file and put it as static if it is doesn't need in other files.

Add extern declaration of the variable in the corresponding header file if it's needed to access from other files

People also ask

How to Declare Global Variable in C?
A global variable in C is declared outside of all functions, usually at the top of the program, so it can be accessed by all functions.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › global-variables
Global Variables in C Programming (With Examples)
What is global variable in C?
A global variable in C is declared outside all functions and can be accessed throughout the entire program.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › global-variables
Global Variables in C Programming (With Examples)
Is it mandatory to declare a global variable during the beginning of a program?
Yes, it is. This way, all the functions get access to the variable throughout the time when that program is in running.
🌐
byjus.com
byjus.com › gate › global-variable-in-c
Use of the Global Variable in C
🌐
TutorialsPoint
tutorialspoint.com › home › cprogramming › c global variables
C Global Variables
June 10, 2012 - Global variables are declared outside of any function, so they can be accessed by all functions within the same file by default. In this example, we declared a global variable (x) before the main() function.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › global-variables-in-c
Global Variables in C - GeeksforGeeks
The only difference is that the global variable is declared outside any function. We can take an example by assuming that we have a chair at our house and one in our school/college then we can say that the chair at our home can only be accessed by the people living inside the home but the chair in our college can be used by any student or faculty.
Published   July 23, 2025
🌐
Hero Vired
herovired.com › learning-hub › topics › global-variable-in-c
Global Variable in C Programming: Examples, How to Declare
So, if we declared int counter without assigning a value, it would start at 0. Global variables can be of any data type—int, float, char, you name it. And we can initialise them just like any other variable. But here’s where it gets interesting. What happens if we need the user to input a value? Let’s tweak our example:
🌐
BYJUS
byjus.com › gate › global-variable-in-c
Use of the Global Variable in C
September 20, 2022 - Here is a program that shows how we use global variables practically: ... You can notice that in line 4, x and y get declared as two of the global variables of the type int. Here, the variable x will get initialized automatically to 0. Then ...
🌐
WsCube Tech
wscubetech.com › resources › c-programming › global-variables
Global Variables in C Programming (With Examples)
1 week ago - Learn in this tutorial about C global variables with examples. Understand their scope, usage, benefits, and limitations. Read now!
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › home › cprogramming › c programming scope rules
C Programming Scope Rules
June 10, 2012 - Let us understand what are local and global variables, and formal parameters. Variables that are declared inside a function or block are called local variables. They can be used only by statements that are inside that function or block of code. Local variables are not known to functions outside their own. The following example ...
🌐
University of Texas
farside.ph.utexas.edu › teaching › 329 › lectures › node19.html
Global variables
It is possible to define variables which are global in extent: such variables are recognized by all the functions making up the program, and have the same values in all of these functions. The C compiler recognizes a variable as global, as opposed to local, because its declaration is located ...
🌐
Testbook
testbook.com › home › gate › global variable in c: usage, examples, redefinition & practice problems
Global Variable in C: Usage, Examples, Redefinition & Practice Problems
Most often, global variables are declared before the main() function. In this comprehensive guide, we will explore Global Variables in C in line with the GATE Syllabus for CSE (Computer Science Engineering) .
🌐
PrepBytes
prepbytes.com › home › c programming › global variable in c
Global Variable in C
January 8, 2024 - Example of a Global Variable in C in which we Declaring a Global Variable in C A global variable can be declared by placing the variable outside of any function but within the scope of the entire program.
🌐
Emory
cs.emory.edu › ~cheung › Courses › 255 › Syllabus › 1-C-intro › cprep2a.html
global variables
Example: Even though the · global variable x is · defined and declared in the · program file p1.c, there is · no error as long as the · declaration and definition do · not conflict. The same is · true for the · global variable y in the · program file p2.c ·
🌐
Scaler
scaler.com › home › topics › what is global variable in c?
What is Global Variable in C? - Scaler Topics
May 21, 2024 - Some examples of the use of global variables are the implementation of singleton patterns and accessing registers in embedded systems. Global variables can also store constant values like pi in a program, or if a variable is being used by many ...
🌐
Dot Net Tutorials
dotnettutorials.net › home › local vs global variables in c
Local Vs Global Variables in C Language - Dot Net Tutorials
December 2, 2023 - In the C programming language, global variables are declared outside of any function. These variables are accessible from any function within the same file or other files if the global variable is declared with the extern keyword.
🌐
Javatpoint
javatpoint.com › global-variable-in-c
Global Variable in C - javatpoint
Global Variable in C with Tutorial, C language with programming examples for beginners and professionals covering concepts, c pointers, c structures, c union, c strings etc.
🌐
Polytechnique
lix.polytechnique.fr › ~liberti › public › computing › prog › c › C › SYNTAX › glo_int_vars.html
Global and Local variables declaration. - LIX
C++ has enhanced the use of global variables. Global variables ARE initalised by the system when you define them! In the next example i is a global variable, it can be seen and modified by main and any other functions that may reference it.
🌐
LabEx
labex.io › tutorials › c-how-to-declare-global-variables-correctly-418762
How to declare global variables correctly | LabEx
graph TD A[Global Variables] --> ... int globalValue = 100; void demonstrateGlobalVariable() { printf("Global value inside function: %d\n", globalValue); globalValue += 50; } int main() { printf("Initial global value: %d\n", globalValue); demonstrateGlobalVariable(); printf("Modified global value: %d\n", globalValue); return 0; }...
🌐
Rip Tutorial
riptutorial.com › using a global variable
C Language Tutorial => Using a Global Variable
#ifndef GLOBAL_DOT_H /* This is an "include guard" */ #define GLOBAL_DOT_H /** * This tells the compiler that g_myglobal exists somewhere. * Without "extern", this would create a new variable named * g_myglobal in _every file_ that included it. Don't miss this! */ extern int g_myglobal; /* _Declare_ g_myglobal, that is promise it will be _defined_ by * some module.