简体   繁体   中英

Why the printf statement inside the fun(int i) is not executed?

This is a code that has a recursive loop. I got the output as 199, but the printf statement marked as --A is not executed. Please tell me why??

#include <stdio.h>

int fun(int); // function defined

int main()
{
    printf(" %d ", fun(200)); // function called
    return 0;
}

int fun(int i) // entire body of the function
{
    static int d = 0;
    d++;

    if (i % 2)
        return (i++);
    else 
        return fun(fun(i - 1));

    printf("%d ", d);// ------A

} // function fun(int) ends here.

From :

if ( i%2 ) return (i++);
else return fun(fun( i - 1 ));

Both parts are returning if the function converges. So printf is unreachable code.

因为return终止当前功能。

return returns control of the program to the calling function. So nothing after return is called inside a function is executed. If you want the printf to be called, put it before return .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM