0

When you have multiple C files, say main.c and process.c I was trying to understand where variables declared outside of functions in both cases are stored.

// this is main.c
#include <stdio.h>
#include "process.h"
int foo = 1;

void main() {
   int count = get_counter();
}

// this is process.c
#include <stdio.h>

int counter = 0;

int get_counter() {
    return counter;
{

So when you have two c files, your main.c and a process.c, you can call get_counter() in main.c and it will return the value from the process.c file. What I was trying to understand is where the compiler, or how it stores int foo in main.c and int count in process.c? Is this part of some data storage section? It is not on the stack right? It also seems having a separate process.c file makes it so it is not a global variable.

I have been really trying to understand how variables scope is handled and can get a little tricky for me. Does the #include "process.h" essentially compile as if you had the functions and their prototypes in the main.c above the rest of the code? To me that would make the int counter global so I know I am confusing something.

Thank you for taking your time to read this.

Steve4879
  • 61
  • 5
  • It's stored in each object file's DATA section. – Barmar Sep 20 '21 at 21:18
  • When the linker combines the object files, it's concatenates all their data sections, and fixes up the variable references to find them in the appropriate part of that. – Barmar Sep 20 '21 at 21:20
  • Why do you think it's not a global variable? Each source file has its own set of global variables. – Barmar Sep 20 '21 at 21:21
  • I might be using my terminology incorrect. The process.c was not global in respect to main.c. I needed a get_counter() in process.c to retrieve that parameter. Was not sure how the code separates data sections where each c code has their own global variables? – Steve4879 Sep 20 '21 at 21:26
  • 1
    Since `counter` in `process.c` is global, in `main.c` you could have cheated, and said `extern int counter;`, and then said `int count = counter;` directly, without using the accessor function. To make that `counter` variable private to `process.c`, and disallow `main.c` from cheating in this way, you could change its declaration to `static int counter;`. – Steve Summit Sep 20 '21 at 21:29
  • So static would allow the compiler to not allow anyone to access that variable except for the functions in *process.c*? That is a compile time catch? Either way I could not have said, in *main.c*, *count = counter* correct? – Steve4879 Sep 20 '21 at 21:37
  • See: https://stackoverflow.com/questions/66055165/advantage-of-using-extern-in-a-header-file/66056029#66056029 – Craig Estey Sep 20 '21 at 22:06

1 Answers1

1

That's a function of the executable file format, not the C language itself. For ELF (*nix and similar systems) and PE/COFF (Windows and similar), globals or other objects with static storage duration will be stored in either the .bss or .data sections depending on whether they're initialized or not. This is space allocated from within the program's binary image itself (not taken from the stack or heap).

Other executable file formats may use different section names.

John Bode
  • 119,563
  • 19
  • 122
  • 198