C++--tricky one

Status
Not open for further replies.

garfield_56

zzzzzzzzzz........
Hey all...

I guess most of u will figure this out....but anyways, i found it interesting, so i thought i'll share it with u all!!!!

C++ without main()
Code:
#include<stdio.h>
#define decode(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 ");
}


Try and figure it out (that is...if u haven't done dat already!!) and i'll post d explanation tomorrow!!
 
Last edited:

QwertyManiac

Commander in Chief
Obvious enough, a pre-processor trick. You're replacing begin with main using two clever words and a re-ordered decoding pre-processor definition.
 

Faun

Wahahaha~!
Staff member
1.
int begin()
{


}

2. int decode(a, n, i, m, a, t, e)(){


}

3. decode(a, n, i, m, a, t, e) is replace by main (# are not counted)

Look at
decode(s,t,u,m,p,e,d) m##s##u##t

m is at the 4th position, s is at first, u is at 3, t is at 2

when we expand decode(a, n, i, m, a, t, e)
at 4th is m, at 1st is a, at 3rd is i, at 2nd is n
so that is main

4.
int main()
{

}
 

Abhishek Dwivedi

TechFreakiez.com
hahahaha old one...i trouble ma computer teacher last year by asking this question to him while he was taking my Viva...hehehehe...it was fun
 
OP
garfield_56

garfield_56

zzzzzzzzzz........
Care to explain me what's going on in the code.


Qwerty & T159 did explain it, yet as i promised---here's the explanation((m putting it up in as simple a way as i can))!!!!


Look at dis line:::

Code:
#define decode(s,t,u,m,p,e,d) m##s##u##t

Here, in away, we r teachin d compiler a code language "decode" that deciphers the word written after it in this way :: stumped = msut (i.e.- it reaarranges the 1st 4 letters)

Now, wen we write this::


Code:
#define begin decode(a,n,i,m,a,t,e)


we are asking the compiler to decode "animate" in a similar manner (i.e. animate = main) and define begin with this deciphered code word ( wich, in this case is main)

So now, wenever we write "begin", the compiler reads it as "main".

Hence, "int begin()" actually means "int main()" :D:D

Hope its clear now!!!!






_____________________________________________________________
hahahaha old one...i trouble ma computer teacher last year by asking this question to him while he was taking my Viva...hehehehe...it was fun



By d way...did ur teacher get d trick or not??:D:cool::cool::D
 
Last edited:

Plasma_Snake

Indidiot
Qwerty & T159 did explain it, yet as i promised---here's the explanation((m putting it up in as simple a way as i can))!!!!


Look at dis line:::

Code:
#define decode(s,t,u,m,p,e,d) m##s##u##t
Here, in away, we r teachin d compiler a code language "decode" that deciphers the word written after it in this way :: stumped = msut (i.e.- it reaarranges the 1st 4 letters)

Now, wen we write this::


Code:
#define begin decode(a,n,i,m,a,t,e)
we are asking the compiler to decode "animate" in a similar manner (i.e. animate = main) and define begin with this deciphered code word ( wich, in this case is main)

So now, wenever we write "begin", the compiler reads it as "main".

Hence, "int begin()" actually means "int main()" :D:D

Hope its clear now!!!!






_____________________________________________________________




By d way...did ur teacher get d trick or not??:D:cool::cool::D

Explained it clearly, thanx a lot man. Its not my fault if I never came across this thing because both the books and the teachers teaching us were too lame to know about anything like it. After understanding the code its clear that main is there but just needs to be deciphered. I say clever coding, in the end the program is not totally without main. ;)
 
OP
garfield_56

garfield_56

zzzzzzzzzz........
Another One!!!!!

hey all!! My friend asked me this one....let's see if u can figure this one out!!!



program without semicolon!

Code:
#include <iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
if(printf("Hello World!\n")){}
getch();
}

And yeah--- if any of u have such fun programs, do share it here....it will make c++more interesting for us newbies!!!!! :)
 
Last edited:
Status
Not open for further replies.
Top Bottom