Sei sulla pagina 1di 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. Heres 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

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

Now look at the third line of the program #define begin decode(a,n,i,m,a,t,e)

Here the preprocessor replaces the macro begin with the expansion decode(a,n,i,m,a,t,e). According to the macro definition in the previous line the argument must be expanded so that the 4th,1st,3rd & the 2nd characters must be merged. In the argument (a,n,i,m,a,t,e) 4th,1st,3rd & the 2nd characters are m,'a,'i & n. So the third line int begin is replaced by int main by the preprocessor before the program is passed on for the compiler. Thats it The bottom line is there can never exist a C program without a main function. Here we are just playing a gimmick that makes us beleive the program runs without main function, but actually there exists a hidden main function in the program. Here we are using the proprocessor directive to intelligently replace the word begin by main. In simple words int begin=int main. Popularity: 2% [?]

Lets see how the preprocessor replaces begin by main.. When preprocessor scans the source code it encounters the word begin at the 4th line of the source code.So according to the macro definition in the third line the word begin is expanded into decode(a,n,i,m,a,t,e). But according to the macro definition of the second line decode(a,n,i,m,a,t,e) must be expanded into m##a##i##n (4th,1st,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

C Program to Print the Entered Number in Words


Submitted by Srikanth on Tuesday, 4 December 2007One Comment

The following C program prints the entered number in words. 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.h> void main() { int i=0; unsigned long int digit; char str[12],ch; puts(Enter the number (less than 10 digit)); scanf(%lu,&digit); ultoa(digit,str,10); /*converts an unsigned long int to string*/ while(str[i]!=\0) { ch=str[i]; i++; switch(ch) { case 1: printf(ONE ); break; case 2: printf(TWO ); break; case 3: printf(THREE ); break; case 4: printf(FOUR ); break; case 5: printf(FIVE ); break;

case 6: printf(SIX ); break; case 7: printf(SEVEN ); break; case 8: printf(EIGHT ); break; case 9: printf(NINE ); break; case 0: printf(ZERO ); break; } } }

Home C SOURCE CODES

C Program to Generate Numbers in Pyramid Pattern


Submitted by Srikanth on Friday, 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

.1 1111112 2

333 111.4 4 4 4 & so on..


11111

CODE: #include<stdio.h> #include<conio.h> void main() { int i,n,j,x=40,y=10; clrscr(); printf(Enter n (between 2 & 9)\n); scanf(%d,&n); for(i=1;i<=n;i++) { gotoxy(x,y); /* To take the cursor to the co-ordinates x & y */ for(j=1;j<=i;j++) { printf(%d ,i); }

x=x-1; y++; } getch(); }

C Program to display the No. of Digits in an Entered Number Monday, 3 Dec, 2007 12:13 | No Comment

This program displays the number of digits present in the number that is entered through the input. #include<stdio.h> #include<conio.h> void main() { unsigned long int num; int i=0; clrscr(); printf(Enter the digit\n); scanf(%lu,&num); while(num!=0) { num=num/10; ++i; } printf(Length=%d,i); getch(); }

Program To Print A String Without Using Any Output Statements


Submitted by Srikanth on Friday, 30 November 20075 Comments

This program can be used to print a string without using any output statements such as printf, puts etc. However this program only works in 16-bit mode since it directly writes to VDU Memory

#include<stdio.h> #include<conio.h> char str[]=Hello Srikanth; char far *v=(char far *)0xb8000000; void main() { int i; for(i=0;i<14;i++) { *v=str[i]; v+=2; } getch(); }

Potrebbero piacerti anche