
{"id":19021,"date":"2022-07-26T21:11:51","date_gmt":"2022-07-26T15:41:51","guid":{"rendered":"https:\/\/beautifulworknew.wordpress.com\/?p=19021"},"modified":"2022-07-26T21:11:51","modified_gmt":"2022-07-26T15:41:51","slug":"linux-spt-sum-and-multiplication","status":"publish","type":"post","link":"https:\/\/www.trueangle.org\/index.php\/2022\/07\/26\/linux-spt-sum-and-multiplication\/","title":{"rendered":"Sum and multiplication"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ls\nfirst-example  first-example.c  new-sum.c  sum  sum.c\n$cat new-sum.c \n#define _XOPEN_SOURCE 500\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;unistd.h&gt;\nvoid printhelp(char progname[]);\n\nint main(int argc, char *argv[])\n{\n    int i, opt, sum;\n\n    \/* Simple sanity check *\/\n    if (argc == 1)\n    {\n        printhelp(argv[0]);\n        return 1;\n    }\n\n    \/* Parse command-line options *\/\n    while ((opt = getopt(argc, argv, \"smh\")) != -1)\n    {\n        switch (opt)\n        {\n            case 's': \/* sum the integers *\/\n                sum = 0;\n                for (i=2; i&lt;argc; i++)\n                    sum = sum + atoi(argv[i]);\n                break;\n            case 'm': \/* multiply the integers *\/\n                sum = 1;\n                for (i=2; i&lt;argc; i++)\n                    sum = sum * atoi(argv[i]);\n                break;\n            case 'h': \/* -h for help *\/\n                printhelp(argv[0]);\n                return 0;\n            default: \/* in case of invalid options*\/\n                printhelp(argv[0]);\n                return 1;\n        }\n    }\n    printf(\"Total: %i\\n\", sum);\n    return 0;\n}\n\nvoid printhelp(char progname[])\n{\n    printf(\"%s [-s] [-m] integer ...\\n\", progname);\n    printf(\"-s sums all the integers\\n\"\n        \"-m multiplies all the integers\\n\"\n        \"This program takes any number of integer \"\n        \"values and either add or multiply them.\\n\"\n        \"For example: %s -m 5 5 5\\n\", progname);\n}\n$ls\nfirst-example  first-example.c  new-sum.c  sum  sum.c\n$gcc new-sum.c \n$rm a.out \n$gcc new-sum.c -o new-sum\n$.\/new-sum\n.\/new-sum [-s] [-m] integer ...\n-s sums all the integers\n-m multiplies all the integers\nThis program takes any number of integer values and either add or multiply them.\nFor example: .\/new-sum -m 5 5 5\n$.\/new-sum -m 3 3\nTotal: 9\n$.\/new-sum -s 3 3\nTotal: 6\n$\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>$ls first-example first-example.c new-sum.c sum sum.c $cat new-sum.c #define _XOPEN_SOURCE 500 #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;unistd.h&gt; void printhelp(char progname[]); int main(int argc, char *argv[]) { int i, opt, sum; \/* Simple sanity check *\/ if (argc == 1) { printhelp(argv[0]); return 1; } \/* Parse command-line options *\/ while ((opt = getopt(argc, argv, &#8220;smh&#8221;)) &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.trueangle.org\/index.php\/2022\/07\/26\/linux-spt-sum-and-multiplication\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Sum and multiplication&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/posts\/19021"}],"collection":[{"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/comments?post=19021"}],"version-history":[{"count":0,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/posts\/19021\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/media?parent=19021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/categories?post=19021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/tags?post=19021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}