18.8k views
3 votes
What keyword would you use to access a global variable in another file?

a. extern
b. global
c. access
d. import

User Mapad
by
7.3k points

1 Answer

5 votes

Final answer:

The keyword 'extern' is used to access a global variable in another file, allowing linkage of the variable across multiple files in languages like C and C++.

Step-by-step explanation:

To access a global variable in another file, the keyword you would use is 'extern'. When you declare a variable in one file as extern, it indicates to the compiler that the variable is defined in another file, effectively allowing you to access its value across file boundaries. This is commonly used in C and C++ programming languages. For instance, if you have a global variable defined in 'file1.c' as int myGlobalVar; and you want to access it in 'file2.c', you would declare it in 'file2.c' using extern int myGlobalVar;. Remember to link the files during compilation to resolve the references.

User Hedfol
by
7.5k points