<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Shin</title>
    <description>The latest articles on Forem by Shin (@awcyet).</description>
    <link>https://forem.com/awcyet</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F442521%2F3a49d10d-6bb5-4b9f-9281-21c46983a24a.png</url>
      <title>Forem: Shin</title>
      <link>https://forem.com/awcyet</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/awcyet"/>
    <language>en</language>
    <item>
      <title>// understanding the trinity (arrays, pointers, and memory allocation)</title>
      <dc:creator>Shin</dc:creator>
      <pubDate>Tue, 25 May 2021 11:41:04 +0000</pubDate>
      <link>https://forem.com/awcyet/understanding-the-trinity-arrays-pointers-and-memory-allocation-2a73</link>
      <guid>https://forem.com/awcyet/understanding-the-trinity-arrays-pointers-and-memory-allocation-2a73</guid>
      <description>&lt;p&gt;Previously, I've talked about the two types of parameter passing and introducing memory addresses, pointers, and the concept of referencing and dereferencing as a whole. However, this isn't the last time that we'll be seeing pointers. Pointers are an integral part of C programming, providing the programmer control with the variables that they create, and we'll be seeing it anywhere, including this post's topics: arrays and strings.&lt;/p&gt;

&lt;p&gt;As a refresher, arrays are collections of objects of the same data type: &lt;code&gt;int&lt;/code&gt;s, &lt;code&gt;double&lt;/code&gt;s, &lt;code&gt;char&lt;/code&gt;s, etc. Strings on the other hand are arrays of characters. In C, there is no native string data type, so for the language to be able to distinguish that it is indeed a string and not just an array of characters, a null terminator or &lt;code&gt;'\0'&lt;/code&gt; is added by the end.&lt;/p&gt;

&lt;p&gt;What's interesting though is that despite the absence of a string data type, C still have a format specifier (&lt;code&gt;%s&lt;/code&gt;) and built-in library (&lt;code&gt;string.h&lt;/code&gt;) dedicated for strings, which I'll be talking about in this post.&lt;/p&gt;

&lt;p&gt;With that said, let's jump in to the problem statement.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Implement a palindrome checker using various string functions in C.&lt;br&gt;
Your program should be able to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scan for the word to be checked&lt;/li&gt;
&lt;li&gt;Pass the word to a function called isPalindrome which returns an int to be interpreted in the main() whether
the given word is a palindrome or not.&lt;/li&gt;
&lt;li&gt;Loop indefinitely until the word ”EXIT” is used as input.&lt;/li&gt;
&lt;li&gt;Store each palindrome in an array, then print all palindromes before exiting the program.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;For this I'll be dividing the objectives into those who belong in the main and those who aren't. Quickly listing the initial variables, the only variable I need for now is just &lt;code&gt;char[]&lt;/code&gt; string for the user input.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;isPalidrome Function&lt;/strong&gt;&lt;br&gt;
Palidromes are words that reads the same backward or forward. This is the basis that I've used for my &lt;code&gt;isPalindrome&lt;/code&gt; function. The goal was to reverse the string and store it on a different variable, then compare it to the original to see if it's the same.&lt;/p&gt;

&lt;p&gt;There is another way to do this, which is by comparing the &lt;code&gt;char&lt;/code&gt;s of the string from the beginning and from the end, meeting at the middle. However, the problem statement mentions that &lt;code&gt;isPalindrome&lt;/code&gt; should be using the built-in string functions in C, so I'll be approaching using for the solution is by the definition.&lt;/p&gt;

&lt;p&gt;As mentioned before, C has a built-in library made for strings called &lt;code&gt;string.h&lt;/code&gt;. It contains various functions that deals with string operations such as &lt;code&gt;strcpy&lt;/code&gt; to copy a string to a memory address and &lt;code&gt;strcat&lt;/code&gt; to concatenate two strings. For this function, however, I'll only be using &lt;code&gt;strlen&lt;/code&gt; to create the reverse string, and &lt;code&gt;strcmp&lt;/code&gt; to compare the original string to its reverse.&lt;/p&gt;

&lt;p&gt;Although it looks like the parameter of my function is the string itself, what's being passed is actually the pointer of the string. This is due to one property of arrays where calling an array without an index is the same as calling  the pointer of its first element. Since the content of the array is stored sequentially in the memory, we can access all of the array just by incrementing said pointer. This is also important to point out as most string functions require the pointer that holds the string as the parameter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="n"&gt;n1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="n"&gt;n2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Dereferencing example&lt;/span&gt;
&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d = %d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 1 = 1&lt;/span&gt;

&lt;span class="n"&gt;n1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="n"&gt;n2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;example&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Dereferencing example&lt;/span&gt;
&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d = %d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 2 = 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Going back to the function, to reverse the string, I first made a &lt;code&gt;char&lt;/code&gt; array of &lt;code&gt;len&lt;/code&gt; values called &lt;code&gt;sReverse&lt;/code&gt;, where &lt;code&gt;len&lt;/code&gt; is the length of the string obtained by &lt;code&gt;strlen(string)&lt;/code&gt;. From there, I set up a loop traversing through the original string, assigning each letter in the &lt;code&gt;((len - 1) - i)&lt;/code&gt;th index to the &lt;code&gt;i&lt;/code&gt;th index of &lt;code&gt;sReverse&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// String reversal&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;sReverse&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;len&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yJso9xPo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/efjxb206u5w7jnbtmjy5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yJso9xPo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/efjxb206u5w7jnbtmjy5.png" alt="Reverse assigning a characters to an empty string"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, this is not done yet as all I've done is make something like &lt;code&gt;{'h', 'e', 'l', 'l', 'o'}&lt;/code&gt; instead of &lt;code&gt;"hello"&lt;/code&gt; because of the missing null terminator at the end. This can be done by adding the following line:&lt;br&gt;
&lt;code&gt;sReverse[i] = '\0';&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now that I've done that, the only thing left to do is to compare the new &lt;code&gt;sReverse&lt;/code&gt; string and the original string. If they are the same, the function returns &lt;code&gt;1&lt;/code&gt; for true, and &lt;code&gt;0&lt;/code&gt; otherwise. This part of the function is easily done by &lt;code&gt;strcmp&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;String Compare or &lt;code&gt;strcmp&lt;/code&gt; takes two parameters and the way it works is by going through the strings character by character and comparing them by their values in the ASCII table. Here is the &lt;a href="https://documentation.help/C-Cpp-Reference/strcmp.html"&gt;reference&lt;/a&gt; for &lt;code&gt;strcmp&lt;/code&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The function strcmp() compares str1 and str2, then returns [a value]:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;less than 0 if &lt;code&gt;str1&lt;/code&gt; is less than &lt;code&gt;str2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;equal to 0 if &lt;code&gt;str1&lt;/code&gt; is equal to &lt;code&gt;str2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;greater than 0 if &lt;code&gt;str1&lt;/code&gt; is greater than &lt;code&gt;str2&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this case though, all that matters is whether the string is equal or not, so I'll just be checking for if the output of &lt;code&gt;strcmp&lt;/code&gt; is &lt;code&gt;0&lt;/code&gt; or not.&lt;/p&gt;

&lt;p&gt;This is the entirety of my &lt;code&gt;isPalidrome&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;isPalindrome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;sReverse&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// String reversal&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;sReverse&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;len&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// Add a null-terminator at the end of the string&lt;/span&gt;
    &lt;span class="n"&gt;sReverse&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'\0'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

    &lt;span class="c1"&gt;// String comparison&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strcmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sReverse&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;lowercase Function&lt;/strong&gt;&lt;br&gt;
Since &lt;code&gt;isPalidrome&lt;/code&gt; uses &lt;code&gt;strcmp&lt;/code&gt; (and by extension, ASCII values) to compare strings, strings that have both uppercase and lowercase letters such as &lt;code&gt;"Level"&lt;/code&gt; or &lt;code&gt;"huH"&lt;/code&gt; are immediately considered non-palindromes since uppercase letters have lower ASCII values than its lowercase counterparts. Therefore, I first need to standardize the inputs before passing them through &lt;code&gt;isPalindrome&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Unfortunately, a built-in function that does this doesn't exist. Luckily, there's a library that deals with operations on characters itself, such as changing cases, or checking what the character is, and that is &lt;code&gt;ctype.h&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;lowercase&lt;/code&gt; is a utility function that I'll be using right before I call &lt;code&gt;isPalindrome&lt;/code&gt; and all it does is exactly what it's called, change the case of the string to lowercase. I wanted to change the string itself instead of making a lowercase copy of the string, so I decided to use the pointer of the string to traverse it then individually use &lt;code&gt;tolower(*i)&lt;/code&gt; where &lt;code&gt;i&lt;/code&gt; is the pointer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;lowercase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// String pointer&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="sc"&gt;'\0'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tolower&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I stored the address of the string in &lt;code&gt;i&lt;/code&gt;, and incremented it by one until it reaches the null terminator. This directly applies what I had mentioned earlier that arrays have its elements stored sequentially in the memory, so by doing &lt;code&gt;i++&lt;/code&gt;, I am able to traverse the string.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;main Function&lt;/strong&gt;&lt;br&gt;
Moving on to the main function, I went ahead and used a &lt;code&gt;while&lt;/code&gt; loop with the terminating condition set to 1 so it would ask for input indefinitely, similar to what I already had done previously. In this case, I also added an &lt;code&gt;if-else&lt;/code&gt; statement that would break the loop when the word &lt;code&gt;EXIT&lt;/code&gt; is entered. All I needed to do was compare the user input to &lt;code&gt;"EXIT"&lt;/code&gt; using &lt;code&gt;strcmp&lt;/code&gt;. This is pretty much half of the main.&lt;/p&gt;

