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.

7
  • It's stored in each object file's DATA section. Commented Sep 20, 2021 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. Commented Sep 20, 2021 at 21:20
  • Why do you think it's not a global variable? Each source file has its own set of global variables. Commented Sep 20, 2021 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? Commented Sep 20, 2021 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;. Commented Sep 20, 2021 at 21:29

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.