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
🌐
GeeksforGeeks
geeksforgeeks.org › c language › global-variables-in-c
Global Variables in C - GeeksforGeeks
In a programming language, each variable has a particular scope attached to them. The scope is either local or global. This article will go through global variables, their advantages, and their properties. The Declaration of a global variable is very similar to that of a local variable. 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
🌐
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!
People also ask

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)
Can global variables be constant in C?
Yes, you can declare a global variable as const to prevent modification, like const int MAX_LIMIT = 100;.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › global-variables
Global Variables in C Programming (With Examples)
Where are Global Variables Stored in C?
Global variables in C are stored in the data segment of memory and remain available throughout the program’s execution.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › global-variables
Global Variables in C Programming (With Examples)
🌐
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.
🌐
Hero Vired
herovired.com › learning-hub › topics › global-variable-in-c
Global Variable in C Programming: Examples, How to Declare
How to Declare and Initialise the Global Variable in CUnderstanding the Scope and Lifetime of the Global Variable in CAccessing and Modifying Global Variables Across Multiple FunctionsRedeclaring and Using the extern Keyword with Global VariablesBest Practices for Using Global Variable in C ProgrammingCommon Mistakes to Avoid When Working with Global VariablesReal World Examples of Global Variable in CExample 2: Storing Configuration SettingsExample 3: Sharing Data Between FunctionsConclusionFAQs
🌐
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 ·
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

🌐
Wikipedia
en.wikipedia.org › wiki › Global_variable
Global variable - Wikipedia
January 5, 2026 - External linkage, however, is not sufficient for such a variable's use in other files: for a compilation unit to correctly access such a global variable, it will need to know its type. This is accomplished by declaring the variable in each file using the extern keyword. (It will be declared in each file but may be defined in only one.) Such extern declarations are often placed in a shared header file, since it is common practice for all .c files in a project to include at least one .h file: the standard header file errno.h is an example, making the errno variable accessible to all modules in a project.
Find elsewhere
🌐
Reddit
reddit.com › r/c_programming › most elegant / best practice for global variable
r/C_Programming on Reddit: Most elegant / best practice for global variable
July 5, 2023 -

Hi,

I have been working as a hobbyist on a multi threaded C program where I have usage of global variables: essentially variables that control the verbosity of error messages, some initial conditions that can be set up (and never changed thereafter etc).

I was wondering on how is the best way to implement that, I have used the following approaches:

  • declare them as extern in a header included by all modules who need those variables.

  • group them in a struct and declare that struct as extern

  • group them in a struct and pass a pointer to that struct to all functions that need those variables.

The latter solution seems cleaner, but is quite heavy to implement, as basically you need to pass the pointer reference to many functions.

Now, since global variables are usually frowned upon but less so for variables like the above ones (unlikely to be changed, used as read-only, etc.) I was thinking what is the approach you guys recommend or use in your code?

Thanks,

🌐
Scaler
scaler.com › home › topics › what is global variable in c?
What is Global Variable in C? - Scaler Topics
May 21, 2024 - Then, we incremented the value of pi and printed the updated value, i.e. 4.140000. Finally, we called the square() function. Because the new value of pi was 4.14, the square() function squared 4.14 and we got 17.139603 as the new value of pi. Example 3: Redefining a global variable.
🌐
Testbook
testbook.com › home › gate › global variable in c: usage, examples, redefinition & practice problems
Global Variable in C: Usage, Examples, Redefinition & Practice Problems
This means that any function anywhere in a program can access the variable, eliminating the need to pass or return it from a function. To understand how to declare and use a global variable, consider the example of your age. The age and float global variables can be affected by both functions.
🌐
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 ...
🌐
TutorialsPoint
tutorialspoint.com › home › cprogramming › c programming scope rules
C Programming Scope Rules
June 10, 2012 - A program can have same name for local and global variables but the value of local variable inside a function will take preference. Here is an example −
🌐
PrepBytes
prepbytes.com › home › c programming › global variable in c
Global Variable in C
January 8, 2024 - Global variables can also be used to store constant values like pi in a program, or if a variable is used by multiple functions, we can declare it as global. 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.
🌐
Code Quoi
codequoi.com › en › local-global-static-variables-in-c
Local, Global and Static Variables in C - codequoi
If we want to use a global variable defined in one file in another file, all we need to do is declare it once again with the extern keyword. This usually implicit keyword tells the compiler that we are declaring something that we are defining elsewhere in the program files. Let’s take our initial example and separate our two functions, main and foo, into two separate files:
🌐
University of Texas
farside.ph.utexas.edu › teaching › 329 › lectures › node19.html
Global variables
Hence, the natural place to put global variable declaration statements is before any function definitions: i.e., right at the beginning of the program. Global variables declarations can be used to initialize such variables, in the regular manner. However, the initial values must be expressed as constants, rather than expressions.
Top answer
1 of 16
67

