0

From this good answer , the best way to declare global variables in C is to declare the variable in 1 header using extern, and define it in 1 source only. Then reference the header in other source files wanting to use the global variable.

So I did that, in my header global.h:

extern Boolean transmitting;

Inside my main.c, I defined it inside my main function:

int main() {
    Boolean transmitting = FALSE;
...
}

However, I get a warning on the variable in the main "unused variable 'transmitting' [-Wunused-variable]" which tells me this is only defined in the scope of the main, so other source files cant use it? Does this mean I need to define them outside main()?

Previoulsy, I was not using the extern keyword to declare in my header and used the variable in my main like this:

int main() {
    transmitting = FALSE;
...
}

Everything worked fine and other source files had access to the variable by including this header. However I want to use the correct method of declaring global variables. Where did I go wrong?

Sarah cartenz
  • 1,313
  • 2
  • 14
  • 37
  • 1
    Global variables need to be defined outside of any function. Anything you define within a function (even main) is a local variable and can never be visible from outside that function – Felix G Apr 01 '20 at 07:55

2 Answers2

2

You must declare the variable at global scope, i.e. outside the main() function.

Marc Balmer
  • 1,780
  • 1
  • 11
  • 18
  • What about my previous approach? Is it really bad practice? – Sarah cartenz Apr 01 '20 at 08:06
  • 2
    Well, it simply does not work like that. Global variables must be declared at global scope. – Marc Balmer Apr 01 '20 at 08:07
  • Its still not clear to me since when I defined my global variables without extern in a header, gave them a value in the main, and so all the other sources that included this 1 header actually printed the correct value of the variable whenever a source changed it. With this behavior, it doesn't seem like I'm "defining the same variable in multiple files". It seems like these vars are really global without using extern. – Sarah cartenz Apr 01 '20 at 08:20
  • @Sarahcartenz I think what you desccribe here would be "tentative definitions" of the variable, see https://stackoverflow.com/questions/3095861/about-tentative-definition – Hulk Apr 01 '20 at 08:45
1

In your case, you declared two different variables. In main function, it's local variable that is limited in scope of main.

See the results of program below, the address of two variable are different:

#include <stdio.h>
#include <stdbool.h>
extern bool transmitting;

void print_transmitting() {
  printf("extern transmitting addr= %p\n", &transmitting);
}
int main(void) {
    bool transmitting = false;
    print_transmitting();
    printf("in main function, transmitting addr = %p\n", &transmitting);
    return 0;
}

The results:

extern transmitting addr= 0x563bc90ce011
in main function, transmitting addr = 0x7ffd251fb367

As the answer of @Marc Balmer, you must declare the variable at global scope, i.e. outside the main() function.

Hitokiri
  • 3,607
  • 1
  • 9
  • 29