You are on page 1of 8

C Program Without a Main Function

Submitted by Srikanth on Monday, 3 March 200834 Comments

How to write a C program without a main function?. Is it possible to do that. Yes there can be a C program without a main function. Here‟s the code of the program without a 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) int begin() { printf(” hello “); }

Does the above program run without the main function? Yes, the above program runs perfectly fine even without a main function. But how, whats the logic behind it? How can we have a C program working without main? Here we are using preprocessor directive #define with arguments to give an impression that the program runs without main. But in reality it runs with a hidden main function. The „##„ operator is called the token pasting or token merging operator. That is we can merge two or more characters with it. NOTE: A Preprocessor is program which processess the source code before compilation.

Look at the 2nd line of program #define decode(s,t,u,m,p,e,d) m##s##u##t

3d 2nd) characters Hence in the expanded source code(the source code after being processed by preprocessor and passed on to compiler) “begin” is eventually replaced by “main”… Now read the post as well as my comment .n.m.1st.n.p..m.3rd & the 2nd characters must be merged. Popularity: 2% [?]  Lets see how the preprocessor replaces begin by main.So according to the macro definition in the third line the word “begin” is expanded into decode(a.t.e. In simple words int begin=int main.a.e.a. The logic is when you pass (s. Now look at the third line of the program #define begin decode(a.1st. But according to the macro definition of the second line decode(a. When preprocessor scans the source code it encounters the word “begin” at the 4th line of the source code.m.'a‟.e) Here the preprocessor replaces the macro “begin” with the expansion decode(a.u.t.m.t.m.m.3rd & the 2nd characters are „m‟.m.n. So the third line “int begin” is replaced by “int main” by the preprocessor before the program is passed on for the compiler.3rd & the 2nd characters(tokens).a.d) as argument it merges the 4th.s.n.t.d) is being expanded as “msut” (The ## operator merges m.u & t into msut).t. but actually there exists a hidden main function in the program.e) must be expanded into m##a##i##n (4th.e). According to the macro definition in the previous line the argument must be expanded so that the 4th.i. In the argument (a. Here we are using the proprocessor directive to intelligently replace the word begin” by “main”. That‟s it… The bottom line is there can never exist a C program without a main function.e).'i‟ & „n‟.u.i.p.i.t.a.1st.1st.a.What is the preprocessor doing here. Here we are just playing a gimmick that makes us beleive the program runs without main function.t.n.e) 4th.i.i. The macro decode(s.

/*converts an unsigned long int to string*/ while(str[i]!=‟\0′) { ch=str[i]. break. scanf(“%lu”. break. case „2′: printf(“TWO “). For example if the number entered is 12345 then the program prints the entered number in words as One Two Three Four Five #include<stdio. break.C Program to Print the Entered Number in Words Submitted by Srikanth on Tuesday. puts(“Enter the number (less than 10 digit)”). char str[12]. break. switch(ch) { case „1′: printf(“ONE “).ch.&digit). case „5′: printf(“FIVE “). case „4′: printf(“FOUR “). .10). i++.str.h> void main() { int i=0. ultoa(digit. break. case „3′: printf(“THREE “). unsigned long int digit. 4 December 2007One Comment The following C program print‟s the entered number in words.

break. break. break. break. case „9′: printf(“NINE “). „6′: printf(“SIX “). case „8′: printf(“EIGHT “). case „0′: printf(“ZERO “). case „7′: printf(“SEVEN “). } } } .

x=40. 11111 CODE: #include<stdio.i<=n.. /* To take the cursor to the co-ordinates x & y */ for(j=1. } .h> #include<conio.y=10.1 1111112 2 333 111.&n).j++) { printf(“%d “. for(i=1.n.4 4 4 4 & so on.h> void main() { int i.i). scanf(“%d”.j<=i.Home » C SOURCE CODES C Program to Generate Numbers in Pyramid Pattern Submitted by Srikanth on Friday.j.y). 7 December 20073 Comments This C program generates the numbers in pyramid pattern. It takes the input from two through nine and generates the numbers in the pyramid pattern. For example if the input is 4 then the program produces the following output … …. printf(“Enter n (between 2 & 9)\n”). clrscr().i++) { gotoxy(x.

getch(). ++i.h> #include<conio.i). } printf(“Length=%d”. } C Program to display the No. y++. int i=0. of Digits in an Entered Number Monday.&num). } getch(). clrscr(). } . scanf(“%lu”. #include<stdio. while(num!=0) { num=num/10. printf(“Enter the digit\n”).x=x-1.h> void main() { unsigned long int num. 2007 – 12:13 | No Comment This program displays the number of digits present in the number that is entered through the input. 3 Dec.

30 November 20075 Comments This program can be used to print a string without using any output statements such as printf. char far *v=(char far *)0xb8000000. v+=2. } . However this program only works in 16-bit mode since it directly writes to VDU Memory #include<stdio.i<14. } getch().h> #include<conio.h> char str[]=”Hello Srikanth”. puts etc.Program To Print A String Without Using Any Output Statements Submitted by Srikanth on Friday.i++) { *v=str[i]. for(i=0. void main() { int i.