递归C函数 - 打印升序

Pro*_*k3u 0 c recursion loops

我正在尝试实现一个递归调用自身并按升序打印给定数字的代码,即如果数字是5,那么函数将打印1 2 3 4 5.我不能以任何方式使用循环!

void print_ascending(int n)
{
   int i = 1;

   if(i < n)
   {
      printf("%d", i);

      i++;

      print_ascending(n);
   }
}
Run Code Online (Sandbox Code Playgroud)

当然,这段代码的问题是它会每次将变量i重新初始化为1并无限循环以打印1.

也没有允许外部全局变量或外部函数!

niy*_*asc 5

每次调用递归函数时,请尝试递增参数值.

void print_ascending(int limit, int current_value)
{
   if(current_value < limt)
   {
     printf("%d ", current_value);
     print_ascending(limit, current_value + 1);
   }
}
Run Code Online (Sandbox Code Playgroud)

最初将函数调用为 print_ascending(5, 1)

或者,

void print_ascending(int n)
{
    if(n > 0)
    {
        print_ascending( n - 1);
        printf("%d ", n); 
    }
}
Run Code Online (Sandbox Code Playgroud)