Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First executable statement in C

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?

like image 388
Vrajendra Avatar asked Jun 02 '16 13:06

Vrajendra


People also ask

What is the first executable statement in C?

@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.

What are executable statements in C?

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.

What is a executable statement?

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.

Is Scanf is an executable in C?

scanf ( ) functionThe scanf ( ) copies data typed at the keyboard into memory during program execution. The input list is preceded by ampersand ( &).


3 Answers

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.

like image 162
Grzegorz Szpetkowski Avatar answered Sep 19 '22 11:09

Grzegorz Szpetkowski


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.

like image 41
Eugene Sh. Avatar answered Sep 20 '22 11:09

Eugene Sh.


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 specifier static, 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.

like image 36
Sourav Ghosh Avatar answered Sep 22 '22 11:09

Sourav Ghosh