- #1
pairofstrings
- 411
- 7
Can you explain the working mechanism of extern keyword in C language? I have tried wiki and other sites but couldn't follow. Please explain in simple english.
int foo[100];
extern int foo[];
int foobar{double};
extern int foobar{double};
The "extern" keyword in C language is used to declare a variable or function that is defined in another source file. It is typically used in conjunction with the "extern" storage class specifier to indicate that the variable or function is defined externally.
The "extern" keyword should be used when a variable or function needs to be accessed from multiple source files in a C program. By using "extern", the variable or function can be declared in one source file and then referenced in other source files without having to redefine it.
No, the "extern" keyword cannot be used with local variables. It is meant to be used with global variables or functions that are defined in other source files. Local variables are only accessible within the scope of the function they are declared in.
Not necessarily. Including header files is one way to make variables and functions accessible in multiple source files without using the "extern" keyword. However, if the header file only contains declarations and not definitions, then the "extern" keyword would be needed when defining the variables or functions.
Yes, the "extern" keyword can be used in both C and C++ languages. However, it is more commonly used in C since C++ has other mechanisms such as namespaces to achieve similar functionality.