# C++--tricky one



## garfield_56 (Nov 26, 2008)

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()*

```
#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)

Obvious enough, a pre-processor trick. You're replacing _begin_ with _main_ using two clever words and a re-ordered decoding pre-processor definition.


----------



## mrintech (Nov 27, 2008)

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

That's Great... keep it up


----------



## MetalheadGautham (Nov 27, 2008)

ROFL... this is an old trick which often confuses some newbies. Its fun.


----------



## Plasma_Snake (Nov 27, 2008)

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


----------



## Faun (Nov 27, 2008)

loludie plasmasnake !


----------



## Plasma_Snake (Nov 27, 2008)

Yaar, batata ho toh bata, yun mazaak naa uda.


----------



## mrintech (Nov 27, 2008)

loludie


----------



## Faun (Nov 27, 2008)

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
> ...



4.
int main()
{

}


----------



## Plasma_Snake (Nov 27, 2008)

loludie loludie, kya bakwaas lagaa rakhi hai, batana hai to batayo nahin to rehene do!


----------



## mrintech (Nov 27, 2008)

loluangry


----------



## Abhishek Dwivedi (Nov 27, 2008)

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


----------



## mrintech (Nov 27, 2008)

loluteacher


----------



## garfield_56 (Nov 27, 2008)

Plasma_Snake said:


> 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:::


```
#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:: 



```
#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!!!!






_____________________________________________________________


Abhishek Dwivedi said:


> 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??


----------



## pushkar (Nov 27, 2008)

nice trick.


----------



## Plasma_Snake (Nov 27, 2008)

garfield_56 said:


> 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:::
> ...



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.


----------



## Abhishek Dwivedi (Nov 28, 2008)

garfield_56 said:


> 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


----------



## red_devil (Nov 28, 2008)

^ thats what happens in colleges.


----------



## garfield_56 (Nov 29, 2008)

*Another One!!!!!*

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



*program without semicolon!*


```
#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 (Nov 29, 2008)

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

and this ones kinda too old...


----------



## QwertyManiac (Nov 30, 2008)

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)

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:::



```
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 (Dec 5, 2008)

*www.spoj.pl/problems/BCEASY/

Change i-- to n--.


----------



## Quiz_Master (Dec 7, 2008)

T159 said:


> 1.
> int begin()
> {
> 
> ...



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


----------



## vamsi360 (Dec 13, 2008)

hehe teachers donot even teach logics we have to get them by ourselves. I was enough tired with my teachers explanations


----------

