Programming

How the main() function is called in the C/C++ language ?

How the main() function is called in the C/C++ language, www.codiens.com

How the main() function is called in the C/C++ language ?

The main() function or complete C program is treated as a normal procedure/ process. Let us understand this more clearly

#include
#include
int main()
{
	printf("Hello Codiens");
	return 0;
}

When we invoke the compiler to compile the source code, it will start compilation from the main() function and return assembly code. You can get assembly code by  executing this command gcc -S filename.c or gcc -S -o filename.asm filename.c in your terminal.

You can read more about C compilation to understand it completely.

.file	"main.c"
	.def	___main;	.scl	2;	.type	32;	.endef
	.text
LC0:
	.ascii "Hello world!\12\0"
.globl _main
	.def	_main;	.scl	2;	.type	32;	.endef
_main:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$8, %esp
	andl	$-16, %esp
	movl	$0, %eax
	movl	%eax, -4(%ebp)
	movl	-4(%ebp), %eax
	call	__alloca
	call	___main
	movl	$LC0, (%esp)
	call	_printf
	movl	$0, %eax
	leave
	ret
	.def	_printf;	.scl	2;	.type	32;	.endef

Then further the assembler converts the above assembly code to the machine code. At this phase, only concrete code is converted into machine language, the library functions like scanf() and printf() are not resolved yet.

MZ       ÿÿ  ¸       @                                      º ´	Í!¸LÍ!This program cannot be run in DOS mode.
 
$       PE  L ûY_   ¢  à 
8 
                    @                     P     ·=                                  @  `                                                                                                                  .text         
                 `  `.data   p                         @  À.bss    p    0                        À.idata  `   @                    @  À                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        U‰åƒìÇ$   ÿÄ@@ èh   ‰ì1À]ÉöU‰åƒìÇ$   ÿÄ@@ èH   ‰ì1À]ÉöU‰åƒì‹U‰$ÿÜ@@ ‰ì]Ív ¼'    U‰åƒì‹U‰$ÿÐ@@ ‰ì]Ív ¼'    U‰åSƒì$Ç$€@ è}  ƒìèe  Ç$  @  @ MøÇEø    L$‰TUô‰TD$ @ èò  ¡  @ …Àt^£0 @ ‹Ì@@ …ÒtD$‹Z‰$è»  ‹Ì@@ ƒúàt  @ ‰\$‹J0‰$è›  ‹Ì@@ ƒúÀt  @ ‰\$‹JP‰$è{  èf  
0 @ ‰è‰  è4    @ ‹‹
@ ‰$‰\$‰L3  ‰Ãèÿ  ‰$è§  ´&    U‰åƒì‰]ø‹M1Û‰uü1ö‹‹=  À‡¾   =  Àsg=  Àt‰Ø‹uü‹]ø‰ì]  ÇD$    Ç$
   è¾  ƒøt"…ÀtÕÇ$
   ´&    ¼'    ÿлÿÿÿÿë·ÇD$   Ç$
   èƒ  ë㐾   ÇD$    Ç$   èg  ƒøt…À„zÿÿÿÇ$   ë±ÇD$   Ç$   è=  …öt›è¤   딉ö=  Àt©=  Àt§é=ÿÿÿU‰å]éG  Hello world! U‰åƒìƒäð¸    è€  èë   Ç$@ è  ¸    ‰ì]АU¹ 0@ ù 0@ ‰åsQ‹ƒÁ‚  @ ù 0@ rê]АU‰åÛã]АU‰åƒì¡D @ ‹…Òu‰ì]Ãÿ‹D @ JB£D @ …Éuéëã‰öU‰åƒì‰]ü¡p@ ƒøÿt)…À‰ÃuÇ[email protected] èÛüÿÿ‹]ü‰ì]Ít& ÿp@ KuöëÝ‹
