如何用函数判断奇偶?解析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:
- 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 processeseven(), it hasn't seen any definition or declaration ofodd()yet—so it assumesodd()is an implicitly declared function (defaulting to returningint). While yourboolis an alias forint, this implicit declaration is invalid in modern C standards (C99+) and will throw a compile error. - No recursive termination condition
Even if the compiler didn't complain, this code would crash at runtime. The functions call each other infinitely, reducingnby 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
- Function Prototypes: The line
bool odd(int n);tells the compiler exactly whatodd()looks like beforeeven()tries to call it—no more implicit declaration errors. - Termination Conditions: When
nreaches 0, we return a concrete value:even(0)returnstrue(since 0 is even),odd(0)returnsfalse. This stops the recursion and lets the results bubble back up. - 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): Returnstrue(1) ifnis even,false(0) if it's odd. - Call
odd(n): Returnstrue(1) ifnis odd,false(0) if it's even.
For example, let's trace even(3):
even(3)callsodd(2)odd(2)callseven(1)even(1)callsodd(0)odd(0)returnsfalse, soeven(1)returnsfalseodd(2)returnsfalse, soeven(3)returnsfalse→ 3 is odd, which is correct.
内容的提问来源于stack exchange,提问作者user2939669




