473,651 Members | 3,063 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how main() is called in C programme

Hi ALL,
I am working in C from past few months. Still now i can't
figure out who is called main() in 'C' programme?
Main() is a function from that we can call our sunroutines but someone
has to call this Main() and it will return to whom and in runtime
this Main() is stored/runnng exactly where in the memroy ?

I will appreciate if someone help me to figure it out .

Looking forward to .

Thanks

Chinmoy
you
Jun 27 '08 #1
6 2555
On 27 Apr 2008 at 18:43, chang wrote:
figure out who is called main() in 'C' programme?
Main() is a function from that we can call our sunroutines but someone
has to call this Main() and it will return to whom and in runtime
this Main() is stored/runnng exactly where in the memroy ?
There's an informative article on this question for gcc/Linux here:
http://linuxgazette.net/issue84/hawk.html

Ask if you have any questions about it.

Jun 27 '08 #2
In article <f1************ *************** *******@l17g200 0pri.googlegrou ps.com>,
chang <ch************ ******@gmail.co mwrote:
I am working in C from past few months. Still now i can't
figure out who is called main() in 'C' programme?
Main() is a function from that we can call our sunroutines but someone
has to call this Main() and it will return to whom and in runtime
this Main() is stored/runnng exactly where in the memroy ?
The details of how main() is called are system dependant.

For a description of how one particular Unix system does it,
and keeping in mind that the details might differ on other unix
systems, see the reference to crt0.o in

http://docs.hp.com/en/B2355-90968/compileandlink.htm
--
"I like to build things, I like to do things. I am having
a lot of fun." -- Walter Chrysler
Jun 27 '08 #3
chang wrote:
Hi ALL,
I am working in C from past few months. Still now i can't
figure out who is called main() in 'C' programme?
Main() is a function from that we can call our sunroutines but someone
has to call this Main() and it will return to whom and in runtime
this Main() is stored/runnng exactly where in the memroy ?

I will appreciate if someone help me to figure it out .
Under "hosted" systems the so-called "host environment" is responsible
for invoking main (note: C is case sensitive. main and Main are
different identifiers). When the program is done control is given back
to the host environment in a implementation defined fashion. One common
candidate for the host environment is the operating system. Examples
are Linux, Solaris, Windows, DOS etc.

