Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 26-11-2008, 08:22 PM   #1 (permalink)
zzzzzzzzzz........
 
garfield_56's Avatar
 
Join Date: Nov 2008
Posts: 41
Talking 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.
garfield_56 is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 27-11-2008, 01:04 AM   #2 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default 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
QwertyManiac is offline  
Old 27-11-2008, 01:57 AM   #3 (permalink)
Wise Old Mouse
 
mrintech's Avatar
 
Join Date: Sep 2005
Location: Bhopal, India
Posts: 1,930
Default Re: C++--tricky one

So you are Enjoying learning in a Funny Way???

That's Great... keep it up
__________________
- MrinTech :)
mrintech is offline  
Old 27-11-2008, 06:59 AM   #4 (permalink)
The Smaller Bang
 
MetalheadGautham's Avatar
 
Join Date: Sep 2007
Location: Gautham City
Posts: 7,492
Default 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
MetalheadGautham is online now  
Old 27-11-2008, 07:39 AM   #5 (permalink)
Indidiot
 
Plasma_Snake's Avatar
 
Join Date: Dec 2007
Location: Rock Island
Posts: 1,416
Default Re: C++--tricky one

Care to explain me what's going on in the code.
Plasma_Snake is offline  
Old 27-11-2008, 09:45 AM   #6 (permalink)
Wahahaha~!
 
Faun's Avatar
 
Join Date: Dec 2006
Location: Pune/there
Posts: 7,676
Default Re: C++--tricky one

loludie plasmasnake !
__________________
Blog | Flickr | Battlelog
Spoiler:
Asus Z68 V-Pro|i5 2500k|TRUE Black|Ripjaws X|U2311H|N560GTX|D7000|XONAR STX|RE272|RE0|CC51|XE200PRO Walnut| TD II V2| Ultraphile|N5800

Mono
Faun is offline  
Old 27-11-2008, 11:08 AM   #7 (permalink)
Indidiot
 
Plasma_Snake's Avatar
 
Join Date: Dec 2007
Location: Rock Island
Posts: 1,416
Default Re: C++--tricky one

Yaar, batata ho toh bata, yun mazaak naa uda.
Plasma_Snake is offline  
Old 27-11-2008, 11:27 AM   #8 (permalink)
Wise Old Mouse
 
mrintech's Avatar
 
Join Date: Sep 2005
Location: Bhopal, India
Posts: 1,930
Default Re: C++--tricky one

loludie
__________________
- MrinTech :)
mrintech is offline  
Old 27-11-2008, 11:30 AM   #9 (permalink)
Wahahaha~!
 
Faun's Avatar
 
Join Date: Dec 2006
Location: Pune/there
Posts: 7,676
Default 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()
{

}
__________________
Blog | Flickr | Battlelog
Spoiler:
Asus Z68 V-Pro|i5 2500k|TRUE Black|Ripjaws X|U2311H|N560GTX|D7000|XONAR STX|RE272|RE0|CC51|XE200PRO Walnut| TD II V2| Ultraphile|N5800

Mono
Faun is offline  
Old 27-11-2008, 11:30 AM   #10 (permalink)
Indidiot
 
Plasma_Snake's Avatar
 
Join Date: Dec 2007
Location: Rock Island
Posts: 1,416
Default Re: C++--tricky one

loludie loludie, kya bakwaas lagaa rakhi hai, batana hai to batayo nahin to rehene do!
Plasma_Snake is offline  
Old 27-11-2008, 11:47 AM   #11 (permalink)
Wise Old Mouse
 
mrintech's Avatar
 
Join Date: Sep 2005
Location: Bhopal, India
Posts: 1,930
Default Re: C++--tricky one

loluangry
__________________
- MrinTech :)
mrintech is offline  
Old 27-11-2008, 11:49 AM   #12 (permalink)
TechFreakiez.com
 
Abhishek Dwivedi's Avatar
 
Join Date: Sep 2006
Location: New Delhi
Posts: 621
Default 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
Abhishek Dwivedi is offline  
Old 27-11-2008, 11:49 AM   #13 (permalink)
Wise Old Mouse
 
mrintech's Avatar
 
Join Date: Sep 2005
Location: Bhopal, India
Posts: 1,930
Default Re: C++--tricky one

