Programmer
Programmer

Reputation: 423

What is the scope of a #define'd variable?

I am trying to use a preprocessor variable I declared in main.c in another C source file in the same project but I get a compile time error, included from main.c: undefined symbol 'mysymbol'. Now, what are the rules concerning the use of #define out side the file in which it is contained?

Upvotes: 1

Views: 172

Answers (2)

Colin Valliant
Colin Valliant

Reputation: 1929

You need to put the #define in a header, and #include it in all files where you want to reference it.

Upvotes: 3

cnicutar
cnicutar

Reputation: 182619

Now, what are the rules concerning the use of #define out side the file in which it is contained

A #define is not visible outside its file. The only way to make it visible outside the file where it is defined is to include that file.

So it looks like you should put it in a header included by both main.c and that other file.

Upvotes: 9

Related Questions