Things are often very different for the so-called "freestandi ng"
systems. Here the program entry point need not be named main at all.
Also very often there is no host environment. The program is directly
invoked by the hardware and normally terminates only when the device is
switched off. Under such environments of course it might not make sense
to return from the main function (or whatever it's called) or to invoke
exit, _Exit or similar.

Details of course vary enormously. Some embedded systems do have
complete operating systems and allow execution of normal hosted C
programs. Note that the host software itself is a (if written in C)
freestanding program.

The details of how exactly control is given to a hosted C program and
what happens after such a program terminates are *very* system
dependent. It will vary depending on the processor, operating system
and your C implementation.

Often the case is that your C compiler will prefix your program with a
small amount of code which is called by the host system when your
program is run. This code may do various housekeeping duties like
opening the predefined streams (stdin, stdout and stderr), initialising
the malloc subsystem, setting up the commandline arguments to main,
setting up default signal handlers, setting your default locale and
other such tasks before invoking main as a normal function call.

Similarly when your program terminates control may be passed to code
that's linked in with your program by the compiler. This often resides
in implementation defined functions called by exit. Often this is the
function _Exit. It closes open streams, flushes unwritten output and
calls any functions registered with atexit before transferring control
back to the host environment, usually through a system call.

Please ask in a system specific group like comp.unix.progr ammer or
comp.os.ms-windows.program mer.win32 for more details. Also reading the
source for a C library implementation like glibc might be illuminating.

The terms used in this post like hosted, freestanding etc. are all
defined in the ISO standard for C, a public draft of which can be
obtained at:

<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf>

Jun 27 '08 #4
On Apr 27, 11:43*am, chang <chinmoy.chitta ran...@gmail.co mwrote:
Hi ALL,
* * * * * *I am working in C from past few months. Still now ican't
figure out who is called main() in 'C' programme?
Main() is a function from that we can call our sunroutines but someone
has to call this Main() *and it will return to whom and in runtime
this Main() is stored/runnng exactly where in the memroy ?

I will appreciate if someone help me to figure it out .

Looking forward to .

Thanks

Chinmoy
you
Thanks for all responses but is there any straight forward
documenatation/link is there so that it can guide me in a disciplined
way?

Again looking forward to you guys.

Thanks
Chinmoy
Jun 27 '08 #5
chang wrote:
On Apr 27, 11:43*am, chang <chinmoy.chitta ran...@gmail.co mwrote:
>Hi ALL,
I am working in C from past few months. Still now i can't
figure out who is called main() in 'C' programme?
Main() is a function from that we can call our sunroutines but
someone has to call this Main() *and it will return to whom and in
runtime this Main() is stored/runnng exactly where in the memroy ?

I will appreciate if someone help me to figure it out .
[ ... ]
Thanks for all responses but is there any straight forward
documenatation/link is there so that it can guide me in a disciplined
way?
The details vary from system to system so there is no one piece of
documentation that will inform you. Some posters have already given you
some links. For further help you should post to a group that deals with
your system like comp.os.linux.p rogramming.apps or comp.unix.progr ammer
or comp.os.ms-windows.program mer.win32 or a group in the
microsoft.publi c.* hierarchy. The details of how your program is called
and how it exits is also heavily influenced by your compiler and C
standard library implementations , in addition to your operating system.
Again try in a compiler specific group. There is simply no universal
answer.

Here is one link that is x86/UNIX specific:

<http://fgiasson.com/articles/memorylayout.tx t>

<snip>

Jun 27 '08 #6
chang <ch************ ******@gmail.co mwrites:
On Apr 27, 11:43Â*am, chang <chinmoy.chitta ran...@gmail.co mwrote:
>Â* Â* Â* Â* Â* Â*I am working in C from past few months. Still now i can't
figure out who is called main() in 'C' programme?
Main() is a function from that we can call our sunroutines but someone
has to call this Main() Â*and it will return to whom and in runtime
this Main() is stored/runnng exactly where in the memroy ?

I will appreciate if someone help me to figure it out .
[...]
Thanks for all responses but is there any straight forward
documenatation/link is there so that it can guide me in a disciplined
way?
The C language doesn't have a whole lot to say about how main() is
invoked. To see just what it does say, download
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf(the
latest semi-official draft of the C standard) and read section 5.1.2,
"Execution environments".

A quick summary:

In a hosted implementation, the main() function is invoked by the
execution environment. Certain things need to be set up before main()
begins executing. The details of how this is done are left up to the
implementation. Under a freestanding implementation (typically, but
not necessarily, an embedded system), the requirements are even
looser; the program entry point might not be called "main".

For more details about what happens in the implementation you're
using, ask in an implementation-specific newsgroup
(comp.unix.prog rammer, comp.os.ms-windows.program mer.win32, etc.).

Why do you want to know? That's not meant to imply that you shouldn't
be curious, but for portable programming you really don't *need* to
know these details. Your program will work as specified regardless of
where your main function happens to be in memory or how it's invoked.

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1242
by: cm012b5105 | last post by:
Hello i want to put an interactive text programme in to a graphical box this is a small section of my text programme s = raw_input ("Hello whats your name? ") if s=='melvyn': print "your my boss's Dad the one they call in indian language DEEP THOUGHT LITTLE HORSE" if s=='carol': print "ahhh you are my boss's mom the one they call WREATH WOMAN" if s=='rebecca':
0
1577
by: melledge | last post by:
Full Programme for XTech 2005 Announced Premier European XML Industry Event Expands Focus to "XML, the Web and Beyond"; Co-hosted by the Mozilla Foundation,W3C, and OASIS, Presenters Include BBC, IBM, Justsystem, Microsoft, Oracle, Sun Alexandria, Va. - March 7, 2005 - IDEAlliance, a leading industry association dedicated to fostering XML and other information technology
2
1999
by: NDAKI MBOULET | last post by:
J'ai un problème pour écrire un programme. Voici mon sujet: Ecrire en c++ un programme qui reçoit en entrée une suite d'instruction encadrées par les mots clés BIBODLE et LISUK dans un langage quelconque appélé Bazoréen et rend en sortie soit une liste des erreurs rencontrées lors du traitement, soit un programme source en c++, sans erreurs de syntaxe ni de semantique, compilable et exécutable. Les éléments du bazoréen sont: 1 opérateurs
5
1480
by: katekukku | last post by:
HI, I need the source code for a programme in C. It should have the basic features of a paint programme like circle, line etc, etc,. I lost a programme which wa driven by keyboard, if somebody could provide me that one it will be great. The source should be of rookie level and not too much complex. Thanks a lot SHIKHAR
2
1105
by: garyusenet | last post by:
static void Main(string args) { } If you know your programme will not take any string arguments, would it make any difference at all if you removed the string arguments from the code? And had this instead: - static void Main() {
0
1010
by: =?Utf-8?B?SmF5ZQ==?= | last post by:
I have created the programme, then using the font in Office 2007. However the user's PC haven't install the font style, when i publish my programme by clickonce, and the user use it, the font style is changed. So how can i publish my progamme by clickonce and the user will atuo install the font style before stasrt the programme
4
1561
by: koolbijay | last post by:
hello plz can u help me in providing the source code for the following programmes 1.c programme without main() function 2.how to get size of a datatype without using sizeof operator
15
2313
by: arnuld | last post by:
i am not able to figure out the error: /* C++ Primer - 4/e * * exercise 7.16 * STATEMENT: * write a programme that accepts the arguments to main. print * the values passed to main. * */
6
1624
by: =?ISO-8859-1?Q?FERHAT_A=C7ICI?= | last post by:
hi everyone...I want to run any programme or file with my programme.example any file or programme, like this "xxx.ncn".I want If users double click this xxx.ncn this file can run by my programme.please help!!!!
0
8361
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8701
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8584
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7299
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1588
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.