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
Code:
#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
Code:
#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?