HI friends I was experimenting with a common c factorial problem when I got a totally unexpected result.
here is the program
# include <stdio.h> main(){ int fact; int x; fact=factorial(6); printf ("%i\n",fff()); }
int factorial(int x){ if(x>1) return(x*factorial(x-1)); }
int fff () { ; }
Naturally I was expecting 1 as the answer. But I am getting the answer 720 which is 6!.
If I comment the 5th line fact=factorial(6); I am getting the expected result "1".
The normal explanation for this is memory leak. Can anyone please tell me what is happening and are you too getting the same result.
I am using redhat linux 7.3 with gcc 2.96
regards Abhijeet D Mhatre
On Dec 18, 2002 at 08:29, Abhijeet D Mhatre wrote:
Naturally I was expecting 1 as the answer. But I am getting the answer 720 which is 6!.
t.c:2: warning: return-type defaults to `int' t.c: In function `main': t.c:5: warning: implicit declaration of function `factorial' t.c:6: warning: implicit declaration of function `fff' t.c:4: warning: unused variable `x' t.c:7: warning: control reaches end of non-void function t.c: In function `factorial': t.c:12: warning: control reaches end of non-void function t.c: In function `fff': t.c:16: warning: control reaches end of non-void function [satyap]~$ ./a.out 720 [satyap]~$ gcc -v Reading specs from /usr/lib/gcc-lib/i386-linux/2.95.4/specs gcc version 2.95.4 20011002 (Debian prerelease) [satyap]~$
On Wed, 18 Dec 2002, Abhijeet D Mhatre wrote:
I was experimenting with a common c factorial problem when I got a totally unexpected result.
unexpected?
# include <stdio.h> main(){
return type defaults to int
int fact; int x; fact=factorial(6); printf ("%i\n",fff());
so where is the statement that returns an int?
}
int factorial(int x){ if(x>1) return(x*factorial(x-1));
what happens if x<=1? what is the return value then?
}
int fff () { ; }
and what is this supposed to return? the printf will print the return value of this function, but this function doesn't return anything.
Naturally I was expecting 1 as the answer. But I am getting the answer 720 which is 6!.
pray tell us why you expected this so naturally.
Philip
On Wed, 18 Dec 2002 08:29:38 +0530 Abhijeet D Mhatre wrote:
# include <stdio.h> main(){ int fact; int x; fact=factorial(6); printf ("%i\n",fff()); }
int factorial(int x){ if(x>1) return(x*factorial(x-1)); }
int fff () { ; }
Naturally I was expecting 1 as the answer. But I am getting the answer 720 which is 6!.
Why were you expecting 1? Could you really expect any particular value from a call to fff()?
Well i think this is related to stack and how the data is stored by the OS in stack.
I had put sleep before calling printf statement and the return result was 0. Or if we give a specific return then that value appears in the printf.
Well if anyone knows about this then do enlighten.
Ciao Tapesh
--- Tahir Hashmi code_martial@softhome.net wrote:
- LUG meet on 12 Jan. 2003 @ VJTI
On Wed, 18 Dec 2002 08:29:38 +0530 Abhijeet D Mhatre wrote:
# include <stdio.h> main(){ int fact; int x; fact=factorial(6); printf ("%i\n",fff()); }
int factorial(int x){ if(x>1) return(x*factorial(x-1)); }
int fff () { ; }
Naturally I was expecting 1 as the answer. But I
am getting the
answer 720 which is 6!.
Why were you expecting 1? Could you really expect any particular value from a call to fff()?
__________________________________________________ Do you Yahoo!? Yahoo! Mail Plus - Powerful. Affordable. Sign up now. http://mailplus.yahoo.com
On Tue, 17 Dec 2002 21:52:51 -0800 (PST) Tapeshwar Nath wrote:
Well i think this is related to stack and how the data is stored by the OS in stack.
The return value for that function would be undefined. It would vary with different combinations of operating systems and compilers. Yes, it's got to do with how the compiler would manage the stack and what policies it has for otherwise undefined behavior. I got 0 with gcc 3.2, sleep() or no sleep().
the return result was 0. Or if we give a specific return then that value appears in the printf.
Of course, it would.
A quick look for functions and return values through _The_C_Programming_Language_ 2/e by Kernighan and Ritchie should leave no scope for doubts.
On Wed, Dec 18, 2002 at 08:29:38AM +0530, Abhijeet D Mhatre wrote:
- LUG meet on 12 Jan. 2003 @ VJTI
int fact; int x; fact=factorial(6); printf ("%i\n",fff()); }
int factorial(int x){ if(x>1) return(x*factorial(x-1)); }
int fff () { ; }
Something very strage, I did the following (inserted wait(1), thats it)
# include <stdio.h> main(){ int fact; int x; fact=factorial(6); wait(1); printf ("%i\n",fff()); } int factorial(int x){ if(x>1) return(x*factorial(x-1)); } int fff () { ; }
And the output I get is (-1).
int fact; int x; fact=factorial(6); printf ("%i\n",fff()); }
int factorial(int x){ if(x>1) return(x*factorial(x-1)); }
int fff () { ; }
OK I give up, what do you mean by the fff() function?
Something very strage, I did the following (inserted wait(1), thats it)
# include <stdio.h> main(){ int fact; int x; fact=factorial(6); wait(1); printf ("%i\n",fff()); }
You've defined fff() as int, and it _doesn't_ return anything, plus you are printing the value it returns (the printf statement)!
If you are really interested in the factorial of 6, I guess you should be printing the return value of factorial(6).
That is your printf statement should look something like this:
printf("%d\n", factorial(6)); or simply printf("%d\n", fact);
Also, the int x declaration in main() is superfluous - you can do without it; and as a good programming practice never choose variable names that collide. Your main() has an x, and so does your factorial() - they are different and don't mean the same x.
Warm wishes,
Amol Hatwar
Abhijeet D Mhatre abhijeetmhatre@hotpop.com writes:
- LUG meet on 12 Jan. 2003 @ VJTI
HI friends I was experimenting with a common c factorial problem when I got a totally unexpected result.
here is the program
# include <stdio.h> main(){ int fact; int x; fact=factorial(6); printf ("%i\n",fff()); }
int factorial(int x){ if(x>1) return(x*factorial(x-1)); }
int fff () { ; }
Naturally I was expecting 1 as the answer. But I am getting the answer 720 which is 6!.
The function fff() is supposed to return an integer according to definition. But it does not. The return of the previous function which is on the stack is taken instead. If you insert a 'return int;' then the value of int is printed.
A good experiment anyway. This is the reason why I love C and so many hate it. '-)
If I comment the 5th line fact=factorial(6); I am getting the expected result "1".
But I am wondering why it should print "1"... & why is it expected.
quasi