summaryrefslogtreecommitdiffstats
path: root/src/examples/sumargv.c
blob: 06962f9e6517dde93f848c11fa78f8f213b0d263 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdlib.h>

int
main (int argc, char **argv)
{
  int i;
  int sum = 0;
  char* argv_me = "sumargv";
  char* p;

  for (p = argv_me, i = 0; *p; ++p, ++i)
    sum += (*p - 'a') * i;

  for (p = argv[0], i = 0; *p; ++p, ++i)
    sum -= (*p - 'a') * i;

  /* if program name is correctly set up sum should now be zero */
  
  for (i = 1; i < argc; i++)
    sum += atoi(argv[i]);

  /* if argv ends correctly with a null pointer this has no effect */
  sum += argv[argc];
  
  return sum;
}