&lt;p&gt;The next key thing that I needed to implement is to print all of the words that were determined to be a palindrome upon termination. This immediately told me to use arrays. One major obstacle, however, is that I don't know how many words the user is going to input since the input scanning runs indefinitely, and thus a fix-sized array will not work for this.&lt;/p&gt;

&lt;p&gt;That said, there is another way of implementing arrays so long as the initial number of elements is known by dynamically allocating the array using &lt;code&gt;malloc&lt;/code&gt;. In this case, I can set it to an arbitrary size and resize later on when the array is full. This is how to use &lt;code&gt;malloc&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;data_type&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;initialSize&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data_type&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;malloc&lt;/code&gt; returns a pointer of the data type indicated with the space requested calculated by &lt;code&gt;sizeof(data_type)&lt;/code&gt; times the amount of elements that I want, similar to how the pointer of the array[0] works.&lt;/p&gt;

&lt;p&gt;Another obstacle is that considering the array is storing strings, this array essentially becomes a multidimensional dynamically allocated array, which honestly sounds complicated, but in reality is pretty digestible. This &lt;a href="https://www.eskimo.com/~scs/cclass/int/sx9b.html"&gt;diagram&lt;/a&gt; should help understand what's happening.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ix4XmE27--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cw5nigsu1fpjzd6v1gkg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ix4XmE27--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cw5nigsu1fpjzd6v1gkg.png" alt="2D Array Visualized"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Therefore, the array is basically a pointer pointing to a pointer that is also pointing to a string. For this, I'll be making a char pointer array first,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;arraySize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arraySize&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then set each element of that array to a dynamically allocated string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;stringLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;stringLength&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that I have set that up, the last objective is to populate that array. Using &lt;code&gt;strcpy&lt;/code&gt;, if &lt;code&gt;isPalindrome&lt;/code&gt; determines that the user input is a palindrome, I can call &lt;code&gt;strcpy&lt;/code&gt; to copy the user input into the array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Scanning for user input&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strcmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"EXIT"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;lowercase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isPalindrome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%s is a Palidrome&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="c1"&gt;// Storing the string&lt;/span&gt;
            &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;stringLength&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; 
            &lt;span class="c1"&gt;// +1 to account for the null-terminator&lt;/span&gt;
            &lt;span class="n"&gt;strcpy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
            &lt;span class="c1"&gt;// Copy the user string to the string address&lt;/span&gt;
            &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%s is not a Palidrome&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the terminating condition is met, all I have to do is to loop through that array then print the elements. Finally, as always when dealing with dynamic allocation, I'll free up the 2D array by looping through the strings first, and then free the array itself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Admittedly, this problem was pretty difficult especially with my almost non-existent understanding of how memory allocation works, added with the confusion once you lose track of where a pointer is pointing to. &lt;/p&gt;

&lt;p&gt;The main issue for me was setting up the 2D array using &lt;code&gt;malloc&lt;/code&gt;, primarily, because I can't internalize what happens when I dynamically allocate things. It only started to make sense once I had started breaking down the syntax itself, looking at what each parameter does, how do they relate to pointers and what purpose does the return value serve.&lt;/p&gt;

&lt;p&gt;On the other hand, the arrays and strings itself as a concept was tolerable for me since I already have experience with it. But overall, with some fiddling and trying things until something sticks helped me sort of work out how to use pointers, arrays, and memory allocation the way I need it. I definitely see now how powerful these concepts are when used correctly.&lt;/p&gt;

</description>
      <category>c</category>
      <category>programming</category>
      <category>codenewbie</category>
      <category>beginners</category>
    </item>
    <item>
      <title>// pointers: the computer's fingers</title>
      <dc:creator>Shin</dc:creator>
      <pubDate>Thu, 22 Apr 2021 18:00:40 +0000</pubDate>
      <link>https://forem.com/awcyet/pointers-the-computer-s-fingers-keo</link>
      <guid>https://forem.com/awcyet/pointers-the-computer-s-fingers-keo</guid>
      <description>&lt;p&gt;In the last post, I dove into functions and how it allows us to make recursions with its syntax. Recursion is more logic-based, showcasing the human side of functions in the sense that we, the programmers, have to think in the shoes of a problem-solver.&lt;/p&gt;

&lt;p&gt;Diving deeper into functions, let's move on to the technical side of things, the computer's perspective. There are two ways of passing parameters into functions, both of which taps into various things about variables, memory addresses, and pointers.&lt;/p&gt;

&lt;p&gt;So let's talk about them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Problem&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Create a program that gets three (3) largest prime numbers from x to y, x and y are given by the user.&lt;/p&gt;

&lt;p&gt;If x is greater than y, swap x and y.&lt;/p&gt;

