For example abc.c contains a variable
#define NAME "supreeth"
Can extern the variable NAME
in def.c?
The extern keyword in C and C++ extends the visibility of variables and functions across multiple source files. In the case of functions, the extern keyword is used implicitly. But with variables, you have to use the keyword explicitly.
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.
“extern” keyword is used to extend the visibility of function or variable. By default the functions are visible throughout the program, there is no need to declare or define extern functions. It just increase the redundancy. Variables with “extern” keyword are only declared not defined.
Extern is a keyword in C programming language which is used to declare a global variable that is a variable without any memory assigned to it. It is used to declare variables and functions in header files. Extern can be used access variables across C files.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With