c语言输入字符串转化为数字

/*
输入一个以回车结束的字符串,它由数字字符组成,将该字符串转换成整数后输出。
**输入提示信息:"Enter a string: "
**输出格式要求:"digit = %d "
*/

#include <stdio.h>

int main()
{
   unsigned long n = 0;
   char c;
   printf("Enter a string: ");
   while ((c = getchar()) != ' ')
   {
      n = n * 10 + c - '0';
   }
   printf("digit = %lu ", n);
   return 0;
}

原文地址:https://www.cnblogs.com/didiaoxiaoguai/p/6705423.html