Valhalla Legends Forums Archive | C/C++ Programming | C or C++ Brain Busters

AuthorMessageTime
Mephisto
See if you can predict the output of the following program without compiling/running it.


[code]#include <stdio.h>
#define S(s)char x[]=#s;s
#define Q(x)x
#define A(x,y)y##x
#define B(x,y)A(y,x)
#define C(x,y)B(y,x)
#define Z(s,t,u)case s:if(*p!=32){t;}else{u;}break;
S(B( A( a ,m ),A(n ,i))() {B (A(h,c ),A(r ,a ))*p=x ;B(A( n, i),t)t
=0;B(A(n , i),t)s =0;B( f ,A(r, o )) (;*p;Q( p)++){C( B( A(c,t) ,h),B(A(
w, s),i))( s){ Z( 0,t+=8 *8-00 ,s ++)Z( 1,t+= 8 ;,s++ )Z
( 2, t++ ,putchar(t-73);t=s=0)}}})[/code]
May 8, 2004, 3:10 AM
Mephisto
The answer is: "Hello, world!"
For people's compilers who automatically close the console window you can replace the code with:


[code]#include <stdio.h>
#include <stdlib.h>
#define S(s)char x[]=#s;s
#define Q(x)x
#define A(x,y)y##x
#define B(x,y)A(y,x)
#define C(x,y)B(y,x)
#define Z(s,t,u)case s:if(*p!=32){t;}else{u;}break;
S(B( A( a ,m ),A(n ,i))() {B (A(h,c ),A(r ,a ))*p=x ;B(A( n, i),t)t
=0;B(A(n , i),t)s =0;B( f ,A(r, o )) (;*p;Q( p)++){C( B( A(c,t) ,h),B(A(
w, s),i))( s){ Z( 0,t+=8 *8-00 ,s ++)Z( 1,t+= 8 ;,s++ )Z
( 2, t++ ,putchar(t-73);t=s=0)}}system("pause");})[/code]

Forum PM me for spoiler information.

Next question: Figure out why the above program prints that.
May 8, 2004, 11:25 PM
Moonshine
http://www.ioccc.org/teaser/hello_2001.c

<shrugs>
May 9, 2004, 12:05 AM
Mephisto
You didn't think that I wrote that did you...? And then imply that I wrote it...?

I thought it would just be interesting if someone could figure it out without actually knowing about it. :p
May 9, 2004, 1:17 AM
Soul Taker
[quote author=Mephisto link=board=30;threadid=6694;start=0#msg59116 date=1084065445]
You didn't think that I wrote that did you...? And then imply that I wrote it...?

I thought it would just be interesting if someone could figure it out without actually knowing about it. :p
[/quote]
Why are you so defensive?
May 9, 2004, 9:54 AM
Adron
Step by step:

[code]
#include <stdio.h>
#define S(s)char x[]=#s;s
#define Q(x)x
#define A(x,y)y##x
#define B(x,y)A(y,x)
#define C(x,y)B(y,x)
#define Z(s,t,u)case s:if(*p!=32){t;}else{u;}break;
S(B( A( a ,m ),A(n ,i))() {B (A(h,c ),A(r ,a ))*p=x ;B(A( n, i),t)t
=0;B(A(n , i),t)s =0;B( f ,A(r, o )) (;*p;Q( p)++){C( B( A(c,t) ,h),B(A(
w, s),i))( s){ Z( 0,t+=8 *8-00 ,s ++)Z( 1,t+= 8 ;,s++ )Z
( 2, t++ ,putchar(t-73);t=s=0)}}})
[/code]


[code]
#include <stdio.h>
#define S(s)char x[]=#s;s
#define Q(x)x
#define A(x,y)y##x
#define B(x,y)A(y,x)
#define C(x,y)B(y,x)
#define Z(s,t,u)case s:if(*p!=32){t;}else{u;}break;
char x[]="B( A( a ,m ),A(n ,i))() {B (A(h,c ),A(r ,a ))*p=x ;B(A( n, i),t)t =0;B(A(n , i),t)s =0;B( f ,A(r, o )) (;*p;Q( p)++){C( B( A(c,t) ,h),B(A( w, s),i))( s){ Z( 0,t+=8 *8-00 ,s ++)Z( 1,t+= 8 ;,s++ )Z ( 2, t++ ,putchar(t-73);t=s=0)}}}";
B( A( a ,m ),A(n ,i))() {
B (A(h,c ),A(r ,a ))*p=x ;
B(A( n, i),t)t =0;
B(A(n , i),t)s =0;
B( f ,A(r, o )) (;*p;Q( p)++){
C( B( A(c,t) ,h),B(A( w, s),i))( s){
Z( 0,t+=8 *8-00 ,s ++)
Z( 1,t+= 8 ;,s++ )
Z( 2, t++ ,putchar(t-73);t=s=0)
}
}
}
[/code]



[code]
#include <stdio.h>
#define B(x,y)x##y
#define C(x,y)y##x
char x[]="B( A( a ,m ),A(n ,i))() {B (A(h,c ),A(r ,a ))*p=x ;B(A( n, i),t)t =0;B(A(n , i),t)s =0;B( f ,A(r, o )) (;*p;Q( p)++){C( B( A(c,t) ,h),B(A( w, s),i))( s){ Z( 0,t+=8 *8-00 ,s ++)Z( 1,t+= 8 ;,s++ )Z ( 2, t++ ,putchar(t-73);t=s=0)}}}";
B( ma,in)() {
B (ch,ar)*p=x ;
B(in,t)t =0;
B(in,t)s =0;
B( f , or) (;*p; p++){
C( B( tc ,h),B(sw,i))( s){
case 0:if(*p!=32){t+=8 *8-00; }else{s ++;}break;
case 1:if(*p!=32){t+= 8 ;;}else{s++;}break;
case 2:if(*p!=32){t++ ;}else{putchar(t-73);t=s=0;}break;
}
}
}
[/code]



[code]
#include <stdio.h>
#define B(x,y)x##y
#define C(x,y)B(y,x)
char x[]="B( A( a ,m ),A(n ,i))() {B (A(h,c ),A(r ,a ))*p=x ;B(A( n, i),t)t =0;B(A(n , i),t)s =0;B( f ,A(r, o )) (;*p;Q( p)++){C( B( A(c,t) ,h),B(A( w, s),i))( s){ Z( 0,t+=8 *8-00 ,s ++)Z( 1,t+= 8 ;,s++ )Z ( 2, t++ ,putchar(t-73);t=s=0)}}}";
main() {
char *p=x;
int t=0;
int s=0;
for(;*p; p++){
switch(s){
case 0:if(*p!=32){t += 64; }else{s ++;}break;
case 1:if(*p!=32){t += 8; }else{s++;}break;
case 2:if(*p!=32){t++; }else{putchar(t-73);t=s=0;}break;
}
}
}
[/code]

The string to be printed is thus encoded in the program itself. The information is carried in the length of the words / non-space strings. Each character is encoded in three words. If the lengths of the three words are a, b and c, the character is a*64 + b*8 + c - 73.

Example #1: "B( A( a "

The word lengths are 2, 2, 1. The character is 2*64 + 2*8 + 1 - 73 = 72 = 'H'.

Example #2: ",m ),A(n ,i))() "

The word lengths are 2, 5, 6. The character is 2*64 + 5*8 + 6 - 73 = 101 = 'e'.
May 9, 2004, 11:28 AM

Search