Translate

Sunday, May 25, 2014

Write a running C code without main()

Write a C code that prints “Hello C” without any main function. This is a trick question and can be solved in following ways.

1) Using a macro that defines main
#include<stdio.h>
#define fun main
int fun(void)
{
    printf("Hello C");
    return 0;
}
Output:
Hello C

2) Using Token-Pasting Operator
The above solution has word ‘main’ in it. If we are not allowed to even write main, we ca use token-pasting operator (see this for details)
#include<stdio.h>
#define fun m##a##i##n
int fun()
{
    printf("Hello C");
    return 0;
}
Output:
Hello C

No comments:

Post a Comment