&lt;p&gt;If there are no prime numbers in the given range, the program must print zero (0). If there is only one prime number, the program must print that number. If there are two prime numbers in the given range, then both prime numbers are printed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Right off the bat, the problem seems complicated. Breaking it down into functions with specific purposes, I will need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;function that will swap x and y&lt;/li&gt;
&lt;li&gt;function that will check if a number n is prime&lt;/li&gt;
&lt;li&gt;function that will print the three largest primes between an interval x, y&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the variables, I declared two &lt;code&gt;int&lt;/code&gt;s, &lt;code&gt;min&lt;/code&gt; for x and &lt;code&gt;min&lt;/code&gt; for y.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Preliminaries&lt;/strong&gt;&lt;br&gt;
Before heading straight to coding, I want to talk about the concepts that I will be using. In this topic, I've learned that there are two ways of passing variables to functions: 1) passing it purely by its value and 2) passing it using its reference. Passing by value is easy as it's the parameter passing type that we've been using since the beginning; it takes the value of the variable and does computations with it. On the other hand, what does passing by reference mean?&lt;/p&gt;

&lt;p&gt;Well first, we need to know memory addresses. In C, variables are stored in free memory addresses so the more variables you store, the more addresses are going to be occupied. Conceptually, it should look like this:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
   &lt;thead&gt;
      &lt;tr&gt;
         &lt;th&gt;Address&lt;/th&gt;
         &lt;th&gt;Value&lt;/th&gt;
         &lt;th&gt;Variable Name&lt;/th&gt;
      &lt;/tr&gt;
   &lt;/thead&gt;
   &lt;tbody&gt;
      &lt;tr&gt;
         &lt;td&gt;000&lt;/td&gt;
         &lt;td&gt;8&lt;/td&gt;
         &lt;td&gt;x&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
         &lt;td&gt;001&lt;/td&gt;
         &lt;td&gt;10&lt;/td&gt;
         &lt;td&gt;y&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
         &lt;td&gt;...&lt;/td&gt;
         &lt;td&gt;...&lt;/td&gt;
         &lt;td&gt;...&lt;/td&gt;
      &lt;/tr&gt;
   &lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Question: why is this relevant?&lt;/p&gt;

&lt;p&gt;To pass by reference, we have to pass the address of the variable itself into the function instead of the value that it holds. In concept, those addresses are assigned arbitrarily and they usually look something like a hexadecimal value, so to get their value, they are stored in variables called pointers which does exactly as the name suggests: point to the address of another variable. You can do this by the following syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, pointer &lt;code&gt;p&lt;/code&gt; is initialized using &lt;code&gt;*&lt;/code&gt;, and then the address of &lt;code&gt;var&lt;/code&gt; is taken using the unary operator &lt;code&gt;&amp;amp;&lt;/code&gt;, which is then stored in &lt;code&gt;p&lt;/code&gt;. The data type of the pointer should be the same as the data type of the variable you're trying to get the address of.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;&amp;amp;&lt;/code&gt; is only one of the two operators that are used when working with pointers. Obviously, the other one is &lt;code&gt;*&lt;/code&gt;, but it has another use aside from just initialization.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"x is: %d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// x is: 1&lt;/span&gt;
&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d is the same as %d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// 1 is the same as 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;*&lt;/code&gt; checks the address stored in the pointer and then takes the value of the variable in that address. Essentially what I've done in the example is take &lt;code&gt;var&lt;/code&gt;'s address using &lt;code&gt;&amp;amp;&lt;/code&gt; then store it into &lt;code&gt;p&lt;/code&gt;, then take the value of &lt;code&gt;var&lt;/code&gt; from &lt;code&gt;p&lt;/code&gt; using &lt;code&gt;*&lt;/code&gt;. This is (sort of) what passing by reference is.&lt;/p&gt;

&lt;p&gt;It's pretty complicated at first sight, but with this in mind, I can finally move on to making the program.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Swapper Function&lt;/strong&gt;&lt;br&gt;
There are situations where using passing by reference is more preferred over passing by value (and vice versa) and it is on the hands of the programmer to decide which is more appropriate for the problem.&lt;/p&gt;

&lt;p&gt;Passing by value is suitable in many cases and you can get by with just using it. However, since only the value is only needed, the function needs to copy it first, which can be expensive in the performance with larger variables. Aside from that, pass by value only works on the copy, so on occasions that you'd want to perform computations on the variable itself, it would be more appropriate to pass by reference.&lt;/p&gt;

&lt;p&gt;Since I'm required to swap the values of two variables, I think it would be better to pass by reference directly. Simply enough, all the function does is take the pointers of the numbers as parameters, store the value of one address in a temporary variable, and then swap them as usual. The function will be &lt;code&gt;void&lt;/code&gt; as well since I've already swapped the numbers and don't need to return anything else.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;swap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The Prime Checker Function&lt;/strong&gt;&lt;br&gt;
Initially, I thought of just adding this bit to the function that prints the primes, however, it would be difficult to implement (and read) as the function is doing two different things, counterproductive for a function. In this case, I'm just checking whether the number is prime or not, so there would be no pass by reference.&lt;/p&gt;

