Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

who is in charge of calling main() function?

Posted on 1999-06-23
13
Medium Priority
?
360 Views
Last Modified: 2010-04-15
In c code can be divided into number of funcions. For invocation of the funcions it is required to give the name or address of that function explicitely by the programmer. Similarly who is calling the main() function?
It is definitely NOT by compiler--it just compiles.
0
Comment
Question by:arvindhp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +4
13 Comments
 

Author Comment

by:arvindhp
ID: 1262899
NOthing
0
 

Accepted Solution

by:
murrayc earned 100 total points
ID: 1262900
The Operating System? I guess that executables files all have an initial execution point. Maybe the compiler (the linker really) sets up the executable file appropriately.
0
 
LVL 1

Expert Comment

by:Lab_Rat
ID: 1262901
Maybe main is actually address 0 in your .exe?
Why dont you dissasemble a 'hello world' type program to see how your compiler does it.

I remember reading somewhere that an .exe needs to meet a few criteria to be accepted by windows, such as the first 2 bytes to be something like 'MZ', which decompile to a relative jump or something.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:marcjb
ID: 1262902
MZ is the file signature of an executable.

Check out

http://www.chesworth.com/pv/vault/file_format/dosexecutables.txt

for an overview of the file format.
0
 
LVL 2

Expert Comment

by:hougaard
ID: 1262903
The one calling main() is the "Runtime Startup Code" - This is a piece of code (mostly written entirely in assember) - some compiler supply the source (In my Watcom the source is located in \watcom\src\startup) ... The startup code setup stacks and other stuff.. (Getting the parameters to supply to main(...)
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1262904
the jump thingy is for COM files, and it was used mostly in the good ol' days ( i wasn't there at the time ;-)) there is nothing that calls main... main is simply the program itself in assembler... the program instructions get loaded into memory and then are being executed one at a time... etc, etc.... here is an assembler Hello World program....

a COM program...

.model tiny
.code
      org 100h
main:
  mov dx, offset msg
  mov ah, 9
  int 21h

  mov ax, 4c00h
  int 21h
.data
  msg db "hello world$"
end main

an EXE program...

.model small
.stack 100h
.data
  Message db "Hello World$"
.code
main:
  mov ax, @data
  mov ds, ax

  mov dx, offset Message
  mov ah, 9
  int 21h

  mov ax, 4c00h
  int 21h
end main

the same thing would looklike this in C

#include <stdio.h>

main()
{
  printf("Hello World");
}

Hope this helps...

..-=ViKtOr=-..
0
 
LVL 1

Expert Comment

by:Lab_Rat
ID: 1262905
Very much.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1262906
>>Very much.

what the hell do you mean?!? :)
0
 
LVL 1

Expert Comment

by:BSoeters
ID: 1262907
IMHO, Hougaard has given the most correct answer.

It is the run-time code that is supplied with your C compiler.

A program will be compiled and then linked to the run-time library. The compiler initially sets the entry point of the final executable to some run-time library function. That function will take care of all the setting up that needs to be performed.

When that function is finished, control will be passed over to main() by that function (just a call to main()).

For the sake of linking the run-time library correctly and making it possible for the compiler and linker to make the executable load and run, there has to be at least one function with a known name. That function is your supplied main().

When main() is finished (i.e. program stopped running), control is given back to the function which called your main(). That function (in the run-time library) is also responsible for calling various sorts of clean-up routines as atexit() (also with predefined names).

When all cleaning-up has been performed, control is given back to the operating system.
0
 
LVL 2

Expert Comment

by:hougaard
ID: 1262908
Amen!
0
 

Author Comment

by:arvindhp
ID: 2172225
sorry for the delay...
hougaard
Thanks for giving me the answer.
I am very much interested to see the code that calls main. I am using Turbo C++, Borlan international version 3.0. Please try to send the code as i doubt of having that part of the code in my compiler. Hope you will reply soon.
0
 
LVL 2

Expert Comment

by:hougaard
ID: 2172380
Well I'm not using Borland so the Watcom code would not help you.
0
 

Author Comment

by:arvindhp
ID: 2173435
can any expert help me regarding this this.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

662 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question