global variable to be initialized

Thread Starter

Kittu20

Joined Oct 12, 2022
511
I have a question about global variable initialization that's been causing me some confusion.

I understand that in C programs, the execution starts from the main function. However, I'm unsure about the difference between initializing a global variable within main versus initializing it before main starts.

Scenario 1: Declaring and Assigning Global Variable in Main

C:
#include <stdio.h>

// Declaration of global variable
int x;

int main() {
    // Assigning a value to the global variable within main
    x = 10;

    // Using the global variable
    printf("The value of x is: %d\n", x);

    return 0;
}
Scenario 2: Declaring and Assigning Global Variable Before Main
C:
#include <stdio.h>

// Declaration and assignment of global variable before main
int x = 20;

int main() {
    // Using the global variable
    printf("The value of x is: %d\n", x);

    return 0;
}
Could you clarify the following points for me?

How does the program flow differ when a global variable is assigned a value within main versus being initialized before main starts?
 

MrChips

Joined Oct 2, 2009
34,632
Your program execution begins with main().

However, when you declare a global variable to be initialized, the compiler creates an initialization function that is executed before executing main().
 

ApacheKid

Joined Jan 12, 2015
1,762
I have a question about global variable initialization that's been causing me some confusion.

I understand that in C programs, the execution starts from the main function. However, I'm unsure about the difference between initializing a global variable within main versus initializing it before main starts.

Scenario 1: Declaring and Assigning Global Variable in Main

C:
#include <stdio.h>

// Declaration of global variable
int x;

int main() {
    // Assigning a value to the global variable within main
    x = 10;

    // Using the global variable
    printf("The value of x is: %d\n", x);

    return 0;
}
Scenario 2: Declaring and Assigning Global Variable Before Main
C:
#include <stdio.h>

// Declaration and assignment of global variable before main
int x = 20;

int main() {
    // Using the global variable
    printf("The value of x is: %d\n", x);

    return 0;
}
Could you clarify the following points for me?

How does the program flow differ when a global variable is assigned a value within main versus being initialized before main starts?
The former example leads to the memory that contains the variable, being initialized prior to loading, this could be done by the linker. If you were to examine the generated file (OBJ, or whatever, you could likely see the storage with its initialized values.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
511
In general, the compilation process involves four steps: preprocessing, compilation, assembly, and linking. My question is regarding the linking step.

Does the linker assign memory locations and assign values for global variables x before to execute main in my program ?

C:
#include <stdio.h>

// Declaration and assignment of global variable before main
int x = 20;

int main() {
    // Using the global variable
    printf("The value of x is: %d\n", x);

    return 0;
}
 

MrChips

Joined Oct 2, 2009
34,632
No. Neither the compiler nor linker can initialize a variable. They can only assign the address of the variable.

It is conceivable that the linker/loader can physically load a value into a specific memory location but I don't believe that is how the memory location is initialized. Initialization code is executed before the main( ) function is called.

It would be possible to find the initialization code and see what it does.
 

ApacheKid

Joined Jan 12, 2015
1,762
No. Neither the compiler nor linker can initialize a variable. They can only assign the address of the variable.

It is conceivable that the linker/loader can physically load a value into a specific memory location but I don't believe that is how the memory location is initialized. Initialization code is executed before the main( ) function is called.

It would be possible to find the initialization code and see what it does.
Go and read about the .data sections in ELF files, here I'll save you the effort:

1714319292318.png
Hardly "inconceivable" Mr Chips.

Or how about this:

1714319486619.png

Understand, the global variable is NOT initialized at runtime, that's the take away here.
 

Attachments

WBahn

Joined Mar 31, 2012
32,713
I have a question about global variable initialization that's been causing me some confusion.

I understand that in C programs, the execution starts from the main function. However, I'm unsure about the difference between initializing a global variable within main versus initializing it before main starts.

Scenario 1: Declaring and Assigning Global Variable in Main

C:
#include <stdio.h>

// Declaration of global variable
int x;

int main() {
    // Assigning a value to the global variable within main
    x = 10;

    // Using the global variable
    printf("The value of x is: %d\n", x);

    return 0;
}
Scenario 2: Declaring and Assigning Global Variable Before Main
C:
#include <stdio.h>

// Declaration and assignment of global variable before main
int x = 20;

int main() {
    // Using the global variable
    printf("The value of x is: %d\n", x);

    return 0;
}
Could you clarify the following points for me?

How does the program flow differ when a global variable is assigned a value within main versus being initialized before main starts?
Your executable code and your global variables are nothing more than just bytes at memory locations, right?

Say you load a program whose executable code is 1000 bytes long. Those bytes are just copied from the file to the memory, right?

There's nothing that prevents the loader from loading another set of bytes from the file into memory and those bytes represent the initialized values of variables that aren't going to be moved during the entire execution of the program. This is where your string literals, your static variables, and your global variables would often be placed. So the compiler determines what their initial values are and puts those values into the executable file, so that as a natural part of loading the program, those variables are initialized at the same time.
 

MrChips

Joined Oct 2, 2009
34,632
@ApacheKid Whatever you say.
It depends on your definition of "runtime".

I just looked at the assembly code generated by IAR. Before main( ) is called, there are two functions called, __data16_memzero and __data16_memcpy.
 
Top