&lt;p&gt;As mentioned in the previous post, to find primes, all I need to do is check if the number is not divisible by at least one smaller number except 1. This version of the function is implemented with an iterative approach using a &lt;code&gt;for&lt;/code&gt; loop starting from &lt;code&gt;n - 1&lt;/code&gt; down to &lt;code&gt;2&lt;/code&gt;. When the loop finishes with no divisor found, it returns the value of &lt;code&gt;flag&lt;/code&gt;, a local variable set to &lt;code&gt;1&lt;/code&gt; by default, indicating that it is prime. Whereas if a smaller number is found, the loop breaks, changing the value of &lt;code&gt;flag&lt;/code&gt; to &lt;code&gt;0&lt;/code&gt; and returning it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;isPrime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;flag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;flag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// If divisor is found,&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// break loop and return 0&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The Prime Printer Function&lt;/strong&gt;&lt;br&gt;
For this function, I'll only be printing numbers, so I made the return type &lt;code&gt;void&lt;/code&gt;. It works is by checking if the number is prime using the &lt;code&gt;isPrime&lt;/code&gt; function, starting from the upper bound of the interval, allowing the function to print the largest primes first. It also has a local variable counter that records how many primes have been found and breaking the loop when three are found. Any less than that will be printed regardless, but if nothing is found, it will print &lt;code&gt;0&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;firstPrimes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;primes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isPrime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;primes&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;primes&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// If there are 3 primes already,&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;              &lt;span class="c1"&gt;// stop printing&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;primes&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"0"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The Other Parts&lt;/strong&gt;&lt;br&gt;
Lastly, the only thing left is to place the function calls wherever necessary in the main. Before that, I added an input prompt for the two numbers x and y, with x as the lower bound and y as the upper bound. Once the user has placed two numbers, the program will check if x is greater than y, calling &lt;code&gt;swap&lt;/code&gt;. If all goes well, the program then calls &lt;code&gt;firstPrimes&lt;/code&gt;, printing the largest three primes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Overall, this has been a new experience for me. It's interesting to see how C deals with its memory management on a deeper level. Learning pointers, to me, feels like a great way to understand how the computer hardware handles the variables and calculations that we add to our programs. Admittedly, this is something that I tend to overlook with other programming languages, and I guess this topic acts like a reminder that "Hey! All this is being processed by your computer's memory." I think that is one of the greatest things about low-level languages.&lt;/p&gt;

</description>
      <category>c</category>
      <category>programming</category>
      <category>codenewbie</category>
      <category>beginners</category>
    </item>
    <item>
      <title>// functions and recursions, a tasteful combination</title>
      <dc:creator>Shin</dc:creator>
      <pubDate>Tue, 20 Apr 2021 17:26:47 +0000</pubDate>
      <link>https://forem.com/awcyet/functions-and-recursions-a-tasteful-combination-02-1804</link>
      <guid>https://forem.com/awcyet/functions-and-recursions-a-tasteful-combination-02-1804</guid>
      <description>&lt;p&gt;With most of the basics tackled, I moved on to functions as well as another concept that comes with it and is fundamental to a lot of languages, recursion. Because of the ability of functions to contain a problem and define it using parameters, we are able to implement recursion whose whole idea is to divide the problem into simpler instances of itself. In a more practical sense, the parameterization that's brought by functions blends well with recursion, and this can be done by using simpler parameters repeatedly until it reaches a base case from which it terminates.&lt;/p&gt;

&lt;p&gt;The way I see it, functions and recursions are the bread-and-butter of programming when we try to solve problems through the divide-and-conquer approach.&lt;/p&gt;

&lt;p&gt;Like the last post, these concepts are mostly familiar to me but there are also bits of the syntax that are specific to C only, which I will briefly mention by the end of this post. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Problem&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Create a program that asks for an integer n and lists all prime numbers from 1 to n. Start by a creating recursive function that determines whether an integer is prime or not. Next, ask for an integer n, then loop through all the integers from 1 to n. Check each integer if it is prime by using the function earlier then print the integer if it is prime.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Looks simple enough. Breaking the problem down, there are three things that I need to do:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;recursive function that checks if a number is prime&lt;/li&gt;
&lt;li&gt;user input for a number &lt;code&gt;n&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;loop from 1 to &lt;code&gt;n&lt;/code&gt; that calls the function and prints the number if it's prime&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For this program, I only need a single &lt;code&gt;int&lt;/code&gt; variable &lt;code&gt;n&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Recursive Function&lt;/strong&gt;&lt;br&gt;
Starting with the hard bit of the program, I first needed to figure out how to check the number if it's prime and then express that recursively. Initially, I thought of dividing the numbers by the first four prime numbers, however, I realized that this method only works for numbers below 100. Since I need a more general description for prime numbers, I looked for its definition.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A prime number is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;From this, we can infer that primes can only be expressed as a product of 1 and itself, and any number that can be divided by any smaller number means it is not a prime. Iteratively, this is easy to do since I can just make a loop dividing &lt;code&gt;num&lt;/code&gt; from &lt;code&gt;num - 1&lt;/code&gt; to &lt;code&gt;2&lt;/code&gt;, unfortunately, this is not the case with recursion.&lt;/p&gt;

&lt;p&gt;Luckily, recursive functions, in general, can be divided into two parts: the &lt;em&gt;base case&lt;/em&gt; and the &lt;em&gt;recursive case&lt;/em&gt;. The base case is essentially the predefined outcome for the simplest version of the problem, hence base, whereas the recursive case reduces the parameters of the next function call until we get to the base case. The base case is very important as it decides when should the program stop calling itself (since otherwise, the function would continue on and on).&lt;/p&gt;

&lt;p&gt;For example, a Fibonacci sequence is a sequence where each number is the sum of the two previous ones, but since we don't know what the "two previous ones" mean, we have to define it ourselves, and that acts as the base case.&lt;br&gt;
Recursive Case:&lt;br&gt;
Fn = Fn - 1 + Fn - 2&lt;br&gt;
Base Cases:&lt;br&gt;
F1 = 1&lt;br&gt;
F2 = 1&lt;/p&gt;

&lt;p&gt;Since the function should check if a number &lt;code&gt;n&lt;/code&gt; is divisible by numbers less than &lt;code&gt;num&lt;/code&gt;, my function will have two parameters, &lt;code&gt;int num&lt;/code&gt; and &lt;code&gt;int div&lt;/code&gt; with &lt;code&gt;div&lt;/code&gt; being the parameter that will be reduced every recursive call.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;isPrime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this problem, if the function finds a number that can divide &lt;code&gt;num&lt;/code&gt;, it should immediately conclude that the number is not prime, however, if it haven't, it should continue looking for a smaller number by putting &lt;code&gt;div - 1&lt;/code&gt; as the new parameter. This will be the recursive case.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;div&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// num is not prime&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;isPrime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;div&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the other hand, the logical base case would be when &lt;code&gt;div&lt;/code&gt; is equal to &lt;code&gt;1&lt;/code&gt; because that would mean that function have gone through all the numbers and found nothing that can divide &lt;code&gt;num&lt;/code&gt;. Overall:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;isPrime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// BASE CASE&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// num is prime&lt;/span&gt;
    &lt;span class="c1"&gt;// RECURSIVE CASE&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;div&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// num is not prime&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;isPrime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;div&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The Other Parts&lt;/strong&gt;&lt;br&gt;
For the user input, I simply added a prompt for the user then used &lt;code&gt;scanf&lt;/code&gt; as usual, storing it in &lt;code&gt;n&lt;/code&gt;. After that, I added the loop that will go through all the numbers from 2 to &lt;code&gt;n&lt;/code&gt; and call the function. If the function returns 1, it should print the number.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter a positive integer: "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;scanf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;The prime numbers from 1 to %d are: "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isPrime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One thing that I haven't mentioned yet is that in C, the compiler issues out a warning when a function is called before it was declared. The order doesn't really matter because the program can run just fine, but it helps with efficiency and memory consumption as the program doesn't need to pass (read the code) more than once. So, either declare the function below &lt;code&gt;main&lt;/code&gt; or add a &lt;a href="https://www.cs.utah.edu/~germain/PPS/Topics/C_Language/c_functions.html"&gt;prototype&lt;/a&gt; of the function on top of the code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
All in all, this has been a nice refresher for me about functions and recursions, and it's definitely nice to see them in the context of C. But aside from that, I had a glimpse of how the compiler works with respect to the functions declared on the program, which is new knowledge from me. With regards to recursion, obviously, it's overkill to use recursion for a simple loop as recursion tends to be costly, so for every problem that involves repetition, I believe that as a programmer, it is good practice that we use these concepts only whenever it is appropriate.&lt;/p&gt;

</description>
      <category>c</category>
      <category>programming</category>
      <category>codenewbie</category>
      <category>beginners</category>
    </item>
    <item>
      <title>// c-ntax, c-lections, and the basics of c</title>
      <dc:creator>Shin</dc:creator>
      <pubDate>Mon, 19 Apr 2021 14:18:33 +0000</pubDate>
      <link>https://forem.com/awcyet/c-ntax-and-c-lections-01-5755</link>
      <guid>https://forem.com/awcyet/c-ntax-and-c-lections-01-5755</guid>
      <description>&lt;p&gt;I've quite an experience with programming particularly in high-level languages like Python and Javascript with the exception of Java, but learning C is something different in terms of how it works on a deeper level. Similar to Java, the program must be compiled first before you can run it. Some syntax from Java and C are also similar so there's a degree of familiarity that I am comfortable with, but there are also vastly different things as well which this post will tackle.&lt;/p&gt;

