Skip to content

Instantly share code, notes, and snippets.

@kunpengku
Created June 28, 2013 00:11
Show Gist options
  • Save kunpengku/5881479 to your computer and use it in GitHub Desktop.
Save kunpengku/5881479 to your computer and use it in GitHub Desktop.
1234,算出每一位的和。
#include<stdio.h>
int main(int argc,char *argv[])
{
int source;
int sum = 0;
source = atoi(argv[1]);
while(source/10)
{
sum += source%10;
source /= 10;
}
sum += source;//最后再加上第一位。
printf("sum = %d\n",sum);
return 0;
}
@hellojinjie
Copy link

使用 argv[1] 前,最好先判断 argc 是否大于 2

if (argc < 2) 
{
    printf("Error: 请至少输入一个参数");
    return 1;
}

source = atoi(argv[1]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment