Can You Write a C program without main() ?

maxresdefault

Trouble No. 011

C programming language, is Mother of all New languages we study.

Most of the people know that C program must have main() function to execute.

But, Have you imagine we can run C program without main() function too..!

Looking somewhat interesting right, Let’s see,

There are 2 ways to execute C program.

Way 1:

Using #define Preprocessor Directive

#include<stdio.h>

#define begin main

 

int begin() {

   printf(“Hello”);

   return (0);

}

Explanation:

  1. Save C Program using Extension .C
  2. Before Program is given to compiler program is processed by Preprocessor .
  3. begin main
  4. Source Program is scanned from Left to Right and from Top to Bottom and “begin” is replaced by “main”
  5. So Programmer feels that “He has written Code Without using Main , but internally begin is already converted into main”

 


Way 2:

Using #define Token Merging Operator

#include<stdio.h>

#define begin m##a##i##n

 

void begin() {

   printf(“Hello”);

}

Explanation:

  1. The ‘##‘ operator is called the token pasting or token merging operator.
  2. That is we can merge two or more characters with it.

 


 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s