&lt;p&gt;With that said, my journey with C have started with this problem set:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Create a C program that will compute for the body-mass index of a given height and weight. After calculating the BMI, the program will also tell under which category (underweight, normal weight, overweight, or obese) it belongs to. The user will be given two (2) choices before entering the height and weight (those choices being metric and imperial measurements). The menu must loop repeatedly until the exit option is entered. Make sure to have proper error prompts."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Breaking the problem down, there are several things that are necessary for the program:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;prompts and user input&lt;/li&gt;
&lt;li&gt;selection for the two modes of calculating BMI (imperial and metric)&lt;/li&gt;
&lt;li&gt;selection for the ranged categories the BMI belong to&lt;/li&gt;
&lt;li&gt;the menu to loop indefinitely until the exit option is selected; and finally&lt;/li&gt;
&lt;li&gt;error prompts for invalid inputs for

&lt;ul&gt;
&lt;li&gt;options that were not listed&lt;/li&gt;
&lt;li&gt;weight and height being 0 and less&lt;/li&gt;
&lt;li&gt;string inputs&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before I started coding, I wanted to list the possible variables that I will use. Since I have a menu, I would need an &lt;code&gt;int option&lt;/code&gt; variable to store the user's choice. I would also need the variables &lt;code&gt;weight&lt;/code&gt;, &lt;code&gt;height&lt;/code&gt;, and &lt;code&gt;BMI&lt;/code&gt; for the calculations, all of which are in &lt;code&gt;float&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Prompts and Inputs&lt;/strong&gt;&lt;br&gt;
Printing and scanning for inputs are one of the building blocks of any programming language, and printing/scanning in C works slightly differently from other languages. &lt;/p&gt;

&lt;p&gt;For C, I need to import the library before I can take inputs first, and this is done by appending &lt;code&gt;#include &amp;lt;stdio.h&amp;gt;&lt;/code&gt; on the first line, &lt;code&gt;stdio.h&lt;/code&gt; being the library consisting the scanner.&lt;/p&gt;

