在线编译器 C

#include <stdio.h> int digit_sum(unsigned int n) { int sum = 0; if (n == 0) { return 0; } while (n > 0) { sum = sum + (n % 10); n = n / 10; } return sum; }
This code defines a function that calculates the sum of the digits of a non-negative integer. It repeatedly extracts the last digit using modulo 10, adds it to a running total, and removes that digit by dividing by 10.

- The function returns 0 for input 0, but the while loop would also handle that case correctly since the condition `n > 0` would be false. Consider whether the initial `if` check is necessary or if it creates any redundancy.
- The function uses `int` for the sum, but the input is `unsigned int`. Think about what happens if the digit sum exceeds the maximum value of an `int` for very large numbers.