Chủ Nhật, 14 tháng 9, 2014

C program with out main

Can You Write a C program with out main ????


If any one asks you this question from now say yes!!!!!Of course you can create a Cprogram and run it with out giving any main() function.

see below code snippet ..written by me


#include<stdio.h>
#include<conio.h>
#define code(s,h,a,v,r,c,e)e##v##s##c
#define decode code(i,h,u,a,r,n,m)
int decode()
{
clrscr();
printf("hello shashi kumar");
return 0;
}


output :

hello shashi kumar

------------------------------------------------
You might have confused by seeing above code .But it works fine(copy the code and run it on your own machine).

note:we are not calling main() in our  program but internally it calls.

How does the  above code works???

Its simple. We all know that C preprocessors  were compiled before actual program starts compiling, so I created two simple preprocessors here, namely

#define code(s,h,a,v,r,c,e)e##v##s##c ,#define decode code(i,h,u,a,r,n,m).

First one have arguments(),followed by e##v##s##c.Here #(pond[hash])  appends each character with its previous characters so whole statements treated as evsc .

The character e in evsc presented at 7th position of argument list of code(.....e),v presented at 4th position ,s presented at 1st position ,c presented at 6th position .

Next second preprocessor with a name decode  is calling code() of arguments i'e the positions of each character of code is substituted by positions of evsc.
i'e  e substituted by m
       v substituted by a
       s substituted by i
       c substituted by n










 

Không có nhận xét nào:

Đăng nhận xét