&lt;p&gt;I've added prompts that ask the user to select between the calculating options by using &lt;code&gt;printf&lt;/code&gt; and take the input using &lt;code&gt;scanf&lt;/code&gt;. That said, printing and scanning in C is a lot more nuanced, for one, to be able to scan (and print) variables, I have to put the &lt;a href="https://codeforwin.org/2015/05/list-of-all-format-specifiers-in-c-programming.html"&gt;format specifier&lt;/a&gt; of the variable. Since I've decided to use &lt;code&gt;int&lt;/code&gt; data type for my &lt;code&gt;option&lt;/code&gt; variable, I should use the &lt;code&gt;%d&lt;/code&gt; specifier with &lt;code&gt;scanf&lt;/code&gt; (and later on &lt;code&gt;printf&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;So far this is what I have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;stdio.h&amp;gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;option&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BMI&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// MENU AND USER INPUT&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Select Mode:&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;
           &lt;span class="s"&gt;"(1) Enter in kilogram and centimeters&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;
           &lt;span class="s"&gt;"(2) Enter in pounds and feet&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;
           &lt;span class="s"&gt;"Choice: "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;scanf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;option&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The Selections&lt;/strong&gt;&lt;br&gt;
Next up are the selection statements. After the user selects the mode, the value is stored in      &lt;code&gt;option&lt;/code&gt;. I decided to use a &lt;code&gt;switch&lt;/code&gt; statement for this, with &lt;code&gt;case: 1&lt;/code&gt; being the metric measurements and &lt;code&gt;case: 2&lt;/code&gt; being the imperial measurements. The formula for solving BMI changes with the mode being used, so this will be the body of my cases.&lt;br&gt;
Metric System: &lt;code&gt;BMI = weight / (height * height)&lt;/code&gt;&lt;br&gt;
Imperial System: &lt;code&gt;BMI = (703 * weight) / (height * height)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Aside from that, both cases will ask the user for the height and weight measurements. The only difference would be the user must provide the measurements according to the selected mode.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;option&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Enter weight in kg: "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;scanf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%f"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter height in cm: "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;scanf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%f"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// BMI CALCULATION&lt;/span&gt;
        &lt;span class="n"&gt;BMI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;weight&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Your BMI is: %f&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BMI&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Enter weight in lbs: "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;scanf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%f"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter height in in: "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;scanf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%f"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// BMI CALCULATION&lt;/span&gt;
        &lt;span class="n"&gt;BMI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;703&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Your BMI is: %f&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BMI&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;default:&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I also need a selection for the BMI categories. Since the conditions for the categories are ranged, using a chain of &lt;code&gt;if-else&lt;/code&gt; statements would be the appropriate choice. Using the basic 4-tier BMI table as the basis, this is what I had:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// BMI CATEGORIES&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BMI&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Your BMI category is: Underweight&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BMI&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;BMI&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Your BMI category is: Normal&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BMI&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;BMI&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Your BMI category is: Overweight&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BMI&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Your BMI category is: Obese&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The Loops&lt;/strong&gt;&lt;br&gt;
This problem requires me to have a menu that will loop on and on until the exit option is selected. To do this, I started modifying the menu by putting it inside a &lt;code&gt;while&lt;/code&gt; loop. I've made the loop run indefinitely by making the condition &lt;code&gt;1&lt;/code&gt; which the loop reads as &lt;code&gt;true&lt;/code&gt;. The only way this loop ends is by adding an &lt;code&gt;if-else&lt;/code&gt; statement that breaks the loop if the option is equal 0.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// MENU AND USER INPUT&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Select Mode:&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;
           &lt;span class="s"&gt;"(1) Enter in kilogram and centimeters&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;
           &lt;span class="s"&gt;"(2) Enter in pounds and feet&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;
           &lt;span class="s"&gt;"(0) Exit&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;
           &lt;span class="s"&gt;"Choice: "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;scanf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;option&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;option&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// THE SWITCH STATEMENT COMES HERE&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The Input Validation and Error Prompts&lt;/strong&gt;&lt;br&gt;
The last thing that I need is to add validation for the user inputs. For the menu, there are two things to address: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;numerical inputs that are not listed as an option&lt;/li&gt;
&lt;li&gt;string inputs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I started by putting what I already have in the &lt;code&gt;while&lt;/code&gt; loop in an &lt;code&gt;if-else&lt;/code&gt; statement that prints out an error prompt if &lt;code&gt;option&lt;/code&gt; was less than 0 or greater than 2, preventing the user from entering numerical inputs that are explicitly not the three allowed options.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;option&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;option&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Error: please enter a number from 0 to 2. &lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;option&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// THE SWITCH STATEMENT COMES HERE&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, I realized that this doesn't account for &lt;code&gt;floating-point&lt;/code&gt; numbers between 0 to 2. Apparently, C programs only show warnings when there is a type mismatch and still compile and run regardless, as opposed to other programming languages. This made debugging input mismatches difficult since the program doesn't show any problems during runtime as warnings only show up before compilation. &lt;/p&gt;

&lt;p&gt;The way input works in C is by input stream and the way I understand is when the user inputs various things like text, characters, numbers, and even the newline, the program reads everything as a single giant string that is split with spaces for every variable. This string is only cut when a period is typed.&lt;/p&gt;

&lt;p&gt;Because of the decimal input being placed in an &lt;code&gt;int&lt;/code&gt; data type, it results in this weird behavior where the input is split into two input streams by the decimal point. The whole number portion of the input is taken by the variable and the remainder is placed on the next variable, ultimately skipping the supposed scanf for that variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Test: "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;scanf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Test: &lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;scanf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For instance, if I enter &lt;code&gt;1.5&lt;/code&gt; in the first prompt, only &lt;code&gt;1&lt;/code&gt; would be accepted in &lt;code&gt;x&lt;/code&gt; while the decimal is interpreted as garbage and placed in &lt;code&gt;y&lt;/code&gt;, preventing the user from entering a different value.&lt;/p&gt;

&lt;p&gt;To deal with this, I added a helper &lt;code&gt;float temp&lt;/code&gt; variable that takes whatever input and then I typecasted it to int before storing in &lt;code&gt;option&lt;/code&gt; to kind of remove the decimal portion of the input. Not the best workaround by any means but it works with what I know so far.&lt;/p&gt;

&lt;p&gt;Input validation for strings works in somewhat the same way. Strings typed into the scanf are interpreted as garbage when placed in an &lt;code&gt;int&lt;/code&gt; variable. Regardless, because of the user pressing &lt;code&gt;Enter&lt;/code&gt;, a newline character gets added to the input stream, also preventing the user from entering a correct value. To fix this, I added the following line every time the validation condition detects that the input is invalid:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;getchar&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Essentially, the line continuously looks at the stream until a newline is found, then removing it to "clean" the stream for new input.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Overall, I have learned a lot about how C works in relation to what I already know. Of course, a good fraction of it is the fundamentals, like the data types, general structure of the &lt;code&gt;main&lt;/code&gt;, selections and comparisons, operations, among many others. The one thing that stood out to me the most however is the way C handles user input. I found this to be quite difficult since it's different from what I am used to, like finding out that C programs do not detect type mismatches which was quite strange for me. And understandably, it should be as any first step to anything. I ended up poking around to see which works and which doesn't, and it definitely helped me discover things on my own. &lt;/p&gt;

</description>
      <category>c</category>
      <category>programming</category>
      <category>codenewbie</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