Variables should always have as small a scope as possible. The argument behind this is that every time you increase the scope, you have more code that potentially modifies the variable, thus more complexity is induced in the solution.

It is thus clear that avoiding using global variables is preferred if the design and implementation naturally allow that. Due to this, I prefer not to use global variables unless they are really needed.

I can not agree with the 'never' statement either. Like any other concept, global variables are something that should be used only when needed. I would rather use global variables than using some artificial constructs (like passing pointers around), which would only mask the real intent.

Some good examples where global variables are used are singleton pattern implementations or register access in embedded systems.

On how to actually detect excessive usages of global variables: inspection, inspection, inspection. Whenever I see a global variable I have to ask myself: Is that REALLY needed at a global scope?

2 of 16
22

The only way you can make global variables work is to give them names that assure they're unique.

That name usually has a prefix associated some some "module" or collection of functions for which the global variable is particularly focused or meaningful.

This means that the variable "belongs" to those functions -- it's part of them. Indeed, the global can usually be "wrapped" with a little function that goes along with the other functions -- in the same .h file same name prefix.

Bonus.

When you do that, suddenly, it isn't really global any more. It's now part of some module of related functions.

This can always be done. With a little thinking every formerly global variable can be assigned to some collection of functions, allocated to a specific .h file, and isolated with functions that allow you to change the variable without breaking anything.

Rather than say "never use global variables", you can say "assign the global variable's responsibilities to some module where it makes the most sense."

🌐
YouTube
youtube.com › watch
C Programming Tutorial - 55 - Global vs Local Variables - YouTube
AboutPressCopyrightContact usCreatorsAdvertiseDevelopersTermsPrivacyPolicy & SafetyHow YouTube worksTest new featuresNFL Sunday Ticket · © 2026 Google LLC
Published   August 23, 2014
🌐
Emory
cs.emory.edu › ~cheung › Courses › 255 › Syllabus › 1-C-intro › cprep3.html
How to organize the global variables in a multi-files C ...
A programming trick to manage definitions/declarations of global variables used in multiple C program files ... Example Program: (Demo above code) &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
🌐
Reddit
reddit.com › r/c_programming › still not sure when to use global variables
r/C_Programming on Reddit: Still not sure when to use global variables
February 17, 2022 -

I did the Advent of Code 2021 in C and I'm reviewing my code from it. In the beginning I wasn't using global variables because i didn't need to, but later I had to manage arrays of pointers and because they were always being passed as arguments to other functions, I decided to make them global. Most functions were receiving and returning void because of that.

Then I got into this mindset of "never use global variables" and I was able to make programs without any global variable, but the functions would have lots of parameters, many variables were passed all the time, and I had to use triple pointers for that. I personally think that's even more unsafe and unreadable than just using a global variable, but everyone always says to not use them. I'm trying to refactor the code and I don't know whether I should use or not global variables.

I know the readability and the code itself are bad, maybe I could've used some known algorithms... I'm just using this one as an example: Day 12

🌐
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.