[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]


    Search the Q&A Archives


Can u write a program without main ??

<< Back to: comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ)

Question by Simha
Submitted on 8/19/2003
Related FAQ: comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ)
Rating: Rate this question: Vote
Can u write a program without main ??


Answer by dn
Submitted on 9/13/2003
Rating:  Rate this answer: Vote
i feel this can be done

please, do inform me how?

 

Answer by srinivas
Submitted on 9/15/2003
Rating:  Rate this answer: Vote
you can compile but you can't execute
suppose you write a program in one file
you can compile and you can include ex #include<ownfile> execute other fileother file

 

Answer by naidu_trk
Submitted on 12/23/2003
Rating:  Rate this answer: Vote
U cant write a full pledged c program with out main U can write code that is compiled and linked as a shared library in the Unix and other os.

U can write kernel level modules with out using main.

 

Answer by sharath
Submitted on 12/28/2003
Rating: Not yet rated Rate this answer: Vote
i hope it is not possible the compiler can't
convert the source file with out main.this is my opinion

 

Answer by sreeram
Submitted on 2/11/2004
Rating:  Rate this answer: Vote
it is possible, but it will not give result.

 

Answer by ch
Submitted on 3/20/2004
Rating:  Rate this answer: Vote
We can write c program with out main

 

Answer by NVP
Submitted on 4/2/2004
Rating: Not yet rated Rate this answer: Vote
We can write the program, but it will not compile..

 

Answer by harish
Submitted on 5/4/2004
Rating:  Rate this answer: Vote
we should understand that main is the entry point in the program.to be more clear when we write a program and compile it and start executing there is one more function called astart or _start present in crt.o file in every computer which calls main and then ur program starts executing.so the startup code calls main.u can change the entry point in ur program by using #pragmas but main is extremelly neceesary to run the program and get desired output.

 

Answer by cidsaril
Submitted on 8/4/2004
Rating: Not yet rated Rate this answer: Vote
Yes We can like

_start()
{
   printf(" No Main !!");
}

compile as
gcc -nomain <filename.c>

 

Answer by Harish
Submitted on 9/10/2004
Rating: Not yet rated Rate this answer: Vote
We can take the help of preprocessor in this

#define CAT(a,b) b##a

int CAT(in,ma) (void)
{
;
}

 

Answer by nobody
Submitted on 9/10/2004
Rating: Not yet rated Rate this answer: Vote
Yes, you can write a C program without a main but you need to tweak the compiler init code/BSP  source. All BSPs (if you are working with hardware) call main as the entry point into your routine after initialization. You can change that to any other name.


 

Answer by anwar
Submitted on 12/15/2004
Rating: Not yet rated Rate this answer: Vote
WE can write and also can compile successfully but cant execute because it'll show run time error

 

Answer by vyom
Submitted on 9/13/2005
Rating: Not yet rated Rate this answer: Vote
u may use
#define xyz main
#include <stdio.h>
int xyz() { printf("done!\n");return 0;}

this works perfectly on VC++ but i am not very sure that it will work with all compilers.However one must understand that this does involve "main" so not a satisfactory solution. :-)

 

Answer by aryan
Submitted on 10/19/2005
Rating: Not yet rated Rate this answer: Vote
this can be done by define main in another file and include it in current file.
#include"file name"

 

Answer by karthikesan
Submitted on 12/29/2005
Rating: Not yet rated Rate this answer: Vote
How can i excute a c program without main?

 

Answer by Surendra Kumar
Submitted on 4/3/2006
Rating: Not yet rated Rate this answer: Vote
Without main function we can not run program but we can run program evenly main function is purely empty.

 

Answer by Gaurav
Submitted on 4/9/2006
Rating: Not yet rated Rate this answer: Vote
/* A C Program without main function.  */

#include <stdio.h>
#define decode(s,t,u,m,p,e,d)  m##s##u##t
#define begin decode(a,n,i,m,a,t,e)
begin()
{
printf("Stumped??\n");
}

 

Answer by Prasad
Submitted on 11/27/2006
Rating: Not yet rated Rate this answer: Vote
This is how you can create a c program without having main() int it.

[Prasad@rsharma lsp]$ cat cprog_without_main.c
#include<stdio.h>
#include <unistd.h>

_start()
{
        _exit(my_main());
}

int my_main(void)
{
        printf("Hello\n");
        return 42;
}

[Prasad@rsharma lsp]$ gcc -O3 -nostartfiles  cprog_without_main.c
[Prasad@rsharma lsp]$ ls
a.out  cprog_without_main.c
[Prasad@rsharma lsp]$ ./a.out
Hello
[Prasad@rsharma lsp]$ echo $?
42

Note that ====> You need to compile the program using -nostartfiles switch with gcc.

 

Answer by TanMay
Submitted on 12/3/2006
Rating: Not yet rated Rate this answer: Vote
yes, we can write a program  with out main.
it will compile till the object file of the *.c file is created,then it will search for main to create exe file.at this point it will give link error.
so to use a *.c file without main we have to compile the *.c file with other *.c file which has a main function.

 

Answer by naren ,JNT University (A.P) INDIA
Submitted on 12/8/2006
Rating: Not yet rated Rate this answer: Vote
Yes.This can be done by using 'gcc' and 'ld'
.I did this.But idon't want to put the answer
here until DECEMBER 20 2006.Because we r cunduncting an exam on this...I will definitly answer after DECEMBER 20 2006.OK
Bye...
narenthegreate@yahoo.com or narengoogol@gmail.com

 

Answer by Bindesh
Submitted on 3/8/2007
Rating: Not yet rated Rate this answer: Vote
It has been done by me. Use linker to assign another entry point. Simple
To see complete code visit this page:

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=11240&lngWId=3

 

Answer by premkumar survi
Submitted on 4/26/2007
Rating: Not yet rated Rate this answer: Vote
_start()
{
est();
}

est()
{
printf("hello\n");
exit(0);
}


compile with :
gcc est.c -nostdlib -lc

 

Answer by Nathan
Submitted on 5/21/2007
Rating: Not yet rated Rate this answer: Vote
Code Without Main:

/*C pgm without_main.c*/

_start()
{
     _exit(my_main());
}

int my_main(void)
{
     printf("I don't have main\n");
     return 0;
}

Compile the above code using the commands given below:
$gcc -O3 -nostartfiles without_main.c



 

Answer by ashutosh
Submitted on 6/12/2007
Rating: Not yet rated Rate this answer: Vote
#include <stdio.h>
#define e1(s,t,u,m,p,e,d) m##s##u##t
#define begin e1(a,n,i,m,a,t,e)
begin()
{
printf("ashutosh");
}

 

Your answer will be published for anyone to see and rate.  Your answer will not be displayed immediately.  If you'd like to get expert points and benefit from positive ratings, please create a new account or login into an existing account below.


Your name or nickname:
If you'd like to create a new account or access your existing account, put in your password here:
Your answer:

FAQS.ORG reserves the right to edit your answer as to improve its clarity.  By submitting your answer you authorize FAQS.ORG to publish your answer on the WWW without any restrictions. You agree to hold harmless and indemnify FAQS.ORG against any claims, costs, or damages resulting from publishing your answer.

 

FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personal opinion of the poster. These posts are not intended to substitute for medical, tax, legal, investment, accounting, or other professional advice. FAQS.ORG does not endorse any opinion or any product or service mentioned mentioned in these posts.

 

<< Back to: comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ)


[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]

© 2008 FAQS.ORG. All rights reserved.