HTF? :mad:
-
How the f***:omg::wtf: X| X| X| this is sickening.
void fun(int);
void main()
{
int a ;
a=3;
fun(a);
}void fun(int n)
{
if(n==0)//orif(n==-1)
exit(0);<--If I put this the ouput is nothing, but if I remove it, the idiot compiler is bypassing the condition (0>0),(-1>0).
if(n>0)//3,2,1,0,-1
{
printf("\nTHERE!");
fun(--n);
printf("\n%d",n); //HOW THE **** the control comes here?!!!?? :((
fun(--n);////HOW THE **** the control comes here?!!!?? :((} else { printf("\\nHERE!"); }
}
How 0,-1 can be > than 0? shit. I'm not on drugs! but :wtf: mark,led,jeron,toxcct,david someone help! help! without exit(0),it prints
0
1
2
0---------------------------- 286? WOWW!:-O
-
How the f***:omg::wtf: X| X| X| this is sickening.
void fun(int);
void main()
{
int a ;
a=3;
fun(a);
}void fun(int n)
{
if(n==0)//orif(n==-1)
exit(0);<--If I put this the ouput is nothing, but if I remove it, the idiot compiler is bypassing the condition (0>0),(-1>0).
if(n>0)//3,2,1,0,-1
{
printf("\nTHERE!");
fun(--n);
printf("\n%d",n); //HOW THE **** the control comes here?!!!?? :((
fun(--n);////HOW THE **** the control comes here?!!!?? :((} else { printf("\\nHERE!"); }
}
How 0,-1 can be > than 0? shit. I'm not on drugs! but :wtf: mark,led,jeron,toxcct,david someone help! help! without exit(0),it prints
0
1
2
0---------------------------- 286? WOWW!:-O
-
First off, what is the output you are expecting? With the recursive calls you are making the output you are getting makes perfect sense.
You may be right
I may be crazy
-- Billy Joel --Within you lies the power for good, use it!!!
Thanks Arends, but why does it bypass this condtion when n =0, or -1
if(n>0) //(0>0) (-1>0) [after successive calls to --n & fun(n)] { but still it comes here! why? }
I still can't get that point. Recursion makes multiple copies of the function? may be I'm missing this? it gets into the stack everytime we call it, so the context of "n" changes? I can somehow come around the point but still need some help to understand it. Please help dude. -- modified at 14:06 Saturday 31st March, 2007---------------------------- 286? WOWW!:-O
-
Thanks Arends, but why does it bypass this condtion when n =0, or -1
if(n>0) //(0>0) (-1>0) [after successive calls to --n & fun(n)] { but still it comes here! why? }
I still can't get that point. Recursion makes multiple copies of the function? may be I'm missing this? it gets into the stack everytime we call it, so the context of "n" changes? I can somehow come around the point but still need some help to understand it. Please help dude. -- modified at 14:06 Saturday 31st March, 2007---------------------------- 286? WOWW!:-O
_8086 wrote:
but why does it bypass this condtion when n =0, or -1
It does not, the recursive function calls stop when
n
is zero or less. I think your confusion is coming from whatn
is. You have to remember that a newn
is created every timefun()
is called, you are not using a singlen
. I cleaned up your sample a bit, so lets step through it:void fun(int n) // start with n = 3
{
if (n > 0) // n is greater than zero
{
fun(--n); // n is now two, recursive call to fun(2)+void fun(int n) // start with n = 2 |{ | if (n > 0) // n is greater than zero | { | fun(--n) // n is now one, another recursive call to fun(1) | | +void fun(int n) // start with n = 1 | |{ | | if (n > 0) // n is greater than zero | | { | | fun(--n) // n is now zero, another recursive call to fun(0) | | | | +void fun(int n) // start with n = 0; | | |{ | | | if (n > 0) // n is not greater than zero | | | { | | | fun(--n) // not called | | | printf("%d\\n", n); | | | } | | +} // exit fun() | | | | printf("%d\\n", n); // n is zero - output "0" | | } | +} // exit fun() | | printf("%d\\n", n); // n is one - output "1" | } +} // exit fun() printf("%d\\n", n); // n is two - output "2"
}
} // exit fun()and repeat ad infinitum.
You may be right
I may be crazy
-- Billy Joel --Within you lies the power for good, use it!!!
-
Thanks Arends, but why does it bypass this condtion when n =0, or -1
if(n>0) //(0>0) (-1>0) [after successive calls to --n & fun(n)] { but still it comes here! why? }
I still can't get that point. Recursion makes multiple copies of the function? may be I'm missing this? it gets into the stack everytime we call it, so the context of "n" changes? I can somehow come around the point but still need some help to understand it. Please help dude. -- modified at 14:06 Saturday 31st March, 2007---------------------------- 286? WOWW!:-O
Of course the compiler is working fine. Please follow me in code inspection, to make analysis shorter, let's start with
fun(2)
:fun(2){
if(2>0){
fun(1);At the moment (no output yet produced) we have to stop considering
fun(2)
and, due to recursion, procede with the inspection offun(1)
:fun(1){
if(1>0){
fun(0);Again (no output yet), we have to suspend considering
fun(1)
and go deeper in recursion withfun(0)
:fun(0){
if(0>0){Here, the compiler, that is a honest guy, correctly evaluates
(0>0)
asfalse
and the function returns. Have we done? No, of course, there are (in the order)fun(1)
andfun(2)
waiting on the stack. So let's go back tofun(1)
and reprise whereever we suspended:printf("\\n%d",0); fun(-1);
}
As you can see, this is the first time we have a number on the console, and the number is
0
(you can also see thatfun(-1)
is called, but it will do nothing). Of course we could go on with code inspection, but I think it's enough: we have thezero
, and we also have the demonstration that the compiler is not insane. hope that: (1) the analysis is correct. (2) it helps. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
_8086 wrote:
but why does it bypass this condtion when n =0, or -1
It does not, the recursive function calls stop when
n
is zero or less. I think your confusion is coming from whatn
is. You have to remember that a newn
is created every timefun()
is called, you are not using a singlen
. I cleaned up your sample a bit, so lets step through it:void fun(int n) // start with n = 3
{
if (n > 0) // n is greater than zero
{
fun(--n); // n is now two, recursive call to fun(2)+void fun(int n) // start with n = 2 |{ | if (n > 0) // n is greater than zero | { | fun(--n) // n is now one, another recursive call to fun(1) | | +void fun(int n) // start with n = 1 | |{ | | if (n > 0) // n is greater than zero | | { | | fun(--n) // n is now zero, another recursive call to fun(0) | | | | +void fun(int n) // start with n = 0; | | |{ | | | if (n > 0) // n is not greater than zero | | | { | | | fun(--n) // not called | | | printf("%d\\n", n); | | | } | | +} // exit fun() | | | | printf("%d\\n", n); // n is zero - output "0" | | } | +} // exit fun() | | printf("%d\\n", n); // n is one - output "1" | } +} // exit fun() printf("%d\\n", n); // n is two - output "2"
}
} // exit fun()and repeat ad infinitum.
You may be right
I may be crazy
-- Billy Joel --Within you lies the power for good, use it!!!
-
Of course the compiler is working fine. Please follow me in code inspection, to make analysis shorter, let's start with
fun(2)
:fun(2){
if(2>0){
fun(1);At the moment (no output yet produced) we have to stop considering
fun(2)
and, due to recursion, procede with the inspection offun(1)
:fun(1){
if(1>0){
fun(0);Again (no output yet), we have to suspend considering
fun(1)
and go deeper in recursion withfun(0)
:fun(0){
if(0>0){Here, the compiler, that is a honest guy, correctly evaluates
(0>0)
asfalse
and the function returns. Have we done? No, of course, there are (in the order)fun(1)
andfun(2)
waiting on the stack. So let's go back tofun(1)
and reprise whereever we suspended:printf("\\n%d",0); fun(-1);
}
As you can see, this is the first time we have a number on the console, and the number is
0
(you can also see thatfun(-1)
is called, but it will do nothing). Of course we could go on with code inspection, but I think it's enough: we have thezero
, and we also have the demonstration that the compiler is not insane. hope that: (1) the analysis is correct. (2) it helps. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.