Sum of numbers


$ls
first-example first-example.c new-sum.c sum.c
$gcc sum.c -o sum
$cat sum.c
#include <stdio.h>
#include <stdlib.h>
void printhelp(char progname[]);

int main(int argc, char *argv[])
{
int i;
int sum = 0;

/* Simple sanity check */
if (argc == 1)
{
printhelp(argv[0]);
return 1;
}

for (i=1; i<argc; i++)
{
sum = sum + atoi(argv[i]);
}
printf(“Total sum: %i\n”, sum);
return 0;
}

void printhelp(char progname[])
{
printf(“%s integer …\n”, progname);
printf(“This program takes any number of ”
“integer values and sums them up\n”);
}
$./sum 1 2 3
Total sum: 6
$


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: