16

For example abc.c contains a variable

#define NAME "supreeth"

Can extern the variable NAME in def.c?

MOHAMED
  • 41,599
  • 58
  • 163
  • 268
user1295872
  • 461
  • 1
  • 6
  • 16
  • 1
    A define is a preprocessor directive. It is replaced inline when compiling the code. You will need to define it in a header (include) file. – woodleg.as Apr 29 '13 at 13:35

3 Answers3

23

You can not use extern with macro. but if you want your macro seen by many C files

put your macro definition

#define NAME "supreeth"

in a header file like def.h

then include your def.h in your C code and then you can use your macro in your C file in all other C file if you include def.h

MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • What's possible? There is no variable, and the concept of `extern` doesn't apply. – Keith Thompson Apr 29 '13 at 14:45
  • I did not mean using extern I ean he can define his macro in the header file then he can see it from his C files . may be I have to be more specific I will update my answer – MOHAMED Apr 29 '13 at 14:47
16

In your code NAME is not a variable. It's a pre-processor symbol, which means the text NAME will be replaced everywhere in the input with the string "supreeth". This happens per-file, so it doesn't make sense to talk about it being "external".

If a particular C file is compiled without that #define, any use of NAME will remain as-is.

unwind
  • 391,730
  • 64
  • 469
  • 606
3

If you have #define NAME "supreeth" in abc.c, you can surely have a extern variable by same name in another file def.c, this is as far as the compiler is concerned. If you are implying some kind of dependency between these two, that dependency/linkage will not happen. Obviously it is confusing and a bad idea to do something like this.

Sudhee
  • 704
  • 6
  • 12