DIGIT-ARC read-only archive · snapshot: July 23, 2026

C++--tricky one

25 posts · originally from https://geek.digit.in/community/threads/c-tricky-one.103003/

garfield_56
garfield_56 Original post #1
Nov 26, 2008 at 8:22 PM
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!!
QwertyManiac
Nov 27, 2008 at 1:04 AM
Obvious enough, a pre-processor trick. You're replacing begin with main using two clever words and a re-ordered decoding pre-processor definition.
M
Nov 27, 2008 at 1:57 AM
So you are Enjoying learning in a Funny Way???

That's Great... keep it up ;)
MetalheadGautham
Nov 27, 2008 at 6:59 AM
ROFL... this is an old trick which often confuses some newbies. Its fun. :lol:
Plasma_Snake
Nov 27, 2008 at 7:39 AM
Care to explain me what's going on in the code.
Faun
Faun #6
Nov 27, 2008 at 9:45 AM
loludie plasmasnake !
Plasma_Snake
Nov 27, 2008 at 11:08 AM
Yaar, batata ho toh bata, yun mazaak naa uda.
M
Nov 27, 2008 at 11:27 AM
loludie :D
Faun
Faun #9
Nov 27, 2008 at 11:30 AM
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()
{

}
Plasma_Snake
Nov 27, 2008 at 11:30 AM
loludie loludie, kya bakwaas lagaa rakhi hai, batana hai to batayo nahin to rehene do!
M
mrintech #11
Nov 27, 2008 at 11:47 AM
loluangry :D
Abhishek Dwivedi
Nov 27, 2008 at 11:49 AM
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
M
mrintech #13
Nov 27, 2008 at 11:49 AM
loluteacher :D
garfield_56
Nov 27, 2008 at 2:43 PM
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
pushkar
pushkar #15
Nov 27, 2008 at 3:18 PM
nice trick.
Plasma_Snake
Nov 27, 2008 at 6:29 PM
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. ;)
red_devil
red_devil #18
Nov 28, 2008 at 7:47 PM
^ thats what happens in colleges. :rolleyes:
garfield_56
Nov 29, 2008 at 11:12 PM
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!!!!! :)
red_devil
red_devil #20
Nov 29, 2008 at 11:41 PM
^^ understand printf's signature and you're done !! :D

and this ones kinda too old...
QwertyManiac
Nov 30, 2008 at 12:28 AM
Here are some, and you can continue giving puzzles there:

Lesser known facts in C and Puzzles for C/C++
garfield_56
Dec 5, 2008 at 3:10 PM
now here's another one-----the program code is extremely :eek:elementary, so i'll just remind all d programmers out there, that this is JUST FOR FUN:):D;)!!!


The question goes like this:::


Code:
int i, n = 20; 
for (i=0; i<n; i--) 
{ 
cout << "x" << endl; 
} 
 
/*   by changing only ONE character in the above code, meaning you cannot change 20 to 31, because you will have changed two characters, you can change 20 to 21, because you only changed the 0, do the following: 
find 3 ways to make the above code print x 20 times (by changing only one character). */

See wat i meant,--this is more of a mental ability :!:question, than a programming one.....
Sykora
Sykora #23
Dec 5, 2008 at 3:30 PM
*www.spoj.pl/problems/BCEASY/

Change i-- to n--.
Quiz_Master
Dec 7, 2008 at 1:34 AM
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)



4.
int main()
{

}

Yeah, I remember having fun with same code back in 2nd semester! Even my teacher got fused. He said its fault of DevC++ (on which I was running the code) and that I should use TC++ ROFL :p
vamsi360
vamsi360 #25
Dec 13, 2008 at 8:50 PM
hehe teachers donot even teach logics we have to get them by ourselves. I was enough tired with my teachers explanations