0

Assume that I have which contains three source files (.c), and three of them included "file.h", i.e at the beginning of each one of them I wrote #include "file.h" .

In the file.h itself I wrote int num=4.

I have a general launching problem. when I wrote int num, It compiled.

What is the problem? and why does the non-initializing case work?

(I'm using Eclispe)

Andy Stow Away
  • 649
  • 1
  • 8
  • 17
Numerator
  • 1,389
  • 3
  • 21
  • 40

2 Answers2

2

In C header files don't have any special semantics for the compiler, they are just text that gets expanded inline by the pre-processor. It means that your variable definition will be seen three times by the linker. To avoid confusion, the linker doesn't know which one of the three values are correct. Even though they happen to have the same value this time, the linker is dumb and doesn't know that.

If you just have "int num;", it's a special case where the variable gets allocated as a common instead of data and the linker knows to unify commons in the final linking stage. Generally, I'd say it's bad form to use commons and header files should only have "extern int foo;" while the variable itself is defined in only one linking unit.

Art
  • 19,807
  • 1
  • 34
  • 60
2

You may declare a global variable as many times as you want, but should initialize it only once, in a single translation unit. So in file.h write

extern int num;

and in some file.c write

int num = 4;

Make sure that you include the last line only in one C file; the others will use the value from that one occurrence.

MvG
  • 57,380
  • 22
  • 148
  • 276
  • and what if I don't declare it as an extern at the header? – Numerator Jul 17 '12 at 08:02
  • this is the way to define a global variable. – mihail Jul 17 '12 at 08:03
  • It seems to work even without "extern", I wonder what really happens then. – Numerator Jul 17 '12 at 08:04
  • "The extern keyword declares a variable or function and specifies that it has external linkage (its name is visible from files other than the one in which it's defined). When modifying a variable, extern specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends). The variable or function may be defined in another source file, or later in the same file. Declarations of variables and functions at file scope are external by default." - http://msdn.microsoft.com/en-us/library/0603949d(v=vs.80).aspx – mihail Jul 17 '12 at 08:06
  • Ok, and you're basically saying that if I'll write "int num" without initializing it in the other two source files, it would be perfectly fine? – Numerator Jul 17 '12 at 08:08
  • depends on what are you trying to achieve. It's like declaring the function before the definition. – mihail Jul 17 '12 at 08:12
  • If you write `int num` you tell the compiler “Please reserve some memory and associate it with the given name”. If you write `extern int num` then you tell it ”Someone will be reserving memory under that name, feel free to use it”. So without `extern`, you'll have several different memory locations with the same name, causing linkage trouble. – MvG Jul 17 '12 at 08:17