1

我写了一个基本的递归代码,但发生了错误:

重新定义;不同的基本类型

这是代码:

int m=1; 
void countDown(int n) 
{ /* count down */ 
    printf("Count down: %d\t", n--); 
    if(n<1) return; /* terminate recursion */ 
    else countUP(n); /* start/continue indirect recursion */ 
} 

void countUP(int n) 
{ 
    printf("up: %d\n", m++); 
    countDown(n); 
    /* indirect recursion */ 
}

void main()
{

    countDown(5);
    return;
}
4

1 回答 1

4

countDown中,您使用了countUP迄今为止尚未声明的函数。根据C89的 §3.3.2.2 ,当一个函数在使用时未声明时,它被隐式声明为

extern int countUP();

稍后,您实际上声明(并实现)countUP,但是 as void countUP(int n),它与上述签名不匹配。

countUP通过添加声明

void countUP(int n);

之前 countDown。当你在做的时候,你也应该

#include <stdio.h>

printf.

于 2013-05-22T18:59:57.493 回答