{"id":6432,"date":"2024-06-08T00:01:12","date_gmt":"2024-06-08T07:01:12","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6432"},"modified":"2024-06-01T13:05:46","modified_gmt":"2024-06-01T20:05:46","slug":"adding-two-values-solutions","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6432","title":{"rendered":"Adding Two Values &#8211; Solutions"},"content":{"rendered":"<p>This <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6427\">month&#8217;s Exercise<\/a> is rather simple, though it doesn&#8217;t promise a simple solution. Instead, I offer three solutions. One of which is obvious and two are kind of out there. They all work, which is the point.<br \/>\n<!--more--><br \/>\nMy first solution is the first one I thought of, the &#8220;easy&#8221; solution. It uses <em>scanf()<\/em> to read in two values, one after the other, separated by whitespace.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_06-Exercise-a.c\" rel=\"noopener\" target=\"_blank\">2024_06-Exercise-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int a,b;\r\n\r\n    printf(\"Enter two values: \");\r\n    scanf(\"%d %d\",&amp;a,&amp;b);\r\n    printf(\"%d + %d = %d\\n\",a,b,a+b);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Remember that <em>scanf()<\/em> is a stream function. It tries to match its input string with standard input, so it looks for two values separated by a space. Here&#8217;s a sample run:<\/p>\n<p><code>Enter two values: 5 6<br \/>\n5 + 6 = 11<\/code><\/p>\n<p>For my second example, I decided to read the command line for values:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_06-Exercise-b.c\" rel=\"noopener\" target=\"_blank\">2024_06-Exercise-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n    int a,b;\r\n\r\n    if( argc&lt;3 )\r\n    {\r\n        puts(\"Enter two values\");\r\n        exit(1);\r\n    }\r\n\r\n    a = strtol(argv[1],NULL,10);\r\n    b = strtol(argv[2],NULL,10);\r\n    printf(\"%d + %d = %d\\n\",a,b,a+b);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The <em>if<\/em> test confirms that three arguments are present on the command line: the program name followed by two items that the code reads as values. When the arguments aren&#8217;t present, a message is output, which serves as the program&#8217;s prompt. (The program name could also be something like <code>InputTwoValues<\/code>.)<\/p>\n<p>The <em>strtol()<\/em> function converts text from a string into a <em>long int<\/em> value in the given base. The first argument is the string, where I use command line arguments <code>argv[1]<\/code> and then <code>argv[2]<\/code>. The second argument is NULL, which directs <em>strtol()<\/em> to stop looking for additional values. The final argument is the number base, <code>10<\/code> for decimal. Here are my sample runs:<\/p>\n<p><code>$ .\/a.out<br \/>\nEnter two values<br \/>\n$ .\/a.out 100 60<br \/>\n100 + 60 = 160<\/code><\/p>\n<p>My final example exploits the second argument in the <em>strtol()<\/em> function. When this value is non-NULL, it&#8217;s used as an address to hold a reference to the invalid character after the first found value. This pointer is used to continue scanning the same string for additional values:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_06-Exercise-c.c\" rel=\"noopener\" target=\"_blank\">2024_06-Exercise-c.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint main()\r\n{\r\n    const int size=16;\r\n    char input[size];\r\n    char *next;\r\n    int a,b;\r\n\r\n    printf(\"Enter two values: \");\r\n    fgets(input,size,stdin);\r\n    a = strtol(input,&amp;next,10);\r\n    b = strtol(next,NULL,10);\r\n    printf(\"%d + %d = %d\\n\",a,b,a+b);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The second argument to <em>strtol()<\/em> is a pointer-pointer. Declaring it as <code>char **next<\/code> doesn&#8217;t work, mostly because the variable must be used directly in the second <em>strtol()<\/em> call.<\/p>\n<p>In the code, <code>next<\/code> is declared as a pointer, but it&#8217;s used in the <em>strtol()<\/em> call with the <code>&<\/code> (address-of) operator to be a pointer-pointer. This code generates on warnings or errors in <em>clang<\/em>. Here is a sample run:<\/p>\n<p><code>Enter two values: 100 -20<br \/>\n100 + -20 = 80<\/code><\/p>\n<p>I&#8217;m certain other solutions are available as well. These demonstrate the many possible ways to code such a problem, specifically to obtain user input for the calculation. I hope your solution(s) met with success.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This month&#8217;s Exercise is rather simple, though it doesn&#8217;t promise a simple solution. Instead, I offer three solutions. One of which is obvious and two are kind of out there. They all work, which is the point.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-6432","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6432","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6432"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6432\/revisions"}],"predecessor-version":[{"id":6440,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6432\/revisions\/6440"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}