Saturday, May 22, 2010

In C language , how can a string be printed to the console , with an empty main body ?? Impossible ??

Right now I can't come up with a way to do it in C.





In C++ you can do this:





class X


{


public:


X()


{


printf("Hello World!\n");


}


};





X x;





void main()


{


}





The constructor for x gets called before main is run.


I just tested this using VC6.0, and it works.

In C language , how can a string be printed to the console , with an empty main body ?? Impossible ??
It's been a long time since I've worked with C, but I believe that if you clear the screen then use the method





cout %26lt;%26lt; "String"





That'll do it... If that doesn't work, try changing the direction of the pointer.
Reply:You can use #error, but that will print a message at compile time. Some compilers also allow you to display a message via #pragma, but again, that's at compile time.





You can do something like this:


#include %26lt;stdio.h%26gt;





#define OBRACKET {printf("Hello world\n");


#define CBRACKET }





main()


OBRACKET


CBRACKET








Another option is use my example above, but substitute atexit(foo); for printf(), then declare function foo as such:


void foo()


{


printf("Test\n");


}





There might also be a way to do it in C++, but I'm not sure.


No comments:

Post a Comment