Here is another approach. This is not as good as fork in that it only works half the time (therefore, does not completely solve the problem), but it is better that the message never changes.
#include <stdio.h> int main() { if ( ftell( stdout ) % 2 || ( printf( " " ), main() ) ) printf( "Hello " ); else printf( "World\n" ); }
He first queries stdout to find out how many were printed and the number of characters is odd. If so, it then prints one character to cancel parity and recursion. A recursive call sees an even number of characters and prints "hello" and returns 0. 0 sends the top call to else, printing "world".
The number of characters in the terminal must be odd for operation.
source share