loluteacher
__________________
- MrinTech :)
mrintech is offline  
Old 27-11-2008, 02:43 PM   #14 (permalink)
zzzzzzzzzz........
 
garfield_56's Avatar
 
Join Date: Nov 2008
Posts: 41
Cool Re: C++--tricky one

Quote:
Originally Posted by Plasma_Snake View Post
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 View Post
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
garfield_56 is offline  
Old 27-11-2008, 03:18 PM   #15 (permalink)
Broken In
 
pushkar's Avatar
 
Join Date: May 2006
Posts: 196
Default Re: C++--tricky one

nice trick.
pushkar is offline  
Old 27-11-2008, 06:29 PM   #16 (permalink)
Indidiot
 
Plasma_Snake's Avatar
 
Join Date: Dec 2007
Location: Rock Island
Posts: 1,416
Default Re: C++--tricky one

Quote:
Originally Posted by garfield_56 View Post
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.
Plasma_Snake is offline  
Old 28-11-2008, 03:43 PM   #17 (permalink)
TechFreakiez.com
 
Abhishek Dwivedi's Avatar
 
Join Date: Sep 2006
Location: New Delhi
Posts: 621
Default Re: C++--tricky one

Quote:
Originally Posted by garfield_56 View Post
By d way...did ur teacher get d trick or not??
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
Abhishek Dwivedi is offline  
Old 28-11-2008, 07:47 PM   #18 (permalink)
Back!
 
red_devil's Avatar
 
Join Date: Jun 2007
Location: Bangalore
Posts: 513
Default Re: C++--tricky one

^ thats what happens in colleges.
red_devil is offline  
Old 29-11-2008, 11:12 PM   #19 (permalink)
zzzzzzzzzz........
 
garfield_56's Avatar
 
Join Date: Nov 2008
Posts: 41
Default 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
garfield_56 is offline  
Old 29-11-2008, 11:41 PM   #20 (permalink)
Back!
 
red_devil's Avatar
 
Join Date: Jun 2007
Location: Bangalore
Posts: 513
Default Re: C++--tricky one

^^ understand printf's signature and you're done !!

and this ones kinda too old...
red_devil is offline  
Old 30-11-2008, 12:28 AM   #21 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: C++--tricky one

Here are some, and you can continue giving puzzles there:

Lesser known facts in C and Puzzles for C/C++
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 05-12-2008, 03:10 PM   #22 (permalink)
zzzzzzzzzz........
 
garfield_56's Avatar
 
Join Date: Nov 2008
Posts: 41
Cool 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.....
garfield_56 is offline  
Old 05-12-2008, 03:30 PM   #23 (permalink)
I see right through you.
 
Sykora's Avatar
 
Join Date: Sep 2005
Location: Chennai
Posts: 597
Default Re: C++--tricky one

https://www.spoj.pl/problems/BCEASY/

Change i-- to n--.
__________________
I didn't make the world, I only try to live in it.
http://lucentbeing.com
-- Sykora --
Sykora is offline  
Old 07-12-2008, 01:34 AM   #24 (permalink)
* Teh Flirt King *
 
Quiz_Master's Avatar
 
Join Date: Dec 2005
Location: Originally From : Ratlam M.P., Currently in: Hyderabad
Posts: 972
Default Re: C++--tricky one

Quote:
Originally Posted by T159 View Post
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.
Quiz_Master is offline  
Old 13-12-2008, 08:50 PM   #25 (permalink)
Always confused
 
vamsi360's Avatar
 
Join Date: May 2008
Location: Mandriva Control Center
Posts: 349
Default 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!
vamsi360 is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
a tricky problem... dtox Hardware Q&A 2 02-08-2007 08:33 AM
Tricky One !!! bajaj151 QnA (read only) 8 21-07-2007 06:51 PM
Tricky Folder mohanty1942 QnA (read only) 6 07-10-2006 02:21 PM
tricky ques ravix Open Source 5 02-07-2006 01:42 PM
Tricky problem: HDD issue? SpitefulPentium QnA (read only) 8 12-01-2005 09:34 AM

 
Latest Threads
- by gforz
- by soumya
- by Sujeet
- by icebags
- by Charan

Advertisement




All times are GMT +5.5. The time now is 03:01 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2