c - 如何仅在递归函数调用中第一次调用另一个函数?

标签 c

我想调用“function2();”仅在第一次调用 recurfunc() 时单次。

recurfunc()
{
    function2();     //to be called first time only in a recursive function
    static int i= 0;
    i++;

    if(i>20)
        return;
    else
    recurfunc();
}

最佳答案

对您的代码进行微不足道的更改:

void recurfunc()         // * Remember to specify return type *
{
    static int i= 0;

    if (i==0)
        function2();     // called only the first time.

    i++;

    if(i>20)
        return;
    else
        recurfunc();
}

关于c - 如何仅在递归函数调用中第一次调用另一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15780212/

相关文章:

c - 警告 : large integer implicitly truncated to unsigned type [-Woverflow]

c - 在 C 中返回字符串数组 (char*)

c++ - OpenGL 三角形邻接索引

c - 变异数组中最小值及其偏移量的数据结构

c - 普通 C - 函数忽略第二个 fgets

c++ - 轴对齐矩形测试中的点?

c - C 中的数据类型混淆

c - 循环运行的程序意外终止

objective-c - 防止将 NSDecimalNumber 输出为 NSString 的科学记数法

c - 有没有办法编写一个带有变量参数的 C 预处理器宏?