Linkers, Loaders & Executables

In C, what does writing "extern int count;" mean?

  1. AIt declares that a variable count is defined somewhere else and reserves no storage here
  2. BIt creates a new variable count initialized to 0
  3. CIt hides the variable count from outside this file
Show answer and explanation

Answer: A. It declares that a variable count is defined somewhere else and reserves no storage here

extern declares "a variable with this name is defined in another file" and reserves no memory. The compiler emits instructions referring to count without knowing its address and records that place in the relocation entries. The linker binds it to a definition such as "int count = 0;" in another file and fixes the address. The usual practice is to put extern declarations in a header and the definition in exactly one source file.

  • A. Correct. It is only a declaration; the definition is elsewhere.
  • B. Storage is reserved by a definition such as "int count;".
  • C. Hiding from other files is static.
Practice this topic Back to topic

More Linkers, Loaders & Executables questions

  1. Which statement is correct about files in the ELF format commonly used on Linux and similar systems?
  2. What is in the .text section of an executable?
  3. In C, a global variable written outside any function as "int limit = 100;" is generally placed in which section?
  4. What is a characteristic of the .bss section?
  5. Running C code "char *p = "hello"; p[0] = 'H';" crashes in many environments. Why?