C program without main function




Can you write a c program without using main function?


We can write a c program without using main function. We can compile it but we cannot execute a program without a main function. Since in C execution of any program start from main function.  Examples of c program without a main is all the c library functions.  Function printf is an example of library function which has been written and complied without using main function. To use or run such functions we write other program using main function and call those functions.

19 comments:

denesh said...

nice!!!!!!!!!!!

Anonymous said...

#include

class test
{
public:
test(){
exit(1);
};
} ;

test worker;

Anonymous said...

hi i am only saying that you r talking about c and using class that does not exist in c

Anonymous said...

not possible in gcc compiler

UNMAD said...

then plz give the code.

NiTiS said...

solution:

#include
#define decode(s,t,u,m,p,e,d) m##s##u##t
#define begin decode(a,n,i,m,a,t,e)

int begin()
{
printf("program without main function");
return 0;
}


Explanation:

The macro decode(s,t,u,m,p,e,d) is being expanded as “msut” (The ## operator merges m, s, u and t into msut). The logic is, when you pass (s,t,u,m,p,e,d) as argument it merges the 4th, 1st, 3rd and the 2nd characters (tokens).

Note :
we can't write a C program without a main function. Here, we are just playing a gimmick that makes us believe that the program runs without the main, but there actually exists a hidden main function in the program. Here, we are using the pre-processor directive to intelligently replace the word “begin” by “main”. In simple words: int begin = int main

Anonymous said...

Good work keep it on

Anonymous said...

Amazing info :)

Anonymous said...

really great

Anonymous said...

Good logic. Appreciate it.

venkata babu said...

#include
#define begin main
void begin()
{
printf("Chinna");
}

Anonymous said...

very nice explanation....

hanu said...

#include
#include
#define m(h,a,n,m,t,i) h##i##m##n
#define mn m(m,b,n,i,c,a)
void mn()
{
printf("\n C progrsmming");
getch();
}

Unknown said...

in GCC compiler
#include stdio.h
#include stdlib.h
f()
{
printf("I can able write program w.o.t main function\n");
exit(0);
}

1)save it as name.c
2)cc -nostartfiles name.c
3)./a.out

1000% working

manohar said...

Its working...!

ron said...

#include
#define decode(s,t,u,m,p,e,d) m##s##u##t
#define begin decode(a,n,i,m,a,t,e)

int begin()
{
printf("program without main function");
return 0;
}




plzz tell me this code syntax...

ron said...

plzz tell me syntax

Unknown said...

#include
#include
#define decode(s,t,u,m,p,e,d) m##s##u##t
#define begin decode(a,n,i,m,a,t,e)
int begin()
{
printf("program without mainfunction");
return 0;
}

Unknown said...

Using a macro that defines main

#include
#define fun main
int fun(void)
{
printf("Geeksforgeeks");
return 0;
}
p/o:-->> Geeksforgeeks