-5

How to write C program without using Main...! When I'm learning how to write ASM file by for a simple C file [of length 3 lines], I got this doubt. I assembly file I used preamble and post ambles, at function.

Sita
  • 1
  • 1
  • 1
    Have a look at: [http://stackoverflow.com/questions/4113731/is-a-main-required-for-a-c-program](http://stackoverflow.com/questions/4113731/is-a-main-required-for-a-c-program), search for similar topics in stackoverflow, there are a lot of them – phoxis Jun 02 '11 at 17:42

3 Answers3

3

This is logical trick. Those who are unaware of it, can learn this trick.

#include<stdio.h>
#include<conio.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()
{
    clrscr();
    printf("\nHello !!! Kaushal Patel.");
    getch();
}

Explanation :
The pre-processor directive #define with arguments is used to give an impression that the program runs without main(). But in reality it runs with a hidden main().

The ‘##‘ operator is called the token pasting or token merging operator. That is how, I can merge two or more characters with it.

#define decode(s,t,u,m,p,e,d) m##s##u##t

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 I pass (s,t,u,m,p,e,d) as argument it merges the 4th,1st,3rd & the 2nd characters.

#define begin decode(a,n,i,m,a,t,e)

Here the pre-processor 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 “void begin” is replaced by “void main” by the pre-processor before the program is passed on for the compiler.

Source : http://ctechnotips.blogspot.in/2012/04/writing-c-c-program-without-main.html

imp25
  • 2,327
  • 16
  • 23
Kaushal
  • 31
  • 1
3

There is a great article and creating the smalest possible elf binary here. It has a lot of info of what is required to have something runnable by the os.

rerun
  • 25,014
  • 6
  • 48
  • 78
0

Here is your answer:->

#include <stdio.h>

extern void _exit(register int);

int _start(){

printf(“Hello World\n”);

_exit(0);

}
Parth
  • 644
  • 4
  • 10