Yes it is possible to write a program in c without using main.
Here is the code:
/* prog_without_main.c */
_start()
{
_exit(my_main());
}
int my_main(void)
{
printf(“Hello\n”);
return 42;
}
And use this command (% is command prompt) to compile:
%gcc -O3 -nostartfiles prog_without_main.c
Try compiling this example:
#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)
void begin()
{
printf(“hello”);
}
here is how it works once we say define we need to understand that
#define x y
then ‘x’ ix replaced by ‘y’
similarly in this case
#define begin decode(a,n,i,m,a,t,e)
decode(a,n,i,m,a,t,e) is replaced by m##a##i##n
bcoz s is replaced by a,t by n,u by i and so on
s->a
t->n
u->i
m->m
p->a
e->t
d->e
now the statement becomes
void m##a##i##n
And u must be knowing that ## is used for string concatenation so it becomes
“main”
finally the code crops down to
void main()
{
printf(“hello”);
}
Filed under: Basic, Functions & Structures
Hi
wht the procedure to run the 1st prog. i try to run but it gives
no out put. can I compile this as follows
gcc -o -astart sourcefile.c
astart function is ist function called before executing main
astart function needs one global main.
very good practice.This helps me in designing of my own kernel,which starts from my own ‘main()’ –>kernel_start().
ThanQ Bye
I know the solution.If u have any doubts in C,send a mail to me. email: narenthegreate@yahoo.com or narengoogol@gmail.com
This is working in Turbo c++ IDE.
Is there any other way of writing any program without main or execute any function before main
Dear Friends,
If you want to write th code without the main function you have to write the equivalent Assembly or machine code for that and from there you can call the required c function.
Thank you.
hi want profect in c lang.so i am working with diff prog.
Thanks for this can u give another example
You can use #pragma directive to start a program execution from some other function other than main()
I dont remember the exact syntax….Use google to explore this option.
Using #define is not the way…its just a useless trick to replace some other word by “main”…the point is the word “main” should not appear in the program when the execution is about to begin, i.e after pre-processing…for that use #pragma directive.
search on google for – #pragma startup
that shall do it…
well , i find an error . if u using int main() then in the end use return (0); cause int main demands a value must be return .
I’m getting errors in the 1st program. I think there is a different way to save or compile the 1st program.
_start()
{
_exit(my_main());
}
int my_main(void)
{
printf(“Hello Santosh. This is start without main!!!\n”);
return 42;
}
gcc -o hello -nostartfiles withoutMain.c
It works
/* nomain.c*/
#include
#include
int start_naren()
{
printf(“Hello! Here no main().– Just say start_naren()! \n”);
exit(0);
}
/*Do not use
return 0;
instead of
exit(0);
or else you will get ‘segment fault’ error ,because of ‘unflushing buffers’ problem.
*/
—————————————————————-
/*compile this program ‘nomain.c’ on Any UNIX/linux distribution
using the fallowing commands.
—————————————————————-
promt$ gcc -c nomain.c
promt$ ld -dynamic-linker /lib/ld-linux.so.2 -estart_naren -o nomain nomain.o -lc
promt$ ./nomain
Hello! Here no main().– Just say start_naren()!
promt$
————————————————
I used gcc version 4.1.0 on SUSE 10.1 and Redhat Fedora core 5 , Linux destributions
you can use any GNU compiler kit .U will definitely got the same results
By
Naren Allam,
Microsoft R&D