 |
26-11-2008, 08:22 PM
|
#1 (permalink)
|
|
zzzzzzzzzz........
Join Date: Nov 2008
Posts: 41
|
C++--tricky one
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 by garfield_56; 26-11-2008 at 08:28 PM.
|
|
|
|
Advertisements. Register and be a member of the community to get rid of them.
|
|
Advertisement
|
|
27-11-2008, 01:04 AM
|
#2 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: C++--tricky one
Obvious enough, a pre-processor trick. You're replacing begin with main using two clever words and a re-ordered decoding pre-processor definition.
__________________
Harsh J
www.harshj.com
|
|
|
27-11-2008, 01:57 AM
|
#3 (permalink)
|
|
Wise Old Mouse
Join Date: Sep 2005
Location: Bhopal, India
Posts: 1,930
|
Re: C++--tricky one
So you are Enjoying learning in a Funny Way???
That's Great... keep it up
|
|
|
27-11-2008, 06:59 AM
|
#4 (permalink)
|
|
The Smaller Bang
Join Date: Sep 2007
Location: Gautham City
Posts: 7,492
|
Re: C++--tricky one
ROFL... this is an old trick which often confuses some newbies. Its fun.
__________________
http://TheSmallerBang.wordpress.com
eMachines E725 - T4400 2.2GHz, 1GB, 160GB
Nokia 5130XM * T-Sonic 610 2GB
Nokia 2323C * Samsung Galaxy Y
Apple iPad 2 16GB WiFi
|
|
|
27-11-2008, 07:39 AM
|
#5 (permalink)
|
|
Indidiot
Join Date: Dec 2007
Location: Rock Island
Posts: 1,416
|
Re: C++--tricky one
Care to explain me what's going on in the code.
|
|
|
27-11-2008, 09:45 AM
|
#6 (permalink)
|
|
Wahahaha~!
Join Date: Dec 2006
Location: Pune/there
Posts: 7,676
|
Re: C++--tricky one
loludie plasmasnake !
|
|
|
27-11-2008, 11:08 AM
|
#7 (permalink)
|
|
Indidiot
Join Date: Dec 2007
Location: Rock Island
Posts: 1,416
|
Re: C++--tricky one
Yaar, batata ho toh bata, yun mazaak naa uda.
|
|
|
27-11-2008, 11:27 AM
|
#8 (permalink)
|
|
Wise Old Mouse
Join Date: Sep 2005
Location: Bhopal, India
Posts: 1,930
|
Re: C++--tricky one
loludie
|
|
|
27-11-2008, 11:30 AM
|
#9 (permalink)
|
|
Wahahaha~!
Join Date: Dec 2006
Location: Pune/there
Posts: 7,676
|
Re: C++--tricky one
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)
Quote:
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()
{
}
|
|
|
27-11-2008, 11:30 AM
|
#10 (permalink)
|
|
Indidiot
Join Date: Dec 2007
Location: Rock Island
Posts: 1,416
|
Re: C++--tricky one
loludie loludie, kya bakwaas lagaa rakhi hai, batana hai to batayo nahin to rehene do!
|
|
|
27-11-2008, 11:47 AM
|
#11 (permalink)
|
|
Wise Old Mouse
Join Date: Sep 2005
Location: Bhopal, India
Posts: 1,930
|
Re: C++--tricky one
loluangry
|
|
|
27-11-2008, 11:49 AM
|
#12 (permalink)
|
|
TechFreakiez.com
Join Date: Sep 2006
Location: New Delhi
Posts: 621
|
Re: C++--tricky one
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
__________________
Personal Log | Star date 05.04.2009: TDF Meet Kanpur was Awesome :D
www.TechFreakiez.com
|
|
|
27-11-2008, 11:49 AM
|
#13 (permalink)
|
|
Wise Old Mouse
Join Date: Sep 2005
Location: Bhopal, India
Posts: 1,930
|
Re: C++--tricky one
loluteacher
|
|
|
27-11-2008, 02:43 PM
|
#14 (permalink)
|
|
zzzzzzzzzz........
Join Date: Nov 2008
Posts: 41
|
Re: C++--tricky one
Quote:
Originally Posted by Plasma_Snake
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()" 
Hope its clear now!!!!
__________________________________________________ ___________
Quote:
Originally Posted by Abhishek Dwivedi
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??   
Last edited by garfield_56; 27-11-2008 at 02:48 PM.
Reason: Automerged Doublepost
|
|
|
27-11-2008, 03:18 PM
|
#15 (permalink)
|
|
Broken In
Join Date: May 2006
Posts: 196
|
Re: C++--tricky one
nice trick.
|
|
|
27-11-2008, 06:29 PM
|
#16 (permalink)
|
|
Indidiot
Join Date: Dec 2007
Location: Rock Island
Posts: 1,416
|
Re: C++--tricky one
Quote:
Originally Posted by garfield_56
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()" 
Hope its clear now!!!!
__________________________________________________ ___________
By d way...did ur teacher get d trick or not??    
|
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.
|
|
|
28-11-2008, 03:43 PM
|
#17 (permalink)
|
|
TechFreakiez.com
Join Date: Sep 2006
Location: New Delhi
Posts: 621
|
Re: C++--tricky one
Quote:
Originally Posted by garfield_56
|
nopes...he got pissed...and gave me a 0 for so-called indiscipline...hahahaha
__________________
Personal Log | Star date 05.04.2009: TDF Meet Kanpur was Awesome :D
www.TechFreakiez.com
|
|
|
28-11-2008, 07:47 PM
|
#18 (permalink)
|
|
Back!
Join Date: Jun 2007
Location: Bangalore
Posts: 513
|
Re: C++--tricky one
^ thats what happens in colleges.
|
|
|
29-11-2008, 11:12 PM
|
#19 (permalink)
|
|
zzzzzzzzzz........
Join Date: Nov 2008
Posts: 41
|
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 by garfield_56; 29-11-2008 at 11:14 PM.
Reason: Automerged Doublepost
|
|
|
29-11-2008, 11:41 PM
|
#20 (permalink)
|
|
Back!
Join Date: Jun 2007
Location: Bangalore
Posts: 513
|
Re: C++--tricky one
^^ understand printf's signature and you're done !!
and this ones kinda too old...
|
|
|
05-12-2008, 03:10 PM
|
#22 (permalink)
|
|
zzzzzzzzzz........
Join Date: Nov 2008
Posts: 41
|
Re: C++--tricky one
now here's another one-----the program code is extremely  elementary, so i'll just remind all d programmers out there, that this is JUST FOR FUN    !!!
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.....
|
|
|
05-12-2008, 03:30 PM
|
#23 (permalink)
|
|
I see right through you.
Join Date: Sep 2005
Location: Chennai
Posts: 597
|
Re: C++--tricky one
__________________
I didn't make the world, I only try to live in it.
http://lucentbeing.com
-- Sykora --
|
|
|
07-12-2008, 01:34 AM
|
#24 (permalink)
|
|
* Teh Flirt King *
Join Date: Dec 2005
Location: Originally From : Ratlam M.P., Currently in: Hyderabad
Posts: 972
|
Re: C++--tricky one
Quote:
Originally Posted by T159
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
__________________
World is just a Quizzical Reality : Quiz_Master//Ashwin :D
Blog: http://ashwinsaxena.com/blog - Tech, Life and Other Things.
|
|
|
13-12-2008, 08:50 PM
|
#25 (permalink)
|
|
Always confused
Join Date: May 2008
Location: Mandriva Control Center
Posts: 349
|
Re: C++--tricky one
hehe teachers donot even teach logics we have to get them by ourselves. I was enough tired with my teachers explanations
__________________
Vamsi Subhash
visit my blog at www.vamsisubhash.co.cc and taste a bit of IT!
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|