We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi guys, I am a new bee in programming for ARM7. I have written the simple following program using for loop to find a factorial of a given number. However to check the result, I have kept the input value static (a = 4), for which factorial should be 24 but somehow the result in the dis assembly window and watch window is not as per expectations. Please help me out. Thank you. The source code is as below:
#include <stdio.h> #include <LPC21xx.h> int main() { int a=4,i,fact=a; // a is user defined though I've taken it as 4 right now, just to check fact value for (i=a;i<=1;i--) { fact = fact*(i-1); // fact should be 24 but at this breakpoint I am getting 4! } }
Magic question.
What's the difference between
int main() { int a=4,i,fact=a; for (i=a; i<=1; i--) { fact = fact*(i-1); } }
and
int main() { int a=4,i,fact=a; for (i=a; i>1; i--) { fact = fact*(i-1); } }
Dear Sir, Thank you for pointing out the logical error. I am feeling ashamed of my self for such a silly mistake! And yes optimization issue wasn't there. Thanks a lot for your help. Regards, Ankur Gajjar