Hi,
Yeah you can write a C program without main() routine. Everytime you
compile a C program, the compiler provides a default startup /
initialization routine, which is mostly named as _start and may be
found in compiler files. This routine initializes the processor
registers and does several other things to make it run on the required
processor. This default routine invokes a routine by the name main()
and we all write a main routine.
So, in conclusion, compiler assumes that our program will contain a
main() routine and  it is not a special routine / has not special
significance, can say its a custom in C compilers.

Now to write a program without main, you have following options.
Mostly, the default init / start up routine is available in cr0.s in
gcc / g++ compilers. You will have to see this for your compiler.

1. Provide your own initialization / start up routine to the compile,
write code for initialization / startup for your program ( including
register init and all ). And then invoke your own routine instead of
main.

2. Modify the default initialization / start up routine to invoke your
routine instead of main. But, this may cause problems because once you
change the default init / start up routine all your programs compiled
with this default init / start up routine would require the same
routine instead of main.

Regarding, why your program with main = 0 is failing:
When you are compiling your program containing main = 0, compiler has
again provided the default init/start routine which assumes that your
program has a symbol main, since you have written int main = 0, the
compiler was happy resolving symbol 'main' and generated the required
executable file. When you run this executable, the default init / start
up routine invokes routine main, which is 0 in your program and it
causes the crash.

Hope it helps, any comments from others are welcome.
regards.
Anish Nema
Arulanandan P wrote:
> hi ,
>
>     Can any one let me , is it possible to write C programs without main()
> function.. If it is possible then how do we change the start up routine
> which transfer the control to the main() function.. Is there any way in the
> compiler to do this..
>
>      Also ,
>            can i write a simple program , f1.c . The content of the file is
> :
>
>                     int main =0 ;
>
>        just the one line of code .. when i compile it  , there is no error.
> but when i run it i get error message: Illegal Instruction..
>
> Can anyone help me out..
>
> regards
> arulanandan
>
> ------=_Part_35706_18530609.1147605379257
> Content-Type: text/html; charset=ISO-8859-1
> Content-Transfer-Encoding: quoted-printable
> X-Google-AttachSize: 984
>
> <br>hi , <br><br>&nbsp;&nbsp;&nbsp; Can any one let me , is it possible to 
> write C programs without main() function.. If it is possible then how do we 
> change the start up routine which transfer the control to the main() 
> function.. Is there any way in the compiler to do this..
> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp; Also , 
> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; can i write 
> a simple program , f1.c . The content of the file is 
> :<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
>  int main =0 ;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; just the one line 
> of code .. when i compile it&nbsp; , there is no error. but when i run it i 
> get error message: Illegal Instruction..
> <br><br>Can anyone help me out..<br><br>regards 
> <br>arulanandan<br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br><br>
> 
> ------=_Part_35706_18530609.1147605379257--

X-Google-Language: ENGLISH,ASCII-7-bit
Received: by 10.11.53.63 with SMTP id b63mr109828cwa;
        Sun, 21 May 2006 17:06:42 -0700 (PDT)
X-Google-Token: bu--JgwAAABIxZeyp2jrQFAg-01hOPFl
Received: from 61.246.60.87 by g10g2000cwb.googlegroups.com with HTTP;
        Mon, 22 May 2006 00:06:42 +0000 (UTC)
From: "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
To: "Algorithm Geeks" <algogeeks@googlegroups.com>
Subject: Re: Writing C program without main()
Date: Sun, 21 May 2006 17:06:42 -0700
Message-ID: <[EMAIL PROTECTED]>
In-Reply-To: <[EMAIL PROTECTED]>
References: <[EMAIL PROTECTED]>
User-Agent: G2/0.2
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) 
Gecko/20051111 Firefox/1.5,gzip(gfe),gzip(gfe)
Mime-Version: 1.0
Content-Type: text/plain

Hi,
Yeah you can write a C program without main() routine. Everytime you
compile a C program, the compiler provides a default startup /
initialization routine, which is mostly named as _start and may be
found in compiler files. This routine initializes the processor
registers and does several other things to make it run on the required
processor. This default routine invokes a routine by the name main()
and we all write a main routine.
So, in conclusion, compiler assumes that our program will contain a
main() routine and  it is not a special routine / has not special
significance, can say its a custom in C compilers.

Now to write a program without main, you have following options.
Mostly, the default init / start up routine is available in cr0.s in
gcc / g++ compilers. You will have to see this for your compiler.

1. Provide your own initialization / start up routine to the compile,
write code for initialization / startup for your program ( including
register init and all ). And then invoke your own routine instead of
main.

2. Modify the default initialization / start up routine to invoke your
routine instead of main. But, this may cause problems because once you
change the default init / start up routine all your programs compiled
with this default init / start up routine would require the same
routine instead of main.

Regarding, why your program with main = 0 is failing:
When you are compiling your program containing main = 0, compiler has
again provided the default init/start routine which assumes that your
program has a symbol main, since you have written int main = 0, the
compiler was happy resolving symbol 'main' and generated the required
executable file. When you run this executable, the default init / start
up routine invokes routine main, which is 0 in your program and it
causes the crash.

Hope it helps, any comments from others are welcome.
regards.
Anish Nema
Arulanandan P wrote:
> hi ,
>
>     Can any one let me , is it possible to write C programs without main()
> function.. If it is possible then how do we change the start up routine
> which transfer the control to the main() function.. Is there any way in the
> compiler to do this..
>
>      Also ,
>            can i write a simple program , f1.c . The content of the file is
> :
>
>                     int main =0 ;
>
>        just the one line of code .. when i compile it  , there is no error.
> but when i run it i get error message: Illegal Instruction..
>
> Can anyone help me out..
>
> regards
> arulanandan
>
> ------=_Part_35706_18530609.1147605379257
> Content-Type: text/html; charset=ISO-8859-1
> Content-Transfer-Encoding: quoted-printable
> X-Google-AttachSize: 984
>
> <br>hi , <br><br>&nbsp;&nbsp;&nbsp; Can any one let me , is it possible to 
> write C programs without main() function.. If it is possible then how do we 
> change the start up routine which transfer the control to the main() 
> function.. Is there any way in the compiler to do this..
> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp; Also , 
> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; can i write 
> a simple program , f1.c . The content of the file is 
> :<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
>  int main =0 ;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; just the one line 
> of code .. when i compile it&nbsp; , there is no error. but when i run it i 
> get error message: Illegal Instruction..
> <br><br>Can anyone help me out..<br><br>regards 
> <br>arulanandan<br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br><br>
> 
> ------=_Part_35706_18530609.1147605379257--


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---

Reply via email to