summaryrefslogtreecommitdiffstats
path: root/src/examples/sumargv.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/examples/sumargv.c')
-rw-r--r--src/examples/sumargv.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/examples/sumargv.c b/src/examples/sumargv.c
new file mode 100644
index 0000000..06962f9
--- /dev/null
+++ b/src/examples/sumargv.c
@@ -0,0 +1,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;
+}