Is main really the first function or first executable statement in a C program? What if there is a global variable int a=0;
?
I have always been taught that main is the starting point of a program. But what about global variable which is assigned some value and is an executable statement in my opinion?
@KevinWallis the C standard defines the starting point for execution of a program in a "hosted" environment to be the main() function. That is where execution will start in such an environment if the program and host conform to the standard.
Executable statements specify the actions to be performed during the execution of program code. Executable statements are normally executed in the sequence they appear in the program code. They may be labeled and references to those labels may be used to alter the execution sequence.
An executable statement is a procedural step in a highlevel imperative programming language that calls for processing action by the computer, such as performing arithmetic, reading data from an external medium, making a decision, etc.
scanf ( ) functionThe scanf ( ) copies data typed at the keyboard into memory during program execution. The input list is preceded by ampersand ( &).
The global variable and in general objects of static storage duration are initialized conceptually before program execution.
C11 (N1570) 5.1.2/1 Execution environments:
All objects with static storage duration shall be initialized (set to their initial values) before program startup.
Given a hosted environment, function main
is designated to be an required entry point, where program execution begins. It may be in one of two forms:
int main(void)
int main(int argc, char* argv[])
where parameters' names does not need to be the same as above (it is just a convention).
For a freestanding environment entry point is implementation-defined, that's why you can sometimes encounter void main()
or any different form in C implementations for embedded devices.
C11 (N1570) 5.1.2.1/1 Freestanding environment:
In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined.
main
is not a starting point of the program. The starting point of the program is the entry point of the program, which is in most cases is transparent for a C programmer. Usually it is denoted by _start symbol, and defined in a startup code written in assembly or precompiled into a C runtime initialization library (like crt0.o
). It is responsible for low-level initialization of stuff you are taking as given, like initializing the uninitialized static variables to zeros. After it is done, it is calling to a predefined symbol main
, which is the main
you know.
But what about global variable which is assigned some value and is an execuatable statement in my opinion
Your opinion is wrong.
In a global context, only a variable definition can exist, with an explicit initialization. All the executable statements (i.e, the assignment) have to reside inside a function.
To elaborate, in global context, you cannot have a statement like
int globalVar;
globalVar = 0; //error, assignement statement should be inside a function
however, the above would be perfectly valid inside a function, like
int main()
{
int localVar;
localVar = 0; //assignment is valid here.
Regarding the initialization, like
int globalVar = 0;
the initialization takes place before start of main()
, so that's not really the part of execution, per se.
To elaborate the scenario of the initialization of a global variable, quoting the C11
, chapter 6.2,
If the declarator or type specifier that declares the identifier appears outside of any block or list of parameters, the identifier has file scope, which terminates at the end of the translation unit.
and for flie scope variables,
If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is external.
and for objects with external linkage,
An object whose identifier is declared without the storage-class specifier
_Thread_local
, and either with external or internal linkage or with the storage-class specifierstatic
, has static storage duration. Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With