Problem understanding function in C
hello,
I have problem with a function declaration, in following program the main section initializes k to 0. The function f is known as k <4. As increments each time, this function will be performed 4 times. My only problem is that I have a printf ( "% ld", i) at the beginning of the loop and the PC gives me the value 0 so the loop should never execute (j <= i)!. Yet it runs 4 times.
Code:
# include <stdio.h>
# include <stdlib.h>
float f (int i)
(
int j = 1, k = 1;
while (j <= i)
(
k = k * j;
j + +;
)
return k;
)
int main (int argc, char * argv)
(Int i;
int k = 0;
do
(
printf ( "f (% i) =% f \ n", k, f (k));
k + +;
)
while (k <4);
system ( "PAUSE");
return 0;
)
Re: Problem understanding function in C
Code:
printf ( "f (% i) =% f \ n", k, f (k));
or it Should be:
Code:
printf ( "f (% Id) =% Id \ n", i, k, f (k));
Re: Problem understanding function in C
Quote:
The function f is known as k <4. As increments each time, this function will be performed 4 times. My only problem is that I have a printf ( "% ld", i) at the beginning of the loop and the PC gives me the value 0 so the loop should never execute (j <= i)!. Yet it runs 4 times.
your function does not return a decimal number in integer that should be used, especially as you refer k, which is an integer variable. For your function by itself, at first sight of the factorial function.