Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring global variables in the header file or the C source file

Tags:

c

header-files

I am kind of confused about the whole including header files and declaration of variables.

Files I am using are: main.c, lib.h and lib.c.

main.c

#include "lib.h"

void main(void)
{
    // Code here
    var++;
}

lib.c

#include <avr/io.h>
#include "lib.h"

void light_led(void)
{
    // Code here
}

lib.h

volatile int var;

void light_led(void);

Is this the correct way of making and including your own custom-made libraries?

like image 808
Ankit Avatar asked Jun 17 '14 10:06

Ankit


People also ask

Can we declare global variable in header file C?

ANSWER. Yes. Although this is not necessarily recommended, it can be easily accomplished with the correct set of macros and a header file. Typically, you should declare variables in C files and create extern definitions for them in header files.

Do you put global variables in header file?

The clean, reliable way to declare and define global variables is to use a header file to contain an extern declaration of the variable. The header is included by the one source file that defines the variable and by all the source files that reference the variable.

Where do you declare global variables in C?

Of course, a global variable can only be used in an executable statement after it has been declared. Hence, the natural place to put global variable declaration statements is before any function definitions: i.e., right at the beginning of the program.

Where should I declare global variables?

The keyword 'Global' is also used to create or declare a global variable inside a function. Usually, when you create a variable inside a function (a local variable), it can only be used within that function.


1 Answers

You should use extern keyword for such cases and not define global variables in headers. Otherwise the linker will throw errors when operating on your header files.

lib.c

#include <avr/io.h>
#include "lib.h"

volatile int var;

void light_led(void)
{
//code here
}

lib.h

extern volatile int var;

void light_led(void);

This way you'll be declaring the global variable in headers and actually defining it in the appropriate source file lib.c.

Note: Notice the difference between declaring and defining a variable. extern keyword allows the variable to be declared in advance without being defined. Had you not defined the variable in lib.c, there would be an error when you tried to use this variable. Since, it is only declared but, not actually defined.

Edit: The whole purpose of static is to declare that a variable is private to the source file that is declared in. Since, extern does the opposite by linking a variable defined in another source file, it defeats the purpose of static. extern says the variable has external linkage static says the variable has internal linkage. An identifier can't have both internal and external linkage.

According to MSND:

When modifying a variable, the static keyword specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends) and initializes it to 0 unless another value is specified. When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared).

For more information check below:

  • Understanding "extern" keyword in C
  • Why we need "extern keyword in C
like image 119
Tamer Tas Avatar answered Oct 14 '22 06:10

Tamer Tas