3

Hi I am a C++ developer now I am doing C programming.

My question is which place is better to declare global variable in c program. Header or source file (provided my global variable is not used in other files)?

I want that variable like private variable in C++.

Liju Thomas
  • 1,054
  • 5
  • 18
  • 25

6 Answers6

4

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

shafeeq
  • 1,499
  • 1
  • 14
  • 30
4

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

Community
  • 1
  • 1
jdarthenay
  • 3,062
  • 1
  • 15
  • 20
2

If you intend to use the global variables in multiple .c files, it is better to declare them in .h files. However, if you want to keep the variables like private member data of classes in C++, it will be better to provide access to the global data through functions.

Instead of

extern int foo;

Use

int getFoo();
void setFoo(int);

That sort of mimics the private access specifiers for member variables of classes.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • @ R sahu. Assume in my header test.h I declared a variable `extern int a' and modified the value of `a` in test.c . and the same header I included in test2.c and if I try to use `a` in test2.c will my `a` is stored with modified value or fresh `a` – Bhavith C Acharya Apr 15 '16 at 04:52
2

You should not place global non-constant variables anywhere. Global as in declared with extern and available to your whole project. The need to do this always originates from bad program design, period. This is true for C and C++ both.

The exception is const variables, which are perfectly fine to share across multiple files.

In the case you need file scope variables, they should be declared in the .c file and always as static. Don't confuse these for "globals" because they are local to the translation unit where they are declared. More info about how static file scope variables can make sense.

Also note the C standard future language directions:

Declaring an identifier with internal linkage at file scope without the static storage class specifier is an obsolescent feature.

So if you don't use static your code might not compile in the next version of the C standard.

Community
  • 1
  • 1
Lundin
  • 195,001
  • 40
  • 254
  • 396
0

Generally what you can do is define the variable in a source file, like int g_foo;, then reference this global in other files with extern, like extern int g_foo; do_sth(g_foo);. You could put the extern int g_foo; declaration in a header file, and include that in other source files. It's not recommend to have definitions of data in header files.

fluter
  • 13,238
  • 8
  • 62
  • 100
0

If you want it to be global (external linkage), you should put it in .h file. And this is one of the best practise, I think:

public_header.h

#ifdef YOUR_SOURCE
 #define EXTERN 
#else
 #define EXTERN extern
#endif

EXTERN int global_var;

your_source.c

//your source makes definition for global_var
#define YOUR_SOURCE 
#include <public_header.h>

other_source.c

#include <public_header.h> //other sources make declaration for global_var

If you want it to be private (internal linkage), the best solution, I think, is just make definition of it right in your source file instead of header file to prevent the header file is included by another source and then make confuse.

your_souce.c

static int private_var;
Van Tr
  • 5,889
  • 2
  • 20
  • 44
  • why should it be static in source.c why can't be non static is there any reason ? . Is it because to make it local to that file ? – Bhavith C Acharya Apr 15 '16 at 05:07
  • yes, like I mention, if you want it to be private (cannot be seen from other sources file) so it need to be go with `static` – Van Tr Apr 15 '16 at 05:09