Wednesday 20 February 2013

Can you write a "C" program with out main() ....???

/*
The answer is YES. Try the following code
*/
#include<stdio.h>
#define decodes(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");
}

// This is possible in Turbo C. In linux we've anothe way.


Here is how it works once we say define we need to understand that
#define x y
then ‘x’ ix replaced by ‘y’
similarly in this case
#define begin decode(a,n,i,m,a,t,e)
decode(a,n,i,m,a,t,e) is replaced by m##a##i##n
bcoz s is replaced by a,t by n,u by i and so on
s->a
t->n
u->i
m->m
p->a
e->t
d->e
now the statement becomes
void m##a##i##n
And u must be knowing that ## is used for string concatenation so it becomes
“main”
finally the code crops down to
void main()
{
printf(“hello”);
}

Monday 18 February 2013

Is main() user defined function or pre-defined function?????

The main function is a user implemented function whose prototype is pre -defined by compilers (known as implementations in the C standard) and used as the entry point of a C program, as per ISO/IEC 9899:TC2 Section 5.1.2.2.1:

The function called at program startup is named main. The implementation declares no prototype for this function. it shall be defined  with a return type of int and with no parameters

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names maybe used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent; or in some other implementation-defined manner.

The reason why it is prototyped by the compiler is simple: the compiler needs to know where to start so it can emit a proper executable that can be run. (Of course, if you are writing a library, it is perfectly fine to not have a main function - and in fact you shouldn't have one).