#include <stdio.h>
int digit_to_num(char c){
int sum=0, digit;
if(c>='0'&&c<='9'){
digit = c-'0';
sum= sum+digit;
}
return sum;
}
int main(void) {
int c;
while ((c = getchar()) != EOF && c != '.') {
printf("%d", digit_to_num(c));
return 0;
}
This code reads characters from input and for each digit character, it converts it to its numeric value and prints that value. However, the program stops after processing only the first character because `return 0` is inside the loop.
- The `return 0` statement inside the `while` loop causes the program to exit immediately after processing the first character. Consider where the program should end instead.
- The `digit_to_num` function adds each digit to a `sum` variable, but it always returns only the last digit's value because `sum` is reset each time the function is called. Think about what the function should actually return for a single digit.