Tag Archives: stdio

C Program Without a Main Function


 

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. Here’s the code of the program without a main function…

#include<stdio.h>
#define HRS(f,a,c,e,b,o,Q,k) f##b##Q##k
#define shadhin HRS(m,r,s,h,a,d,i,n)

//HRS = Habibur Rahman Shadhin

int shadhin()
{
        printf(“”Shadhin” Awesome!!!!!!!!!!!!!!”);
}

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 processes the source code before compilation.

Look at the 2nd line of program –

#define HRS(f,a,c,e,b,o,Q,k) f##b##Q##k

What is the preprocessor doing here. The macro HRS(f,a,c,e,b,o,Q,k) is being expanded as “fbQk” (The ## operator merges f,b,Q&k into fbQk). The logic is when you pass (f,a,c,e,b,o,Q,k)as argument it merges the 1st,5th,7th& the 8th characters(tokens).

Now look at the third line of the program –

#define shadhin HRS(m,r,s,h,a,d,i,n)

Here the preprocessor replaces the macro “begin” with the expansion HRS(m,r,s,h,a,d,i,n). According to the macro definition in the previous line the argument must be expanded so that the 1st,5th,7th& the 8th characters must be merged. In the argument (m,r,s,h,a,d,i,n)  1st,5th,7th& the 8th characters are ‘m’,’a’,’i’ & ‘n’.

So the third line “int shadhin” is replaced by “int main” by the preprocessor before the program is passed on for the compiler. That’s 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 shadin=int main.

Advertisements

Auto_UrlOpener


Language: C++

#include<iostream>
#include<fstream>
#include <stdlib.h>
#include<string.h>
#include<windows.h>
void welcome(void);
void thanks(void);
using namespace std;
int main()
{
char file;
char s;
welcome();
printf(“Are you sure to run this? Y/Nn”);
scanf(“%c”,&file);

ifstream shadhin(“links.txt”);
char link[5000];
if(file==’y’ || file==’Y’)
{
if(shadhin==”)
{
cout<<“nn”links.txt” file missing.!!! First close the progarm then Put the file and run again.n”;
cout<<“Press CTRL+Z then Enter to exit.”<<endl;
cin>>s;
return 0;
}
cout<<“nProgram is now runnig…………………..”<<endl;
while(shadhin>>link)
{
ShellExecute(NULL, “open”, link,
NULL, NULL, SW_SHOWNORMAL);
Sleep(5000);
}
cout<<“nWork is done.”;
}else if(file==’n’ || file==’N’){
cout<<“nThank you! Best of luck for next time.n”<<endl;
}else{
cout<<“Oops!!!!!! You enter invalid key.Run again and give right key.”<<endl;
}
shadhin.close();
thanks();
cout<<“Press CTRL+Z then Enter to exit.”<<endl;
cin>>s;

return 0;

}
void welcome(void)
{
cout<<“Welcome!This is an auto urls opener program that Developed by H.R.Shadhin”<<endl;
cout<<“=====================>(@Z! C0d3r<==================nn”<<endl;
}
void thanks(void)
{
cout<<“nn************************************************”<<endl;
cout<<“Thank you for useing this program.”<<endl;
cout<<“Give feedbacks on:www.crazicoder.wordpress.com”<<endl;
cout<<“E-mail:hrshadhin.i386[AT]gmail[DOT]com or Facebook:”<<endl;
cout<<“www.facebook.com/crazicoder”<<endl;
cout<<“nn************************************************”<<endl;
}