You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何用函数判断奇偶?解析C语言互调奇偶函数的错误与修正

Alright, let's break this down step by step, just like we'd hash it out on a dev forum.

问题1:代码的定义错误原因

First, let's look at your original code:

typedef int bool;
bool even(int n) { return odd(n-1); }
bool odd(int n) { return even(n-1); }

There are two critical issues here:

  1. Missing function prototypes
    In C, when you call a function, the compiler needs to know what that function looks like (its return type and parameter types) before it encounters the call. When the compiler processes even(), it hasn't seen any definition or declaration of odd() yet—so it assumes odd() is an implicitly declared function (defaulting to returning int). While your bool is an alias for int, this implicit declaration is invalid in modern C standards (C99+) and will throw a compile error.
  2. No recursive termination condition
    Even if the compiler didn't complain, this code would crash at runtime. The functions call each other infinitely, reducing n by 1 every time until the program runs out of stack memory (stack overflow). There's no point where the recursion stops to return a concrete value.

问题2:修正代码 + 使用说明

To fix this, we need to add function prototypes so the compiler recognizes cross-calls, and add termination conditions to end the recursion properly.

Fixed Code

// Use standard bool if you're on C99+ (recommended)
// #include <stdbool.h>
// Or stick with your custom bool definition:
typedef int bool;
#define true 1
#define false 0

// Declare the odd() prototype first, so even() knows it exists
bool odd(int n);

bool even(int n) {
    // Termination case: 0 is even
    if (n == 0) {
        return true;
    }
    // Handle negative numbers (parity doesn't care about sign)
    if (n < 0) {
        return even(-n);
    }
    // Recursive call: even(n) = odd(n-1)
    return odd(n - 1);
}

bool odd(int n) {
    // Termination case: 0 is not odd
    if (n == 0) {
        return false;
    }
    // Handle negative numbers
    if (n < 0) {
        return odd(-n);
    }
    // Recursive call: odd(n) = even(n-1)
    return even(n - 1);
}

// Example usage
int main() {
    int test_num = 6;
    if (even(test_num)) {
        printf("%d is even\n", test_num);
    } else {
        printf("%d is odd\n", test_num);
    }

    test_num = -7;
    if (odd(test_num)) {
        printf("%d is odd\n", test_num);
    } else {
        printf("%d is even\n", test_num);
    }
    return 0;
}

How It Works

  1. Function Prototypes: The line bool odd(int n); tells the compiler exactly what odd() looks like before even() tries to call it—no more implicit declaration errors.
  2. Termination Conditions: When n reaches 0, we return a concrete value: even(0) returns true (since 0 is even), odd(0) returns false. This stops the recursion and lets the results bubble back up.
  3. Negative Number Handling: We flip negative numbers to positive because parity (even/odd) doesn't depend on the sign of the number.

Using the Functions

  • Call even(n): Returns true (1) if n is even, false (0) if it's odd.
  • Call odd(n): Returns true (1) if n is odd, false (0) if it's even.

For example, let's trace even(3):

  • even(3) calls odd(2)
  • odd(2) calls even(1)
  • even(1) calls odd(0)
  • odd(0) returns false, so even(1) returns false
  • odd(2) returns false, so even(3) returns false → 3 is odd, which is correct.

内容的提问来源于stack exchange,提问作者user2939669

火山引擎 最新活动