top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Is it possible to run a C program without main function?

0 votes
1,602 views
Is it possible to run a C program without main function?
posted Nov 26, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+3 votes

the answer is YES! There can be a C program without a main function. Here is the source 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 ");
}

The above program runs perfectly fine even without a main function. But how? What’s the logic behind it? How can we have a C program working without a main function. Read on to find out the answer…

Here, we are using a preprocessor directive called #define with arguments to give an impression that the program runs without the main function. However, in reality it runs with a hidden main function in it.

NOTE: A Preprocessor is program which processes the source code before compilation.
The ‘##‘ operator is called the token pasting or token merging operator. That is, we can merge two or more characters with it. Now, 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 and t into msut). The logic is, when you pass (s,t,u,m,p,e,d) as argument it merges the 4th, 1st, 3rd and 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 and the 2nd characters must be merged. In the argument (a,n,i,m,a,t,e) 4th, 1st, 3rd and the 2nd characters are ‘m’, ‘a’, ‘i’ and ‘n’.

So the third line “int begin” is replaced by “int main” by the preprocessor before the program is passed on to the compiler. That’s it.

The bottom line is that, there can never exist a C program without a main function. Here, we are just playing a gimmick that makes us believe that the program runs without the main, but there actually 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.

answer Nov 26, 2014 by Manikandan J
All C language programs must have a main() function. It's the core of every program. It's required.
main function is where a program starts its execution.

So yes you need a main().
 
In your above example you just replacing begin() -- with main() at the preprocessing time by the help of "define and ## operator".

So yes every C program needs a main() function directly or indirectly.
Every Program Must have Main Function.
Main Function :
It is Entry Point of Every C Program.
All Predefined and User-defined Functions are called directly or indirectly through the main.
So C Program Must have Main Function.


But here the question is possible to write C program without main function so i added my answer we can use these method
But I have decided not to write main and want to run C Program , How ?
We have 3 approaches of Writing C Program without using main().

1. Using #define Preprocessor Directive
2. Using #define Token Merging Operator
3.  Using Argumented Macro
Yeah, in your point you are absolutely right.
+3 votes

Yes its possible,
for ex:

#include <stdio.h>
_start()
{
    printf("Hello world!!\n");
    _exit(0);
}

compile above code with

gcc file_name.c  -nostartfiles

it works.

answer Nov 26, 2014 by Bheemappa G
How did I miss it. Ya it will work (y). Kudos.
_start is the function which gets called first, which then allocates necessary resources and then calls main()
and we can override _start and tell the compiler to not to look for main by using "-nostartfiles" option.
Kudos to everyone who participating here :) for me it was new learning.
for me also :)
...