t@ 1À…Ét˺p@ v @‹L‚…Éu÷븐t& U‰åƒì‰]ü‹@ @ …Òup@ º   ‰@ @ ƒøÿt%…À‰ÃuÇ[email protected] èfüÿÿ‹]ü‰ì]Ãÿp@ Kuöëá‹
t@ 1À…ÉtϺp@ ‰ö@‹\‚…Ûu÷뽐-LIBGCCW32-EH-SJLJ-GTHR-MINGW32 U‰åƒì¡00@ ÿP‰ì]Í´&    ¼'    U1À‰åüƒì¹   ‰$‹]|$‰ßó«Ç8   ¡ 0@ d @ ÇC @ C¡` @ SÇC,ÿÿÿÿ‰C¡0@ ÇC0@ ÇC$    C(¡h @ l @ C0S4‹$‹|$‰ì]Ѝt& U‰åƒìX]ø¡00@ uü…Àt
]ø‹uü‰ì]ÐÇE¸AAAA¡@ u¸ÇE¼AAAAÇEÀAAAAEØ¡@ ÇEÄAAAAÇEÈAAAAEÜ¡@ ÇEÌAAAAÇEÐAAAAEà¡@ ÇEÔAAAA4$‰Eä¡ @ Eè¡[email protected] Eì¡(@ Eð¡,@ Eôè½  ·Àƒìf…ÀubÇ$8   èf  …À‰Ãt\‰$è¸þÿÿ‰$è`   f…Àt00@ C£ 0@ C£@0@ é%ÿÿÿ‰$è  4$è`  ƒì·Àv ¼'    ‰$èÈ   ‰Ãë¹è  ë
U¸   ‰åVº   SƒìPut& ¼'    …ò±Au±aˆL¸ÒHyï¡@ EØ¡@ EÜ¡@ Eà¡@ Eä¡ @ Eè¡[email protected] Eì¡(@ Eð¡,@ EôE¸‰$èÄ  ·Øƒìf…Ûu
1ҍeø‰Ð[^]É$è   9ð‰Úuçë獴&    ¼'    U‰åU¸SƒìT1Û·EÇD[email protected]   T$‰$è|  ƒì…Àt1¸   º   t& ¼'    |¸AtÒHyôƒ;8u
‰Ø‹]üÉÃ	Óëëèï   Q‰áƒÁ=   ré   ƒ	 -   ëé)Áƒ	 ‰à‰Ì‹‹@ÿàÿ%Ä@@         ÿ%È@@         ÿ%¼@@         ÿ%ì@@         ÿ%À@@         ÿ%Ô@@         ÿ%¸@@         ÿ%è@@         ÿ%à@@         ÿ%ä@@         ÿ%Ø@@         ÿ%¬@@         ÿ% @@         ÿ%¤@@         ÿ%œ@@         ÿ%¨@@         U‰å]éúÿÿÿÿÿÿ`@     ÿÿÿÿ                                                                                                                                                                                                                                                                                                                                                                                                                ÿÿÿÿ                             @                  €@                             ÿÿÿÿ    ÿÿÿÿ                                                                                                                                                                                                                                                                                                                                                                                                                @@          B  œ@  \@          TB  ¸@                          ô@   A  A  A  ,A          LA  \A  lA  |A  A  œA  ¤A  °A  ¼A  ÄA  ÐA  ØA  äA  ìA          ô@   A  A  A  ,A          LA  \A  lA  |A  A  œA  ¤A  °A  ¼A  ÄA  ÐA  ØA  äA  ìA       AddAtomA   ExitProcess   ¯ FindAtomA Ü GetAtomNameA  ßSetUnhandledExceptionFilter   ' __getmainargs < __p__environ  > __p__fmode    P __set_app_type    y _cexit    é _iob  ^_onexit   _setmode  abort atexit    ?free  rmalloc    puts  signal     @   @   @   @   @  KERNEL32.dll    @  @  @  @  @  @  @  @  @  @  @  @  @  @  msvcrt.dll                                                                                                                                                                                                                                                                                                                                                                                                                                  .file      þÿ  gcrt1.c                                                           _atexit @       __onexit`           '              ;         .text          s  #             .data                           .bss                             .file      þÿ  gcrtstuff.c            U                          .text        	                .data                           .bss                             .file   "   þÿ  gmain.c            _main         .text        :                .data                           .bss                             .file   *   þÿ  gCRTglob.c         .text   Ð                       .data                          .bss                             .file   2   þÿ  gCRTfmode.c        .text   Ð                       .data                           .bss                             .file   :   þÿ  gtxtmode.c         .text   Ð                       .data   0                       .bss                             .file   D   þÿ  gpseudo-reloc.c        e   Ð                       .text   Ð     (                .data   @                        .bss                             .file   N   þÿ  gCRT_fp10.c            €                           .text                          .data   @                        .bss                             .file   \   þÿ  ggccmain.c             Š   @       _p.0    D           —                              ª   @      ___main        .text        û                .data   @                      .bss                             .file   å   þÿ  g    ½             .text                          .data   P                       .bss                                 Ñ             â   0                                           `           &             4  h           @  P          \  Ð          x               °      .text          %             .data   `                       .bss                             probe   6      done    M      .text   0     -                 .data   p                        .bss                             .text   `                       .data   p                        .bss                             .text   `      .data   p       .bss            .idata$7(      .idata$5Ä       .idata$4h       .idata$6|      .text   p      .data   p       .bss            [email protected]      .idata$5Ü       .idata$4€       .idata$6Ä      .text   p      .data   p       .bss            .idata$74      .idata$5Ð       .idata$4t       .idata$6¤      .text   p      .data   p       .bss            .idata$70      .idata$5Ì       .idata$4p       .idata$6œ      .text   p      .data   p       .bss            .idata$7,      .idata$5È       .idata$4l       .idata$6      .text   €      .data   p       .bss            .idata$7       .idata$5¼       .idata$4`       .idata$6\      .text         .data   p       .bss            .idata$7P      .idata$5ì       .idata$4       .idata$6ì      .text          .data   p       .bss            .idata$7$      .idata$5À       .idata$4d       .idata$6l      .text   °      .data   p       .bss            .idata$78      .idata$5Ô       .idata$4x       .idata$6°      .text   À      .data   p       .bss            .idata$7      .idata$5¸       .idata$4\       .idata$6L      .text   Ð      .data   p       .bss            .idata$7L      .idata$5è       .idata$4Œ       .idata$6ä      .text   à      .data   p       .bss            .idata$7D      .idata$5à       .idata$4„       .idata$6Ð      .text   ð      .data   p       .bss            .idata$7H      .idata$5ä       .idata$4ˆ       .idata$6Ø      .text          .data   p       .bss            .idata$7<      .idata$5Ø       .idata$4|       .idata$6¼      .file   õ   þÿ  gfake              hname   \       fthunk  ¸       .text                          .data   p                        .bss                             .idata$2                      .idata$5´                       .idata$4X                       .file   &  þÿ  gfake              .text                          .data   p                        .bss                             .idata$4”                       .idata$5ð                       .idata$7T     
                 .text         .data   p       .bss            .idata$7      .idata$5¬       .idata$4P       .idata$6,      .text          .data   p       .bss            .idata$7ü      .idata$5        .idata$4D       .idata$6       .text   0      .data   p       .bss            .idata$7       .idata$5¤       .idata$4H       .idata$6      .text   @      .data   p       .bss            .idata$7ø      .idata$5œ       .idata$4@       .idata$6ô       .text   P      .data   p       .bss            .idata$7      .idata$5¨       .idata$4L       .idata$6      .file   6  þÿ  gfake              hname   @       fthunk  œ       .text   `                       .data   p                        .bss                             .idata$2                       .idata$5˜                       .idata$4<                       .file   D  þÿ  gfake              .text   `                       .data   p                        .bss                             .idata$4T                       .idata$5°                       .idata$7     
                 .file   P  þÿ  gcrtstuff.c            ¢  `                       .text   `     	                .data   p                        .bss                             .ctors  t                     __cexit p          ´              Ó  Ô           ã              ò  |      _free   à            Ð                                    ;              V  T          j  ¤           }  Ø           Š     ÿÿ       £      ÿÿ       ½     ÿÿ       Ù  @          å  0      _puts   Ð          ï  ¼             Ì                                       @     ÿÿ       X        _fpreset           f  ì       __dll__     ÿÿ       t  è                 ÿÿ         Ü           £                 @ ÿÿ       Å     ÿÿ       Û              ù  À       __argc              
         end                   p           '  À          6  0           G  p          U  `      etext             e  p           q          __alloca0      __argv             }  p          Œ  ¨       __fmode 0           £     ÿÿ         ä           Ä     ÿÿ   __end__         _signal       _malloc ð          Ù  |          ç     ÿÿ             ÿÿ   _abort               @           *  ¸           ?              T  à           `  ¬                ÿÿ   _end                      ÿÿ       ®             ¹  °          à  œ           Õ              ê  È           ø      ÿÿ             ÿÿ       ,  Ä           B  0          O  P          `                      ˜  _mainCRTStartup _WinMainCRTStartup ___mingw_CRTStartup __gnu_exception_handler@4 ___do_sjlj_init __pei386_runtime_relocator __fpreset _initialized ___do_global_dtors ___do_global_ctors pseudo-reloc-list.c _w32_atom_suffix ___w32_sharedptr_default_unexpected _dw2_object_mutex.0 _dw2_once.1 _sjl_fc_key.2 _sjl_once.3 ___w32_eh_shared_initialize ___w32_sharedptr_initialize ___w32_sharedptr_set ___w32_sharedptr_get ___sjlj_init_ctor ___RUNTIME_PSEUDO_RELOC_LIST__ __imp___setmode __data_start__ ___DTOR_LIST__ __imp___onexit ___p__fmode _SetUnhandledExceptionFilter@4 ___w32_sharedptr_terminate __libmsvcrt_a_iname __imp__FindAtomA@4 __imp__abort __size_of_stack_commit__ __size_of_stack_reserve__ __major_subsystem_version__ _AddAtomA@4 ___chkstk __imp____p__environ __imp___iob __bss_start__ ___RUNTIME_PSEUDO_RELOC_LIST_END__ 

After this the above assorted code will go to the linker and it will attach the function calls with their function definition. And then gives us executable code which is pure machine code.

Now, when we get the executable code and run it. At this state, the operating system handles the request and the executable code will load in the main memory and then  the address of the first instruction of the executable code gets stored in the program counter (PC is an instruction address register which holds the process first instruction address).

Then the Processor first fetches the instruction from the address stored in the program counter and the fetched instruction is then decoded so that it can be well interpreted by the microprocessor. Once it is decoded, the execution takes place and the PC (program counter)  increments so that it contains the address of the next instruction of executable code.

In this section the compiler decides which will be the first instruction of the c/c++ program.