你这里ch被覆盖了。
可以参考如下:
int main() { int n = 0; char ch; while ((ch = getchar()) != '\n') { n = n * 10 + (ch - '0'); } printf("%d\n", n); return 0; }