C Program Without a Main Function

Embed Size (px)

Citation preview

  • 7/29/2019 C Program Without a Main Function

    1/8

    C Program Without a Main Function

    Submitted bySrikanthon Monday, 3 March 200834 Comments

    How to write a C program without a main function?. Is it possible to dothat. Yes there can be a C program without a main function. Heres the code of the program

    without a main function

    #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( hello );}

    Does the above program run without the main function? Yes, the above program runs perfectlyfine even without a main function. But how, whats the logic behind it? How can we have a Cprogram working without main?

    Here we are using preprocessor directive #define with arguments to give an impression that theprogram 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

    http://www.gohacking.com/author/Srikanth/http://www.gohacking.com/author/Srikanth/http://www.gohacking.com/author/Srikanth/http://www.gohacking.com/2008/03/c-program-without-main-function.html#respondhttp://www.gohacking.com/2008/03/c-program-without-main-function.html#respondhttp://www.gohacking.com/2008/03/c-program-without-main-function.html#respondhttp://www.gohacking.com/author/Srikanth/
  • 7/29/2019 C Program Without a Main Function

    2/8

    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 youpass (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 & the2nd characters are m,'a,'i & n.

    So the third line int begin is replaced by int main by the preprocessor before the program ispassed 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 isexpanded 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

    http://alexking.org/projects/wordpress/popularity-contesthttp://alexking.org/projects/wordpress/popularity-contesthttp://alexking.org/projects/wordpress/popularity-contesthttp://alexking.org/projects/wordpress/popularity-contest
  • 7/29/2019 C Program Without a Main Function

    3/8

    C Program to Print the Entered Number in Words

    Submitted bySrikanthon Tuesday, 4 December 2007One Comment

    The following C program prints the entered number in words. Forexample if the number entered is 12345 then the program prints the entered number in words as

    One Two Three Four Five

    #includevoid 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;

    http://www.gohacking.com/author/Srikanth/http://www.gohacking.com/author/Srikanth/http://www.gohacking.com/author/Srikanth/http://www.gohacking.com/2007/12/c-program-to-print-entered-number-in.html#respondhttp://www.gohacking.com/2007/12/c-program-to-print-entered-number-in.html#respondhttp://www.gohacking.com/2007/12/c-program-to-print-entered-number-in.html#respondhttp://www.gohacking.com/author/Srikanth/
  • 7/29/2019 C Program Without a Main Function

    4/8

    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;

    }

    }}

  • 7/29/2019 C Program Without a Main Function

    5/8

    HomeC SOURCE CODES

    C Program to Generate Numbers in Pyramid Pattern

    Submitted bySrikanthon Friday, 7 December 20073 Comments

    This C program generates the numbers in pyramid pattern. It takes theinput from two through nine and generates the numbers in the pyramid pattern. For example ifthe input is 4 then the program produces the following output

    .1

    1111112 2

    111113 3 3

    111.4 4 4 4 & so on..

    CODE:

    #include

    #include

    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

  • 7/29/2019 C Program Without a Main Function

    6/8

    x=x-1;

    y++;

    }getch();

    }

    C Program to display the No. of Digits in an Entered Number

    Monday, 3 Dec, 200712:13 |No Comment

    This program displays the number of digits present in the number that is entered through theinput.

    #include

    #includevoid 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();

    }

    http://www.gohacking.com/2007/12/c-program-to-display-no-of-digits-in.htmlhttp://www.gohacking.com/2007/12/c-program-to-display-no-of-digits-in.htmlhttp://www.gohacking.com/2007/12/c-program-to-display-no-of-digits-in.html#respondhttp://www.gohacking.com/2007/12/c-program-to-display-no-of-digits-in.html#respondhttp://www.gohacking.com/2007/12/c-program-to-display-no-of-digits-in.html#respondhttp://www.gohacking.com/2007/12/c-program-to-display-no-of-digits-in.htmlhttp://www.gohacking.com/2007/12/c-program-to-display-no-of-digits-in.html#respondhttp://www.gohacking.com/2007/12/c-program-to-display-no-of-digits-in.html
  • 7/29/2019 C Program Without a Main Function

    7/8

    Program To Print A String Without Using Any Output

    Statements

    Submitted bySrikanthon 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#include

    char str[]=Hello Srikanth;

    char far *v=(char far *)0xb8000000;

    void main(){

    int i;

    for(i=0;i

  • 7/29/2019 C Program Without a Main Function

    8/8