<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Dionysia Lemonaki - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Dionysia Lemonaki - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Wed, 17 Jun 2026 16:18:21 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/author/dionysialemonaki/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ JS Remove Char from String – How to Trim a Character from a String in JavaScript ]]>
                </title>
                <description>
                    <![CDATA[ Manipulating strings is a fundamental skill in programming. A common task you might encounter when coding in JavaScript is trimming characters from a string. Trimming involves removing specific characters from the beginning and/or end of a string. Th... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/javascript-remove-char-from-string/</link>
                <guid isPermaLink="false">663d29485899aaa3791e15ef</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Dionysia Lemonaki ]]>
                </dc:creator>
                <pubDate>Thu, 09 May 2024 19:51:36 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/cckf4TsHAuw/upload/1d75957a397f479d41bc73b407025508.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Manipulating strings is a fundamental skill in programming.</p>
<p>A common task you might encounter when coding in JavaScript is trimming characters from a string. Trimming involves removing specific characters from the beginning and/or end of a string. These characters can be leading or trailing whitespace, tabs, line breaks, or trailing commas.</p>
<p>You may also want to remove specific characters you don't want in your program and replace them with others.</p>
<p>In this article, you will learn about some methods you can use to trim and remove characters from a string in JavaScript.</p>
<h2 id="heading-how-to-remove-whitespace-characters-using-the-trim-method-in-javascript">How to Remove Whitespace Characters Using the <code>trim()</code> Method in JavaScript</h2>
<p>You can remove leading and trailing whitespace characters from a string using the built-in <code>trim()</code> method.</p>
<p>Here is the general syntax for the <code>trim()</code> method:</p>
<pre><code class="lang-javascript">string.trim()
</code></pre>
<p>The <code>trim()</code> method is called directly on the string you want to trim. The method removes any whitespace characters such as spaces, tabs, or line breaks, from the beginning and end of the string.</p>
<p>Note that the <code>trim()</code> method doesn't modify the original string. Instead, it returns a new trimmed string, with the leading and trailing characters removed.</p>
<p>Let's take the following example:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> greeting = <span class="hljs-string">"    Hello World    "</span>;

<span class="hljs-keyword">let</span> trimmedGreeting = greeting.trim();

<span class="hljs-built_in">console</span>.log(trimmedGreeting); <span class="hljs-comment">// Output: "Hello World"</span>
</code></pre>
<p>In the example above, I declared a variable named <code>greeting</code> and assigned the string value <code>Hello World</code> . The string has spaces both at the beginning and the end.</p>
<p>To remove those leading and trailing spaces, I called the <code>trim()</code> method on the <code>greeting</code> variable and stored the result in a new variable, <code>trimmedGreeting</code>.</p>
<p>When I used <code>console.log()</code> to print the new string to the console, the leading and trailing spaces had been removed.</p>
<p>The original string in <code>greeting</code> still contains the leading and trailing spaces.</p>
<h3 id="heading-how-to-remove-leading-whitespace-characters-using-the-trimstart-method-in-javascript">How to Remove Leading Whitespace Characters Using the <code>trimStart()</code> Method in JavaScript</h3>
<p>To remove whitespace characters only form the start of a string in JavaScript, you can use the <code>trimStart()</code> method.</p>
<p>Let's take the following example:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> greeting = <span class="hljs-string">"    Hello World    "</span>;

<span class="hljs-keyword">let</span> trimmedGreeting = greeting.trimStart();

<span class="hljs-built_in">console</span>.log(trimmedGreeting); <span class="hljs-comment">// Output: "Hello World    "</span>
</code></pre>
<p>In the example above, the <code>greeting</code> variable has a string value of <code>Hello World</code> .The string has spaces at the beginning and the end.</p>
<p>I called the <code>trimStart()</code> method on <code>greeting</code> and saved the result in <code>trimmedGreeting</code>. This method removes only any whitespaces from the beginning of the string. The whitespaces at the end of the string will remain.</p>
<h3 id="heading-how-to-remove-trailing-whitespace-characters-using-the-trimend-method-in-javascript">How to Remove Trailing Whitespace Characters Using the <code>trimEnd()</code> Method in JavaScript</h3>
<p>To remove whitespace characters only form the end of a string in JavaScript, you can use the <code>trimEnd()</code> method.</p>
<p>Let's take the following example:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> greeting = <span class="hljs-string">"    Hello World    "</span>;

<span class="hljs-keyword">let</span> trimmedGreeting = greeting.trimEnd();

<span class="hljs-built_in">console</span>.log(trimmedGreeting); <span class="hljs-comment">// Output: "    Hello World"</span>
</code></pre>
<p>The string stored in <code>greeting</code> has spaces at the beginning and the end.</p>
<p>I called the <code>trimEnd()</code> method on <code>greeting</code> and saved the result in <code>trimmedGreeting</code>. This method removes only any whitespaces from the end of the string. The whitespaces at the beginning of the string will remain.</p>
<h2 id="heading-how-to-remove-a-character-using-the-replace-method-in-javascript">How to Remove a Character Using the <code>replace()</code> Method in JavaScript</h2>
<p>To remove a specific character from a string in JavaScript, you can use the <code>replace()</code> method.</p>
<p>Here is the general syntax for the <code>replace()</code> method:</p>
<pre><code class="lang-javascript">string.replace(pattern, replacement);
</code></pre>
<p>You call the <code>replace()</code> method on a string you want to modify. The method accepts two arguments: <code>pattern</code> and <code>replacement</code>.</p>
<p>The <code>pattern</code> argument specifies the pattern you want to find and replace in the string. This can be a specific character, substring, or regular expression pattern you want to find and replace in the string.</p>
<p>The <code>replacement</code> argument is the new character or string you want to replace <code>pattern</code> with.</p>
<p>Note that the <code>replace()</code> method doesn't modify the original string.</p>
<p>Let's take a look at an example:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> sentence = <span class="hljs-string">"I love dogs"</span>;

<span class="hljs-keyword">let</span> modifiedSentence  = sentence.replace(<span class="hljs-string">"dogs"</span>, <span class="hljs-string">"cats"</span>);

<span class="hljs-built_in">console</span>.log(modifiedSentence); <span class="hljs-comment">// Output: "I love cats"</span>
</code></pre>
<p>I first declared a variable named <code>sentence</code> and assigned the string value <code>I love dogs</code>.</p>
<p>Then, I called the <code>replace()</code> method on <code>sentence</code>, as I want to remove the substring <code>dogs</code> and replace it with <code>cats</code>. I then stored the result in the new variable <code>modifiedSentence</code>.</p>
<p>Lastly, I logged the string stored in the <code>modifiedSentence</code> variable to the console. The <code>"I love cats"</code> string is printed to the console.</p>
<p>The <code>replace()</code> method found the substring <code>dogs</code> and replaced it with <code>cats</code>, without changing anything else.</p>
<h2 id="heading-how-to-remove-multiple-instances-of-a-character-using-the-replace-method-in-javascript">How to Remove Multiple Instances of a Character Using the <code>replace()</code> Method in JavaScript</h2>
<p>In the previous section, you saw an example of how to use the <code>replace()</code> method to replace one word with another one.</p>
<p>What happens though when you've got multiple occurrences of the word you want to replace?</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> sentence = <span class="hljs-string">"I love dogs because dogs are cute"</span>;

<span class="hljs-keyword">let</span> modifiedSentence  = sentence.replace(<span class="hljs-string">"dogs"</span>, <span class="hljs-string">"cats"</span>);

<span class="hljs-built_in">console</span>.log(modifiedSentence); <span class="hljs-comment">// Output: "I love cats because dogs are cute"</span>
</code></pre>
<p>In the example above, the <code>sentence</code> variable has two occurrences of the word <code>dogs</code> that I want to replace with the word <code>cats</code>. However, the <code>replace()</code> method by default only replaces the first occurrence of <code>dogs</code>.</p>
<p>Removing multiple occurrences of a word using the <code>replace()</code> method is a bit different. You achieve this by using a regular expression.</p>
<p>Let's rewrite the code using a regular expression:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> sentence = <span class="hljs-string">"I love dogs because dogs are cute"</span>;

<span class="hljs-keyword">let</span> modifiedSentence  = sentence.replace(<span class="hljs-regexp">/dogs/g</span>, <span class="hljs-string">"cats"</span>);

<span class="hljs-built_in">console</span>.log(modifiedSentence); <span class="hljs-comment">// Output: "I love cats because cats are cute"</span>
</code></pre>
<p>In the example above, I replaced all occurrences of the string <code>dogs</code> with the string <code>cats</code>.</p>
<p>Instead of passing a string as the first argument to <code>replace()</code>, I passed the regular expression <code>/dogs/g</code>, which using the <code>g</code> flag. This flag stands for <code>global</code>, and matches all occurrences of the word <code>dogs</code>, not just the first one.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this article, you learned the very basics of trimming strings and replacing characters in strings in JavaScript.</p>
<p>Specifically, you learned how to use the <code>trim()</code> method to trim both leading and trailing whitespace characters, and learned about the <code>trimStart()</code> and <code>trimEnd()</code> methods to remove only leading or only trailing whitespace characters, respectively.</p>
<p>Lastly, you learned how to use the <code>replace()</code> method to remove specific characters and replace them with others.</p>
<p>Thanks for reading, and happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ JavaScript Concatenate Strings – How JS String Concatenation Works ]]>
                </title>
                <description>
                    <![CDATA[ When coding in JavaScript, you may need to combine multiple strings to create a new, longer string. This operation is known as concatenation. In this article, you will learn five ways to concatenate strings in JavaScript. How to Concatenate Strings i... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-js-string-concatenation-works/</link>
                <guid isPermaLink="false">663a61681ea07dedd4b5da31</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Strings ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Dionysia Lemonaki ]]>
                </dc:creator>
                <pubDate>Tue, 07 May 2024 17:14:16 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/mfB1B1s4sMc/upload/138f5daa340578a0ba2da07274b59252.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>When coding in JavaScript, you may need to combine multiple strings to create a new, longer string. This operation is known as concatenation.</p>
<p>In this article, you will learn five ways to concatenate strings in JavaScript.</p>
<h2 id="heading-how-to-concatenate-strings-in-javascript-using-the-operator">How to Concatenate Strings in JavaScript Using the <code>+</code> Operator</h2>
<p>The <code>+</code> operator isn't used only for performing addition but also for concatenating strings.</p>
<p>Let’s take the following example:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">let</span> greeting = <span class="hljs-string">"Hello"</span>;
<span class="hljs-keyword">let</span> name = <span class="hljs-string">"John"</span>;

<span class="hljs-keyword">let</span> result = greeting + name;

<span class="hljs-built_in">console</span>.log(result); <span class="hljs-comment">// Output: HelloJohn</span>
</code></pre>
<p>In the code above, I created two variables named <code>greeting</code> and <code>name</code>, and stored the string values <code>Hello</code> and <code>John</code>, respectively.</p>
<p>I also created another variable named <code>result</code> and stored the result of concatenating <code>greeting</code> and <code>name</code> using the <code>+</code> operator.</p>
<p>Finally, I used <code>console.log()</code> to output the <code>result</code> to the console.</p>
<p>If you look closely at the output, <code>HelloJohn</code>, you will notice that there is no space between <code>Hello</code> and <code>John</code>. The result of joining the two strings, <code>Hello</code> and <code>John</code>, will be a new single string, <code>HelloJohn</code>.</p>
<p>When concatenating strings with the <code>+</code> operator, you have to remember to add spaces between the strings, or you will end up with unexpected output:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">let</span> greeting = <span class="hljs-string">"Hello"</span>;
<span class="hljs-keyword">let</span> name = <span class="hljs-string">"John"</span>;

<span class="hljs-keyword">let</span> result = greeting + <span class="hljs-string">" "</span> + name;

<span class="hljs-built_in">console</span>.log(result); <span class="hljs-comment">// Output: Hello John</span>
</code></pre>
<p>So, although the <code>+</code> operator is a convenient approach for basic string concatenation in JavaScript, you have to be mindful of manually separating the strings, which can lead to errors when performing more complex string concatenation.</p>
<h2 id="heading-how-to-concatenate-strings-in-javascript-using-the-operator-1">How to Concatenate Strings in JavaScript Using the <code>+=</code> Operator</h2>
<p>The <code>+=</code> operator is used when you want to add a string to an existing string.</p>
<p>Let's take the following example:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> name = <span class="hljs-string">"John "</span>;

name += <span class="hljs-string">"Doe"</span>;

<span class="hljs-built_in">console</span>.log(name); <span class="hljs-comment">// Output: John Doe</span>
</code></pre>
<p>In the example above, I created a variable <code>name</code> and stored the string value <code>John</code> with a space at the end. Note that when using the <code>+=</code> operator, you have to add spaces to separate the strings, similar to when using the <code>+</code> operator.</p>
<p>Then, I added the string <code>Doe</code> to the <code>name</code> variable. After this operation, the <code>name</code> variable will contain the string <code>John Doe</code>.</p>
<p>The <code>+=</code> operator takes the original value of the variable <code>name</code>, <code>John</code>, adds the value <code>Doe</code> and assigns the result back to the variable.</p>
<p>You can think of the line <code>name += "Doe";</code> as a shorthand for <code>name = name + "Doe"</code>.</p>
<h2 id="heading-how-to-concatenate-strings-in-javascript-using-template-literals">How to Concatenate Strings in JavaScript Using Template Literals</h2>
<p>As you saw earlier, the <code>+</code> operator is convenient for basic string concatenation. However, code can become hard to read or lead to errors when performing more complex string concatenation.</p>
<p>Template literals offer a more readable alternative and make working with strings easier.</p>
<p>Template literals use backticks (`) to enclose a string instead of single or double quotes. Inside the backticks, you can insert variables or expressions directly into strings using <code>${}</code>.</p>
<p>Let's revisit the code for concatenating strings using the <code>+</code> operator:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> greeting = <span class="hljs-string">"Hello"</span>;
<span class="hljs-keyword">let</span> name = <span class="hljs-string">"John"</span>;

<span class="hljs-keyword">let</span> result = greeting + <span class="hljs-string">" "</span> + name;

<span class="hljs-built_in">console</span>.log(result); <span class="hljs-comment">// Output: Hello John</span>
</code></pre>
<p>Here is how you would rewrite the code using template literals:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> greeting = <span class="hljs-string">"Hello"</span>;
<span class="hljs-keyword">let</span> name = <span class="hljs-string">"John"</span>;

<span class="hljs-keyword">let</span> result = <span class="hljs-string">`<span class="hljs-subst">${greeting}</span> <span class="hljs-subst">${name}</span>`</span>;

<span class="hljs-built_in">console</span>.log(result); <span class="hljs-comment">// Ouput: Hello John</span>
</code></pre>
<p>The <code>${greeting}</code> and <code>${name}</code> are like placeholders that get replaced with the actual values of the variables. <code>${greeting}</code> embeds the value of the variable <code>greeting</code> into the string, and <code>${name}</code> embeds the value of the variable <code>name</code>.</p>
<p>While both code examples achieve the same output, the code using template literals is more readable and concise compared to the code using the <code>+</code> operator.</p>
<h2 id="heading-how-to-concatenate-strings-in-javascript-using-the-concat-method">How to Concatenate Strings in JavaScript Using the <code>concat()</code> Method</h2>
<p>You can also use the built-in <code>concat()</code> method to concatenate two or more strings in JavaScript.</p>
<p>The general syntax for the <code>concat()</code> method looks something similar to the following:</p>
<pre><code class="lang-javascript">string.concat(string1, string2, ..., stringN)
</code></pre>
<p>You can call the <code>concat()</code> method on a string and pass the string(s) you want to concatenate as arguments inside the parentheses. When you pass multiple strings as arguments, you separate each string with a comma.</p>
<p>Note that the <code>concat()</code> method doesn't change the original string. Instead, it returns a new concatenated string.</p>
<p>Let's see an example of the <code>concat()</code> method in action:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> greeting = <span class="hljs-string">"Hello"</span>;
<span class="hljs-keyword">let</span> name = <span class="hljs-string">"John"</span>;

<span class="hljs-keyword">let</span> result = greeting.concat(name);

<span class="hljs-built_in">console</span>.log(result); 
<span class="hljs-built_in">console</span>.log(greeting);

<span class="hljs-comment">// Output: </span>

<span class="hljs-comment">// Hello John</span>
<span class="hljs-comment">// Hello</span>
</code></pre>
<p>In the code above, the <code>concat()</code> method is called on the initial string variable <code>name</code>, and the <code>greeting</code> string variable is passed as an argument.</p>
<p>This creates a new string, <code>Hello John</code>, where <code>name</code> is added to the end of <code>greeting</code>. The string in the <code>greeting</code> variable doesn't change.</p>
<h2 id="heading-how-to-concatenate-strings-in-javascript-using-the-join-method">How to Concatenate Strings in JavaScript Using the <code>join()</code> Method</h2>
<p>Lastly, you can concatenate strings using the built-in <code>join()</code> method.</p>
<p>The general syntax for the <code>join()</code> method looks something like the following:</p>
<pre><code class="lang-javascript">array.join(separator);
</code></pre>
<p>The <code>join()</code> method comes in handy when working with arrays of strings, as it combines all array elements into a single string separated by a separator you specify. When you don't specify a separator, a comma is used by default.</p>
<p>Let's take the following example:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> programmingLanguages = [<span class="hljs-string">"JavaScript"</span>, <span class="hljs-string">"Java"</span>, <span class="hljs-string">"Python"</span>];

<span class="hljs-keyword">let</span> result = programmingLanguages.join(<span class="hljs-string">", "</span>);

<span class="hljs-built_in">console</span>.log(result); <span class="hljs-comment">// Output: JavaScript, Java, Python</span>
</code></pre>
<p>In the example above, I first created an array called <code>programmingLanguages</code> containing three strings: <code>JavaScript</code>, <code>Java</code>, and <code>Python</code>.</p>
<p>Next, I called the <code>join()</code> method on <code>programmingLanguages</code> to concatenate all array elements into a single string and used a comma followed by a space, <code>,</code> , as the separator. Then, I stored the result in a new variable called <code>result</code>.</p>
<p>The array elements <code>JavaScript</code>, <code>Java</code>, and <code>Python</code> are joined together with a comma and a space between each element, resulting in the string <code>JavaScript, Java, Python</code>.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this article, you learned five ways of concatenating strings in JavaScript.</p>
<p>To summarise:</p>
<ul>
<li><p>The <code>+</code> operator is useful for performing basic string concatenation, but it can become less readable when performing more complex concatenations.</p>
</li>
<li><p>The <code>+=</code> operator comes in handy when you want to add a string to an existing string and modify the original string.</p>
</li>
<li><p>Template literals allow you to embed variables directly within a string and provide a readable and concise syntax.</p>
</li>
<li><p>The <code>concat()</code> method is useful when you want to concatenate strings but don't want to modify the existing strings.</p>
</li>
<li><p>The <code>join()</code> method allows you to concatenate an array of strings into a single string, with an optional separator between each array element.</p>
</li>
</ul>
<p>Thanks for reading, and happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ C Print String – How to Print a String in C ]]>
                </title>
                <description>
                    <![CDATA[ Printing strings is a fundamental operation in programming. It helps you output information, inspect and debug your code, and display prompts to users. In this article, you will learn some of the different techniques to print strings in C. What is a ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-print-a-string-in-c/</link>
                <guid isPermaLink="false">661fd38bd486669f99451ce9</guid>
                
                    <category>
                        <![CDATA[ c programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ C ]]>
                    </category>
                
                    <category>
                        <![CDATA[ string ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Dionysia Lemonaki ]]>
                </dc:creator>
                <pubDate>Wed, 17 Apr 2024 13:50:03 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/Hcfwew744z4/upload/73cd11d4c62fcaa9d6fa85514d7cb732.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Printing strings is a fundamental operation in programming. It helps you output information, inspect and debug your code, and display prompts to users.</p>
<p>In this article, you will learn some of the different techniques to print strings in C.</p>
<h2 id="heading-what-is-a-string-in-c">What is a String in C?</h2>
<p>A string is a sequence of characters, like letters, numbers, or symbols, that are grouped together. It is used to represent text in programs.</p>
<p>Strings are not a built-in data type in C. Instead, they are represented as arrays of characters, terminated with a special character called the null terminator, <code>\0</code>.</p>
<p>Here is an example of how to create a string in C:</p>
<pre><code class="lang-c"><span class="hljs-keyword">char</span> greeting[] = <span class="hljs-string">"Hello world!"</span>;
</code></pre>
<p>In the code above, I declared a character array named <code>greeting</code>, and initialized it with the string <code>Hello world!</code> enclosed within double quotes, <code>" "</code>.</p>
<p>The C compiler automatically includes the null terminator, <code>\0</code>, at the end of <code>Hello world!</code>.</p>
<h2 id="heading-how-to-print-a-string-in-c-using-the-printf-function">How to Print a String in C Using the <code>printf()</code> Function</h2>
<p>The <code>printf()</code> function is one of the most commonly used ways of printing strings in C.</p>
<p>It stands for "print formatted", and belongs to the standard input/output library, <code>stdio.h</code>. So, in order to use it, you need to first include the <code>stdio.h</code> header file at the beginning of your program.</p>
<p>Let’s take the following example:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{
  <span class="hljs-keyword">char</span> greeting[] = <span class="hljs-string">"Hello world!"</span>;

  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%s\n"</span>, greeting);
}

<span class="hljs-comment">// Output:</span>
<span class="hljs-comment">// Hello world!</span>
</code></pre>
<p>In the example above, I first included the <code>stdio.h</code> header file at the beginning of my program, which contains the declaration of the <code>printf()</code> function.</p>
<p>Next, I declared a character array named <code>greeting</code> and initialized it with the text <code>Hello world!</code> wrapped in double quotes.</p>
<p>Lastly, I used the <code>printf()</code> function to print the text <code>Hello world!</code>.</p>
<p>When printing a string using the <code>printf()</code> function, you need to use a format specifier.</p>
<p>A format specifier acts as a placeholder that tells the <code>printf()</code> function how to format and print specific types of data. They begin with a percent sign <code>%</code>, followed by a character that specifies the type of data to be formatted. The format specifier for strings is <code>%s</code>.</p>
<p>So, in the line <code>printf("%s\n", greeting);</code>, the <code>%s</code> format specifier tells <code>printf()</code> to print the string stored in the <code>greeting</code> variable followed by a newline character, <code>\n</code>.</p>
<p>Note that the <code>%s</code> format specifier doesn’t include the null terminator, <code>\0,</code> when printing strings. It prints the characters in the string until it encounters it.</p>
<h2 id="heading-how-to-print-a-string-in-c-using-the-puts-function">How to Print a String in C Using the <code>puts()</code> Function</h2>
<p>Another function used for printing strings is <code>puts()</code>.</p>
<p>Let’s take the following example:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{
  <span class="hljs-keyword">char</span> greeting[] = <span class="hljs-string">"Hello world!"</span>;

  <span class="hljs-built_in">puts</span>(greeting);
}

<span class="hljs-comment">// Output</span>
<span class="hljs-comment">// Hello world!</span>
</code></pre>
<p>In the example above, I first included the <code>stdio.h</code> header file which contains the <code>puts()</code> declaration.</p>
<p>Then, I declared a character array and initialized it with the text <code>Hello world!</code>. The string automatically ends with the null terminator, <code>\0</code>.</p>
<p>Lastly, I used the <code>puts()</code> function to print the string to the console and passed the string variable <code>greeting</code> as an argument.</p>
<p>The <code>puts()</code> function automatically adds a newline character, <code>\n</code>, at the end of the string.</p>
<p>Note that the <code>puts()</code> function is used to print null-terminated strings. A null-terminated string is a sequence of characters stored in memory followed by a character called the null terminator <code>\0</code>.</p>
<p>So far, all the examples have used only null-terminated strings, such as <code>char greeting[] = "Hello world!";</code>. In memory, it would be represented as <code>['H', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd', '!', '\0']</code>.</p>
<p>Creating non-null-terminated strings intentionally is not common in C.</p>
<p>Here is an example of a non-null-terminated string: <code>char greeting[] = {'H', 'e', 'l', 'l', 'o'};</code>This array of characters does not include the null terminator, <code>\0</code>, so it is a non-null-terminated string.</p>
<p>If you try to print a non-null-terminated string using <code>puts()</code>, you will end up getting undefined behavior, such as garbage characters at the end of the string:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{
  <span class="hljs-keyword">char</span> greeting[] = {<span class="hljs-string">'H'</span>, <span class="hljs-string">'e'</span>, <span class="hljs-string">'l'</span>, <span class="hljs-string">'l'</span>, <span class="hljs-string">'o'</span>};

  <span class="hljs-built_in">puts</span>(greeting);
}

<span class="hljs-comment">// Ouput when I run the code the first time:</span>
<span class="hljs-comment">// Helloq</span>

<span class="hljs-comment">// Ouput when I run the code a second time:</span>
<span class="hljs-comment">// Hellop</span>

<span class="hljs-comment">// Ouput when I run the code a thrid time:</span>
<span class="hljs-comment">// Hellow</span>
</code></pre>
<h2 id="heading-the-printf-function-vs-the-puts-function-whats-the-difference">The <code>printf()</code> Function VS the <code>puts()</code> Function – What's the Difference?</h2>
<p>You may be wondering what the difference is between <code>printf()</code> and <code>puts()</code>.</p>
<p>The <code>puts()</code> function prints the text as it is, without any formatting. It also automatically adds a newline character at the end of the string.</p>
<p>The <code>printf()</code> function doesn’t automatically add a new line - you have to do it explicitly.</p>
<p>However, it allows for formatted output, and gives you more control and flexibility over where and how you insert different data types into the format string:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{
    <span class="hljs-keyword">char</span> name[] = <span class="hljs-string">"John"</span>;
    <span class="hljs-keyword">int</span> age = <span class="hljs-number">30</span>;

    <span class="hljs-comment">// Printing strings using puts()</span>
    <span class="hljs-built_in">puts</span>(<span class="hljs-string">"Using puts():"</span>);
    <span class="hljs-built_in">puts</span>(<span class="hljs-string">"My name is John and I'm 30 years old."</span>);

    <span class="hljs-comment">// Printing strings usingprintf()</span>
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"\nUsing printf():\n"</span>);
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"My name is %s and I'm %d years old. \n"</span>, name, age);
}
</code></pre>
<p>In the example above, the <code>puts()</code> function prints a simple string without any formatting. It also automatically adds a newline character, <code>\n</code>, at the end of the string.</p>
<p>On the other hand, the <code>printf()</code> function formats the string and embeds two variable values. It uses format specifiers, such as <code>%s</code> for strings and <code>%d</code> for integers, to specify the type of data the variables hold, and where the variables should be inserted into the string. It also adds a newline character at the end.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this article, you learned about the two most commonly used functions in C for printing strings.</p>
<p>The <code>printf()</code> function is commonly used for printing formatted text to the console. It allows you to format your output and print strings, numbers and characters.</p>
<p>The <code>puts()</code> function is more simple compared to <code>printf()</code>. It is great for basic text output and automatically adds a newline character, <code>\n</code>, to the printed string.</p>
<p>Thank you for reading, and happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Length of C String – How to Find the Size of a String in C ]]>
                </title>
                <description>
                    <![CDATA[ When working with strings in C, you need to know how to find their length. Finding the length of a string in C is essential for many reasons. You may need to perform string manipulation, and many string manipulation functions require the length of th... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-find-length-of-c-string-with-examples/</link>
                <guid isPermaLink="false">661e927979656a65c64b216f</guid>
                
                    <category>
                        <![CDATA[ c programming ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Dionysia Lemonaki ]]>
                </dc:creator>
                <pubDate>Tue, 16 Apr 2024 15:00:09 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/JfhNqZtV56s/upload/0d58732b5a311298756b16ad3540b820.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>When working with strings in C, you need to know how to find their length.</p>
<p>Finding the length of a string in C is essential for many reasons.</p>
<p>You may need to perform string manipulation, and many string manipulation functions require the length of the string as an argument. You may also need to validate user input, compare two strings, or manage and allocate memory dynamically.</p>
<p>In this this article, you will learn different ways to find the length of a string in C.</p>
<h2 id="heading-what-are-strings-in-c">What are Strings in C?</h2>
<p>Unlike other programming languages, C doesn’t have a built-in string data type.</p>
<p>Instead, strings in C are arrays of characters that have a character called the null terminator <code>\0</code> at the end.</p>
<p>There are many ways to create a string in C. Here is an example of one way:</p>
<pre><code class="lang-c"><span class="hljs-keyword">char</span> greeting[] = <span class="hljs-string">"Hello"</span>;
</code></pre>
<p>In the code above, I created a character array named <code>greeting</code> using the <code>char</code> data type followed by square brackets, <code>[]</code>.</p>
<p>I then assigned the the string <code>Hello</code> which is surrounded by double quotes to <code>greeting</code>.</p>
<p>In this example, the size of the array is not specified explicitly – the size is determined by the size of the string assigned to it. Also, the null terminator, <code>\0,</code> is automatically added to the end of the string.</p>
<h3 id="heading-what-is-the-stringh-header-file-in-c">What is the <code>string.h</code> header file in C?</h3>
<p>The <code>string.h</code> header file provides functions for manipulating and working with strings.</p>
<p>It contains functions that complete tasks such as copying and concatenating. It also provides functions for finding the length of a string, such as <code>strlen()</code> which you will learn how to use in the following section.</p>
<p>To use functions from <code>string.h</code>, you need to include it at the beginning of your file like this:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;string.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{
  <span class="hljs-comment">// Your code goes here</span>
}
</code></pre>
<h2 id="heading-how-to-find-the-length-of-a-string-in-c-using-the-strlen-function">How to Find the Length of a String in C Using the <code>strlen()</code> Function</h2>
<p>The <code>strlen()</code> function is defined in the <code>string.h</code> header file, and is used to find the length of a string.</p>
<p>Let’s take the following example:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;string.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{
  <span class="hljs-keyword">char</span> greeting[] = <span class="hljs-string">"Hello"</span>;

  <span class="hljs-keyword">int</span> length = <span class="hljs-built_in">strlen</span>(greeting);

  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"The length is: %d\n"</span>, length);
}

<span class="hljs-comment">// Output: </span>
<span class="hljs-comment">// The length is: 5</span>
</code></pre>
<p>In the example above, I first included the <code>stdio.h</code> header file to be able to use input/output functions such as <code>printf()</code>. I also included the <code>string.h</code> header file so I could use the <code>strlen()</code> function.</p>
<p>Inside the <code>main()</code> function, I created a <code>greeting</code> array and stored the string <code>Hello</code>.</p>
<p>Then, I called the <code>strlen()</code> function and passed <code>greeting</code> as the argument – this is the string I want to find the length of.</p>
<p>Lastly, I used the value returned in <code>length</code> and printed it using the <code>printf()</code> function.</p>
<p>Note that the <code>strlen()</code> function returns the number of characters in the string excluding the null terminator (<code>\0</code>).</p>
<h2 id="heading-how-to-find-the-length-of-a-string-in-c-using-the-sizeof-operator">How to Find the Length of a String in C Using the <code>sizeof()</code> Operator</h2>
<p>Another way to find the length of a string in C is using the <code>sizeof()</code> operator.</p>
<p>The <code>sizeof()</code> operator returns the total size in bytes of a string.</p>
<p>Let's take the following example:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{
  <span class="hljs-keyword">char</span> greeting[] = <span class="hljs-string">"Hello"</span>;

  <span class="hljs-keyword">int</span> size = <span class="hljs-keyword">sizeof</span>(greeting);

  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"The size is %d bytes \n"</span>, size);
}

<span class="hljs-comment">// Output:</span>
<span class="hljs-comment">// The size is 6 bytes</span>
</code></pre>
<p>In the following example, <code>sizeof(greeting)</code> returns the entire size of the <code>greeting</code> array in bytes – including the null operator, <code>\0</code>.</p>
<p>This is not always very helpful.</p>
<p>To exclude this character, you need to subtract one from the total <code>size</code>:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{
  <span class="hljs-keyword">char</span> greeting[] = <span class="hljs-string">"Hello"</span>;

  <span class="hljs-keyword">int</span> length = <span class="hljs-keyword">sizeof</span>(greeting) - <span class="hljs-number">1</span>;

  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"The length is %d\n"</span>, length);
}

<span class="hljs-comment">// Output:</span>
<span class="hljs-comment">// The length is 5</span>
</code></pre>
<p>Although the <code>sizeof()</code> operator doesn’t require you to include the <code>string.h</code> header file like <code>strlen()</code> does, it returns the total size of the array and not the length of the string.</p>
<p>The total size of the array includes the null terminator, <code>\0</code>, whereas the length of the string is the number of characters before the null terminator.</p>
<h2 id="heading-how-to-find-the-length-of-a-string-in-c-using-a-while-loop">How to Find the Length of a String in C Using a <code>while</code> Loop</h2>
<p>Another way to find the length of a string is C is using a <code>while</code> loop.</p>
<p>The way this works is you keep iterating over the characters in a string until you reach the end of it and encounter the null terminator <code>\0</code>.</p>
<p>Let's look at the following example:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{
  <span class="hljs-keyword">char</span> greeting[] = <span class="hljs-string">"Hello"</span>;
  <span class="hljs-keyword">int</span> length = <span class="hljs-number">0</span>;

<span class="hljs-keyword">while</span> (greeting[length] != <span class="hljs-string">'\0'</span>) {
    length++;
}
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"The length is %d"</span>, length );
}

<span class="hljs-comment">// Output:</span>
<span class="hljs-comment">// The length is 5</span>
</code></pre>
<p>Let’s break down how the loop works.</p>
<p>I initialized a counter variable, <code>length</code>, to <code>0</code>. This variable will store the length of the string.</p>
<p>The condition of the <code>while</code> loop, <code>greeting[length] != '\0'</code>, checks if the character at index <code>length</code> of the string is not equal to the null terminator <code>\0</code>.</p>
<p>If it is not, the <code>length</code> variable is incremented and the loop continues and moves on to the next character in <code>greeting</code>.</p>
<p>The <code>while</code> loop iterates over <code>greeting</code> until it encounters <code>\0</code> and then the iteration stops.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this article, you learned how to find the length of a string in C.</p>
<p>You learned how to use the <code>strlen()</code> function, which returns the number of characters in the string excluding the null terminator.</p>
<p>You also learned how to use the <code>sizeof()</code> operator which does not always return the desired result as it includes the null terminator in the length.</p>
<p>Lastly, you learned how to use a <code>while</code> loop to find the length of a string. A loop counts the characters in the string until it reaches the null terminator.</p>
<p>Thank you for reading, and happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Def in C – How to Define a Function in C ]]>
                </title>
                <description>
                    <![CDATA[ Functions play a fundamental role in programming in C. They allow you to write code that is organized and easier to maintain. In this article, you'll learn the basics of defining functions in C. What is a Function in C? In programming, a function is ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-define-a-function-in-c/</link>
                <guid isPermaLink="false">661975533d607a40280a2012</guid>
                
                    <category>
                        <![CDATA[ C ]]>
                    </category>
                
                    <category>
                        <![CDATA[ c programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ functions ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Dionysia Lemonaki ]]>
                </dc:creator>
                <pubDate>Fri, 12 Apr 2024 17:54:27 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/npxXWgQ33ZQ/upload/070d054c2dafa7e4a5f90cf0d0af30eb.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Functions play a fundamental role in programming in C. They allow you to write code that is organized and easier to maintain.</p>
<p>In this article, you'll learn the basics of defining functions in C.</p>
<h2 id="heading-what-is-a-function-in-c"><strong>What is a Function in C?</strong></h2>
<p>In programming, a function is a block of code that performs a specific task.</p>
<p>Functions take inputs, process them, perform operations, and produce an output.</p>
<p>Functions are important because they organize your code and promote code reusability.</p>
<p>Instead of writing the same code again and again and repeating yourself, you write code once and then can use it whenever you want to perform that specific task.</p>
<p>In C, there are generally two types of functions:</p>
<ul>
<li><p><strong>Standard library functions</strong>. Standard library functions are provided by the C standard library and defined in header files. Examples of standard library functions include <code>printf()</code> for printing formatted output to the console and <code>scanf()</code> for reading formatted input from the user. Both are defined in the <code>stdio.h</code> header file.</p>
</li>
<li><p><strong>User-defined functions</strong>. User-defined functions are defined by you, the programmer. These functions are tailored to your program’s needs and requirements. For example, a user-defined function may calculate the sum of two numbers or check if a number is even or odd.</p>
</li>
</ul>
<p>In this article, you will learn how to create user-defined functions.</p>
<h2 id="heading-syntax-of-functions-in-c">Syntax of Functions in C</h2>
<p>Here is the general syntax of a function in C:</p>
<pre><code class="lang-c"><span class="hljs-function">return_type <span class="hljs-title">function_name</span><span class="hljs-params">(parameter)</span> </span>{
  <span class="hljs-comment">// function body with the code to be executed</span>
  <span class="hljs-keyword">return</span> value;
}
</code></pre>
<p>Let’s break it down:</p>
<ul>
<li><p>The <code>return_type</code> lets the C compiler know the type of data of the value the function will return after its execution. It can be any valid C data type such as <code>int</code>, <code>float</code>, <code>char</code>, or <code>void</code> if the function doesn’t return a value.</p>
</li>
<li><p>The <code>function_name</code> is the name you give the function. It should be meaningful and accurately describe what the function does. You will later use this to call the function.</p>
</li>
<li><p>The <code>parameter</code> is optional. A parameter is a variable a function accepts as input inside parentheses. A function can receive zero or more parameters. If the function accepts multiple parameters, they are separated by commas. Each parameter consists of the data type followed by a name.</p>
</li>
<li><p>Inside the curly braces, <code>{}</code>, is the function’s body. Here is the actual code, the instructions that perform a specific task.</p>
</li>
<li><p>Inside the function body, there can be an optional return value. You use the <code>return</code> keyword followed by the value you want to return. If the function has a <code>voidreturn_type</code>, you don't need to specify a return value.</p>
</li>
</ul>
<h2 id="heading-how-to-call-a-function-in-c">How to Call a Function in C</h2>
<p>Here is the syntax for calling a function in C:</p>
<pre><code class="lang-c">function_name(arguments);
</code></pre>
<p>Let's break it down:</p>
<ul>
<li><p><code>function_name</code> is the name of the function you want to call. It should be the same name you used to define your function.</p>
</li>
<li><p><code>arguments</code> are the values you pass to the function. If the function accepts any parameters, you pass the arguments in parentheses when you call the function. Each argument is separated by a comma.</p>
</li>
</ul>
<p>If the function returns a value, you can store it in a variable for later use:</p>
<pre><code class="lang-c">data_type result = function_name(arguments);
</code></pre>
<h2 id="heading-how-to-define-and-call-a-function-in-c-example">How to Define and Call a Function in C Example</h2>
<p>Let's look at a simple function that adds two numbers:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">add</span><span class="hljs-params">(<span class="hljs-keyword">int</span> num1, <span class="hljs-keyword">int</span> num2)</span> </span>{
    <span class="hljs-keyword">return</span> num1 + num2;
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{
    <span class="hljs-keyword">int</span> num1, num2, result;

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Enter first number: "</span>);
    <span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d"</span>, &amp;num1);

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Enter second number: "</span>);
    <span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d"</span>, &amp;num2);

    result = add(num1, num2);

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"The sum of %d and %d is %d\n"</span>, num1, num2, result);

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}

<span class="hljs-comment">// Output: </span>

<span class="hljs-comment">// Enter first number: 2</span>
<span class="hljs-comment">// Enter second number: 3</span>
<span class="hljs-comment">// The sum of 2 and 3 is 5</span>
</code></pre>
<p>Let’s break down the code step by step.</p>
<h3 id="heading-include-the-header-file">Include the Header File</h3>
<p>I first included the library <code>stdio.h</code> with the line <code>#include &lt;stdio.h&gt;</code>.</p>
<p>This line includes the standard input-output library (<code>&lt;stdio.h&gt;</code>), which gives you access to the <code>printf()</code> and <code>scanf()</code> functions. Now, you can receive user input and print text to the console.</p>
<h3 id="heading-define-the-add-function">Define the <code>add</code> Function</h3>
<p>Next, I defined the following function:</p>
<pre><code class="lang-c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">add</span><span class="hljs-params">(<span class="hljs-keyword">int</span> num1, <span class="hljs-keyword">int</span> num2)</span> </span>{
    <span class="hljs-keyword">return</span> num1 + num2;
}
</code></pre>
<p>This function has an <code>int</code> return type, which indicates that it will return an integer value after execution.</p>
<p>The function is named <code>add</code>, and inside parentheses, it accepts the integer parameters <code>num1</code> and <code>num2</code>.</p>
<p>Within the curly braces, the function body contains the function code. In this case, the function code consists of only the return statement <code>return num1 + num2;</code>. This code calculates the sum of <code>num1</code> and <code>num2</code> using the <code>+</code> operator, and returns the result.</p>
<p>The <code>add()</code> function is defined before being used in the <code>main()</code> function later on. In C, functions must be defined before they are used. By placing the <code>add()</code> function definition above the <code>main()</code> function, the compiler knows about it when it encounters the function call in <code>main()</code>.</p>
<h3 id="heading-define-the-main-function">Define the <code>main()</code> Function</h3>
<p>Next, I defined the <code>main()</code> function, which is the starting point of every C program:</p>
<pre><code class="lang-c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{
    <span class="hljs-keyword">int</span> num1, num2, result;

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Enter first number: "</span>);
    <span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d"</span>, &amp;num1);

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Enter second number: "</span>);
    <span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d"</span>, &amp;num2);

    result = add(num1, num2);

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"The sum of %d and %d is %d\n"</span>, num1, num2, result);

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Inside the <code>main()</code> function, I first declared the integer variables <code>num1</code>, <code>num2</code>, and <code>result</code>.</p>
<p>Note that <code>num1</code> and <code>num2</code> variables are different from the <code>num1</code> and <code>num2</code> parameters that the <code>add()</code> function receives. These two variables will store the numbers that the user will enter.</p>
<p>Then, I prompted the user to enter the first number using the <code>printf()</code> function, and used the <code>scanf()</code> function to read the input and store it in the variable <code>num1</code>. The <code>%d</code> format specifier is used to indicate that <code>scanf()</code> should expect an integer input.</p>
<p>I followed the exact same procedure for receiving and storing the second number.</p>
<p>Next, I called the <code>add()</code> function with the <code>num1</code> and <code>num2</code> as arguments. The <code>add()</code> function will add the two numbers together. The result of the calculation is then stored in the <code>result</code> variable.</p>
<p>Following that, I used the <code>printf()</code> function to print the <code>num1</code>, <code>num2</code> and <code>result</code> variables to the console. The format specifier <code>%d</code> is used to print integer values.</p>
<p>Lastly, the line <code>return 0;</code> is a statement that indicates that the program executed successfully. When a C program terminates, it returns an exit status to the operating system, with <code>0</code> typically indicating the program executed without any errors.</p>
<h3 id="heading-execute-the-program">Execute the Program</h3>
<p>When the program is executed, the <code>main()</code> function is called first.</p>
<p>You first see the prompt <code>Enter first number:</code>. In my case, I entered <code>2</code> as the first number.</p>
<p>Once you enter a number, you will see the second prompt: <code>Enter second number:</code>. I entered the number <code>3</code> as the second number.</p>
<p>Then, the <code>add()</code> function is called, which adds the numbers <code>2</code> and <code>3</code>.</p>
<p>Lastly, the line <code>The sum of 2 and 3 is 5</code> is printed to the console.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this article, you learned the very basics of defining functions in C.</p>
<p>Specifically, you learned about the two different types of functions in C, and the general syntax for defining your own functions.</p>
<p>Lastly, you saw an example of a simple function that added two numbers and returned the result.</p>
<p>Thanks for reading, and happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ The C Programming Handbook for Beginners ]]>
                </title>
                <description>
                    <![CDATA[ C is one of the oldest, most widely known, and most influential programming languages. It is used in many industries because it is a highly flexible and powerful language. Learning C is a worthwhile endeavor – no matter your starting point or aspirat... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/the-c-programming-handbook-for-beginners/</link>
                <guid isPermaLink="false">66b1e4bf0938e6258a76bbdd</guid>
                
                    <category>
                        <![CDATA[ beginners guide ]]>
                    </category>
                
                    <category>
                        <![CDATA[ c programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ handbook ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Dionysia Lemonaki ]]>
                </dc:creator>
                <pubDate>Tue, 29 Aug 2023 20:38:16 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1726039032547/73b9df27-a4f7-4ee2-81c0-d1e3db521cdb.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>C is one of the oldest, most widely known, and most influential programming languages.</p>
<p>It is used in many industries because it is a highly flexible and powerful language.</p>
<p>Learning C is a worthwhile endeavor – no matter your starting point or aspirations – because it builds a solid foundation in the skills you will need for the rest of your programming career.</p>
<p>It helps you understand how a computer works underneath the hood, such as how it stores and retrieves information and what the internal architecture looks like.</p>
<p>With that said, C can be a difficult language to learn, especially for beginners, as it can be cryptic.</p>
<p>This handbook aims to teach you C programming fundamentals and is written with the beginner programmer in mind.</p>
<p>There are no prerequisites, and no previous knowledge of any programming concepts is assumed.</p>
<p>If you have never programmed before and are a complete beginner, you have come to the right place.</p>
<p>Here is what you are going to learn in this handbook:</p>
<ul>
<li><p><a class="post-section-overview" href="#chapter-1">Chapter 1: Introduction to C Programming</a></p>
</li>
<li><p><a class="post-section-overview" href="#chapter-2">Chapter 2: Variables and Data Types in C</a></p>
</li>
<li><p><a class="post-section-overview" href="#chapter-3">Chapter 3: Operators in C</a></p>
</li>
<li><p><a class="post-section-overview" href="#chapter-4">Chapter 4: Conditional Statements in C</a></p>
</li>
<li><p><a class="post-section-overview" href="#chapter-5">Chapter 5: Loops in C</a></p>
</li>
<li><p><a class="post-section-overview" href="#chapter-6">Chapter 6: Arrays in C</a></p>
</li>
<li><p><a class="post-section-overview" href="#chapter-7">Chapter 7: Strings in C</a></p>
</li>
<li><p><a class="post-section-overview" href="#further-learning">Further learning: Advanced C Topics</a></p>
</li>
</ul>
<p>Without further ado, let’s get started with learning C!</p>
<h2 id="heading-chapter-1-introduction-to-c-programming">Chapter 1: Introduction to C Programming</h2>
<p>In this introductory chapter, you will learn the main characteristics and use cases of the C programming language.</p>
<p>You will also learn the basics of C syntax and familiarize yourself with the general structure of all C programs.</p>
<p>By the end of the chapter, you will have set up a development environment for C programming so you can follow along with the coding examples in this book on your local machine.</p>
<p>You will have successfully written, compiled, and executed your first simple C program that prints the text "Hello, world!" to the screen.</p>
<p>You will have also learned some core C language features, such as comments for documenting and explaining your code and escape sequences for representing nonprintable characters in text.</p>
<h3 id="heading-what-is-programming">What Is Programming?</h3>
<p>Computers are not that smart.</p>
<p>Even though they can process data tirelessly and can perform operations at a very high speed, they cannot think for themselves. They need someone to tell them what to do next.</p>
<p>Humans tell computers what to do and exactly how to do it by giving them detailed and step-by-step instructions to follow.</p>
<p>A collection of detailed instructions is known as a program.</p>
<p>Programming is the process of writing the collection of instructions that a computer can understand and execute to perform a specific task and solve a particular problem.</p>
<p>A programming language is used to write the instructions.</p>
<p>And the humans who write the instructions and supply them to the computer are known as programmers.</p>
<h4 id="heading-low-level-vs-high-level-vs-middle-level-programming-languages-whats-the-difference">Low-level VS High-Level VS Middle-level Programming Languages – What's The Difference?</h4>
<p>There are three types of programming languages: low-level languages, high-level languages, and middle-level languages.</p>
<p>Low-level languages include machine language (also known as binary) and assembly language.</p>
<p>Both languages provide little to no abstraction from the computer's hardware. The language instructions are closely related to or correspond directly to specific machine instructions.</p>
<p>This 'closeness to the machine' allows for speed, efficiency, less consumption of memory, and fine-grained control over the computer's hardware.</p>
<p>Machine language is the lowest level of programming languages.</p>
<p>The instructions consist of series of <code>0</code>s and <code>1</code>s that correspond directly to a particular computer’s instructions and locations memory.</p>
<p>Instructions are also directly executed by the computer’s processor.</p>
<p>Even though machine language was the language of choice for writing programs in the early days of computing, it is not a human-readable language and is time-consuming to write.</p>
<p>Assembly language allows the programmer to work closely with the machine on a slightly higher level.</p>
<p>It uses mnemonics and symbols that correspond directly to a particular machine’s instruction set instead of using sequences of <code>0</code>s and <code>1</code>s.</p>
<p>Next, high-level languages, like Python and JavaScript, are far removed from the instruction set of a particular machine architecture.</p>
<p>Their syntax resembles the English language, making them easier to work with and understand.</p>
<p>Programs written in high-level languages are also portable and machine-independent. That is, a program can run on any system that supports that language.</p>
<p>With that said, they tend to be slower, consume more memory, and make it harder to work with low-level hardware and systems because of how abstract they are.</p>
<p>Lastly, middle-level languages, like C and C++, act as a bridge between low-level and high-level programming languages.</p>
<p>They allow for closeness and a level of control over computer hardware. At the same time, they also offer a level of abstraction with instructions that are more human-readable and understandable for programmers to write.</p>
<h3 id="heading-what-is-the-c-programming-language">What Is the C Programming Language?</h3>
<p>C is a general-purpose and procedural programming language.</p>
<p>A procedural language is a type of programming language that follows a step-by-step approach to solving a problem.</p>
<p>It uses a series of instructions, otherwise known as procedures or functions, that are executed in a specific order to perform tasks and accomplish goals. These intructions tell the computer step by step what to do and in what order.</p>
<p>So, C programs are divided into smaller, more specific functions that accomplish a certain task and get executed sequentially, one after another, following a top-down approach.</p>
<p>This promotes code readability and maintainability.</p>
<h4 id="heading-a-brief-history-of-the-c-programming-language">A Brief History of the C Programming Language</h4>
<p>C was developed in the early 1970s by Dennis Ritchie at AT&amp;T Bell Laboratories.</p>
<p>The development of C was closely tied to the development of the Unix operating system at Bell Labs.</p>
<p>Historically, operating systems were typically written in Assembly language and without portability in mind.</p>
<p>During the development of Unix, there was a need for a more efficient and portable programming language for writing operating systems.</p>
<p>Dennis Ritchie went on to create a language called B, which was an evolution from an earlier language called BCPL (Basic Combined Programming Language).</p>
<p>It aimed to bridge the gap between the low-level capabilities of Assembly and the high-level languages used at the time, such as Fortran.</p>
<p>B was not powerful enough to support Unix development, so Dennis Ritchie developed a new language that took inspiration from B and BCPL and had some additional features. He named this language C.</p>
<p>C’s simple design, speed, efficiency, performance, and close relationship with a computer’s hardware made it an attractive choice for systems programming. This led to the Unix operating system being rewritten in C.</p>
<h4 id="heading-c-language-characteristics-and-use-cases">C Language Characteristics and Use Cases</h4>
<p>Despite C being a relatively old language (compared to other, more modern, programming languages in use today), it has stood the test of time and still remains popular.</p>
<p>According to the <a target="_blank" href="https://www.tiobe.com/tiobe-index/">TIOBE index</a>, which measures the popularity of programming languages each month, C is the second most popular programming language as of August 2023.</p>
<p>This is because C is considered the "mother of programming languages" and is one of the most foundational languages of computer science.</p>
<p>Most modern and popular languages used today either use C under the hood or are inspired by it.</p>
<p>For example, Python’s default implementation and interpreter, CPython, is written in C. And languages such as C++ and C# are extensions of C and provide additional functionality.</p>
<p>Even though C was originally designed with systems programming in mind, it is widely used in many other areas of computing.</p>
<p>C programs are portable and easy to implement, meaning they can be executed across different platforms with minimal changes.</p>
<p>C also allows for efficient and direct memory manipulation and management, making it an ideal language for performance-critical applications.</p>
<p>And C provides higher-level abstractions along with low-level capabilities, which allows programmers to have fine-grained control over hardware resources when they need to.</p>
<p>These characteristics make C an ideal language for creating operating systems, embedded systems, system utilities, Internet of things (IoT) devices, database systems, and various other applications.</p>
<p>C is used pretty much everywhere today.</p>
<h3 id="heading-how-to-set-up-a-development-environment-for-c-programming-on-your-local-machine">How to Set Up a Development Environment for C Programming on Your Local Machine</h3>
<p>To start writing C programs on your local machine, you will need the following:</p>
<ul>
<li><p>A C Compiler</p>
</li>
<li><p>An Integrated Development Environment (IDE)</p>
</li>
</ul>
<p>C is a compiled programming language, like Go, Java, Swift, and Rust.</p>
<p>Compiled languages are different from interpeted languages, such as PHP, Ruby, Python, and JavaScript.</p>
<p>The difference between compiled and interpeted languages is that a compiled language is directly translated to machine code all at once.</p>
<p>This process is done by a special program called a compiler.</p>
<p>The compiler reads the entire source code, checks it for errors, and then translates the entire program into machine code. This is a language the computer can understand and it's directly associated with the particular instructions of the computer.</p>
<p>This process creates a standalone binary executable file containing sequences of <code>0</code>s and <code>1</code>s which is a more computer-friendly form of the initial source code. This file contains instructions that the computer can understand and run directly.</p>
<p>An interpeted language, on the other hand, doesn’t get translated into machine code all at once and doesn’t produce a binary executable file.</p>
<p>Instead, an interpreter reads and executes the source code one instruction at a time, line by line. The interpreter reads each line, translates it into machine code, and then immediately runs it.</p>
<p>If you are using a Unix or a Unix-like system such as macOS or Linux, you probably have the popular <a target="_blank" href="https://gcc.gnu.org/">GNU Compiler Collection (GCC)</a> already installed on your machine.</p>
<p>If you are running either of those operating systems, open the terminal application and type the following command:</p>
<pre><code class="lang-plaintext">gcc --version
</code></pre>
<p>If you're using macOS and have not installed the command line developer tools, a dialog box will pop-up asking you to install them – so if you see that, go ahead and do so.</p>
<p>If you have already installed the command line tools, you will see an output with the version of the compiler, which will look similar to the following:</p>
<pre><code class="lang-plaintext">Apple clang version 14.0.0 (clang-1400.0.29.202)
</code></pre>
<p>If you are using Windows, you can check out <a target="_blank" href="https://www.codeblocks.org/">Code::Blocks</a> or look into installing <a target="_blank" href="https://learn.microsoft.com/en-us/windows/wsl/install">Linux on Windows with WSL</a>. Feel free to pick whatever programming environment works best for you.</p>
<p>An IDE is where you write, edit, save, run, and debug your C programs. You can think of it like a word processor but for writing code.</p>
<p><a target="_blank" href="https://code.visualstudio.com/">Visual Studio Code</a> is a great editor for writing code, and offers many IDE-like features.</p>
<p>It is free, open-source, supports many programming languages, and is available for all operating systems.</p>
<p>Once you have downloaded Visual Studio Code, install the <a target="_blank" href="https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools">C/C++ extension</a>.</p>
<p>It’s also a good idea to enable auto-saving by selecting: "File" -&gt; "Auto Save".</p>
<p>If you want to learn more, you can look through the <a target="_blank" href="https://code.visualstudio.com/docs/languages/cpp">Visual Studio Code documentation for C/C++</a>.</p>
<p>With your local machine all set up, you are ready to write your first C program!</p>
<h3 id="heading-how-to-write-your-first-c-program">How to Write Your First C Program</h3>
<p>To get started, open Visual Studio Code and create a new folder for your C program by navigating to "File" -&gt; "Open" -&gt; "New Folder".</p>
<p>Give this folder a name, for example, <code>c-practice</code>, and then select "Create" -&gt; “Open".</p>
<p>You should now have the <code>c-practice</code> folder open in Visual Studio Code.</p>
<p>Inside the folder you just created, create a new C file.</p>
<p>Hold down the <code>Command</code> key and press <code>N</code> on macOS or hold down the <code>Control</code> and press <code>N</code> for Windows/Linux to create an <code>Untitled-1</code> file.</p>
<p>Hold down the <code>Command</code> key and press <code>S</code> on macOS or hold down the <code>Control</code> key and press <code>S</code> for Windows/Linux, and save the file as a <code>main.c</code> file.</p>
<p>Finally, click "Save".</p>
<p>Make sure that you save the file you created with a <code>.c</code> extension, or it won’t be a valid C file.</p>
<p>You should now have the <code>main.c</code> file you just created open in Visual Studio Code.</p>
<p>Next, add the following code:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

  <span class="hljs-comment">// output 'Hello, world!' to the console</span>

  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Hello, world!\n"</span>);
}
</code></pre>
<p>Let’s go over each line and explain what is happening in the program.</p>
<h4 id="heading-what-are-header-files-in-c">What Are Header Files in C?</h4>
<p>Let’s start with the first line, <code>#include &lt;stdio.h&gt;</code>.</p>
<p>The <code>#include</code> part of <code>#include &lt;stdio.h&gt;</code> is a preprocessor command that tells the C compiler to include a file.</p>
<p>Specifically, it tells the compiler to include the <code>stdio.h</code> header file.</p>
<p>Header files are external libraries.</p>
<p>This means that some developers have written some functionality and features that are not included at the core of the C language.</p>
<p>By adding header files to your code, you get additional functionality that you can use in your programs without having to write the code from scratch.</p>
<p>The <code>stdio.h</code> header file stands for standard input-output.</p>
<p>It contains function definitions for input and output operations, such as functions for gathering user data and printing data to the console.</p>
<p>Specifically, it provides functions such as <code>printf()</code> and <code>scanf()</code>.</p>
<p>So, this line is necessary for the function we have later on in our program, <code>printf()</code>, to work.</p>
<p>If you don't include the <code>stdio.h</code> file at the top of your code, the compiler will not understand what the <code>printf()</code> function is.</p>
<h4 id="heading-what-is-the-main-function-in-c">What is the <code>main()</code> function in C?</h4>
<p>Next, <code>int main(void) {}</code> is the main function and starting point of every C program.</p>
<p>It is the first thing that is called when the program is executed.</p>
<p>Every C program must include a <code>main()</code> function.</p>
<p>The <code>int</code> keyword in <code>int main(void) {}</code> indicates the return value of the <code>main()</code> function.</p>
<p>In this case, the function will return an integer number.</p>
<p>And the <code>void</code> keyword inside the <code>main()</code> function indicates that the function receives no arguments.</p>
<p>Anything inside the curly braces, <code>{}</code>, is considered the body of the function – here is where you include the code you want to write. Any code written here will always run first.</p>
<p>This line acts as a boilerplate and starting point for all C programs. It lets the computer know where to begin reading the code when it executes your programs.</p>
<h4 id="heading-what-are-comments-in-c">What Are Comments in C?</h4>
<p>In C programming, comments are lines of text that get ignored by the compiler.</p>
<p>Writing comments is a way to provide additional information and describe the logic, purpose, and functionality of your code.</p>
<p>Comments provide a way to document your code and make it more readable and understandable for anyone who will read and work with it.</p>
<p>Having comments in your source code is also helpful for your future self. So when you come back to the code in a few months and don't remember how the code works, these comments can help.</p>
<p>Comments are also helpful for debugging and troubleshooting. You can temporarily comment out lines of code to isolate problems.</p>
<p>This will allow you to ignore a section of code and concentrate on the piece of code you are testing and working on without having to delete anything.</p>
<p>There are two types of comments in C:</p>
<ul>
<li><p>Single-line comments</p>
</li>
<li><p>Multi-line comments</p>
</li>
</ul>
<p>Single-line comments start with two forward slashes, <code>//</code>, and continue until the end of the line.</p>
<p>Here is the syntax for creating a single-line comment in C:</p>
<pre><code class="lang-c"><span class="hljs-comment">// I am a single-line comment</span>
</code></pre>
<p>Any text written after the forward slashes and on the same line gets ignored by the compiler.</p>
<p>Multi-line comments start with a forward slash, <code>/</code>, followed by an asterisk, <code>*</code>, and end with an asterisk, followed by a forward slash.</p>
<p>As the name suggests, they span multiple lines.</p>
<p>They offer a way to write slightly longer explanations or notes within your code and explain in more detail how it works.</p>
<p>Here is the syntax for creating a multi-line comment in C:</p>
<pre><code class="lang-c"><span class="hljs-comment">/*
This is
a multi-line
comment
*/</span>
</code></pre>
<h3 id="heading-what-is-the-printf-function-in-c">What is the <code>printf()</code> function in C?</h3>
<p>Inside the function's body, the line <code>printf("Hello, World!\n");</code> prints the text <code>Hello, World!</code> to the console (this text is also known as a string).</p>
<p>Whenever you want to display something, use the <code>printf()</code> function.</p>
<p>Surround the text you want to display in double quotation marks, <code>""</code>, and make sure it is inside the parentheses of the <code>printf()</code> function.</p>
<p>The semicolon, <code>;</code>, terminates the statement. All statements need to end with a semicolon in C, as it identifies the end of the statement.</p>
<p>You can think of a semicolon similar to how a full stop/period ends a sentence.</p>
<h3 id="heading-what-are-escape-sequences-in-c">What Are Escape Sequences in C?</h3>
<p>Did you notice the <code>\n</code> at the end of <code>printf("Hello, World!\n");</code>?</p>
<p>It's called an escape sequence, which means that it is a character that creates a newline and tells the cursor to move to the next line when it sees it.</p>
<p>In programming, an escape sequence is a combination of characters that represents a special character within a string.</p>
<p>They provide a way to include special characters that are difficult to represent directly in a string.</p>
<p>They consist of a backslash, <code>\</code>, also known as the escape character, followed by one or more additional characters.</p>
<p>The escape sequence for a newline character is <code>\n</code>.</p>
<p>Another escape sequence is <code>\t</code>. The <code>\t</code> represrents a tab character, and will insert a space within a string.</p>
<h3 id="heading-how-to-compile-and-run-your-first-c-program">How to Compile and Run Your first C Program</h3>
<p>In the previous section, you wrote your first C program:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

  <span class="hljs-comment">// output 'Hello, world!' to the console</span>

  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Hello, world!\n"</span>);
}
</code></pre>
<p>Any code you write in the C programming language is called source code.</p>
<p>Your computer doesn’t understand any of the C statements you have written, so this source code needs to be translated into a different format that the computer can understand. Here is where the compiler you installed earlier comes in handy.</p>
<p>The compiler will read the program and translate it into a format closer to the computer’s native language and make your program suitable for execution.</p>
<p>You will be able to see the output of your program, which should be <code>Hello, world!</code>.</p>
<p>The compilation of a C program consists of four steps: preprocessing, compilation, assembling, and linking.</p>
<p>The first step is preprocessing.</p>
<p>The preprocessor scans through the source code to find preprocessor directives, which are any lines that start with a <code>#</code> symbol, such as <code>#include</code> .</p>
<p>Once the preprocessor finds these lines, it substitutes them with something else.</p>
<p>For example, when the preprocessor finds the line <code>#include &lt;stdio.h&gt;</code>, the <code>#include</code> tells the preprocessor to include all the code from the <code>stdio.h</code> header file.</p>
<p>So, it replaces the <code>#include &lt;stdio.h&gt;</code> line with the actual contents of the <code>stdio.h</code> file.</p>
<p>The output of this phase is a modified version of the source code.</p>
<p>After preprocessing, the next step is the compilation phase, where the modified source code gets translated into the corresponding assembly code.</p>
<p>If there are any errors, compilation will fail, and you will need to fix the errors to continue.</p>
<p>The next step is the assembly phase, where the assembler converts the generated assembly code statements into machine code instructions.</p>
<p>The output of this phase is an object file, which contains the machine code instructions.</p>
<p>The last step is the linking phase.</p>
<p>Linking is the process of combining the object file generated from the assembly phase with any necessary libraries to create the final executable binary file.</p>
<p>Now, let’s go over the commands you need to enter to compile your <code>main.c</code> file.</p>
<p>In Visual Studio Code, open the built-in terminal by selecting "Terminal" -&gt; "New Terminal".</p>
<p>Inside the terminal, enter the following command:</p>
<pre><code class="lang-plaintext">gcc main.c
</code></pre>
<p>The <code>gcc</code> part of the command refers to the C compiler, and <code>main.c</code> is the file that contains the C code that you want to compile.</p>
<p>Next, enter the following command:</p>
<pre><code class="lang-plaintext">ls
</code></pre>
<p>The <code>ls</code> command lists the contents of the current directory.</p>
<pre><code class="lang-plaintext">a.out  main.c
</code></pre>
<p>The output of this command shows an <code>a.out</code> file – this is the executable file containing the source code statements in their corresponding binary instructions.</p>
<p>The <code>a.out</code> is the default name of the executable file created during the compilation process.</p>
<p>To run this file, enter the following command:</p>
<pre><code class="lang-plaintext">./a.out
</code></pre>
<p>This command tells the computer to look in the current directory, <code>./</code>, for a file named <code>a.out</code>.</p>
<p>The above command generates the following output:</p>
<pre><code class="lang-plaintext">Hello, world!
</code></pre>
<p>You also have the option to name the executable file instead of leaving it with the default <code>a.out</code> name.</p>
<p>Say you wanted to name the executable file <code>helloWorld</code>.</p>
<p>If you wanted to do this, you would need to enter the following command:</p>
<pre><code class="lang-plaintext">gcc -o helloWorld main.c
</code></pre>
<p>This command with the <code>-o</code> option (which stands for output) tells the <code>gcc</code> compiler to create an executable file named <code>helloWorld</code>.</p>
<p>To run the new executable file that you just created, enter the following command:</p>
<pre><code class="lang-plaintext">./helloWorld
</code></pre>
<p>This is the output of the above command:</p>
<pre><code class="lang-plaintext">Hello, world!
</code></pre>
<p>Note that whenever you make a change to your source code file, you have to repeat the process of compiling and running your program from the beginning to see the changes you made.</p>
<h2 id="heading-chapter-2-variables-and-data-types">Chapter 2: Variables and Data Types</h2>
<p>In this chapter, you will learn the basics of variables and data types – the fundamental storage units that allow you to preserve and manipulate data in your programs.</p>
<p>By the end of this chapter, you will know how to declare and initialize variables.</p>
<p>You will also have learned about various data types available in C, such as integers, floating-point numbers, and characters, which dictate how information is processed and stored within a program's memory.</p>
<p>Finally, you'll have learned how to receive user input in your programs, and how to use constants to store values that you don't want to be changed.</p>
<h3 id="heading-what-is-a-variable-in-c">What Is a Variable in C?</h3>
<p>Variables store different kind of data in the computer's memory, and take up a certain amount of space.</p>
<p>By storing information in a variable, you can retrieve and manipule it, perform various calculations, or even use it to make decisions in your program.</p>
<p>The stored data is given a name, and that is how you are able to access it when you need it.</p>
<h3 id="heading-how-to-declare-variables-in-c">How to Declare Variables in C</h3>
<p>Before you can use a variable, you need to declare it – this step lets the compiler know that it should allocate some memory for it.</p>
<p>C is a strongly typed language, so to declare a variable in C, you first need to specify the type of data the variable will hold, such as an integer to store a whole number, a floating-point number for numbers with decimal places, or a char for a single character.</p>
<p>That way, during compilation time, the compiler knows if the variable is able to perform the actions it was set out to do.</p>
<p>Once you have specified the data type, you give the variable a name.</p>
<p>The general syntax for declaring variables looks something like this:</p>
<pre><code class="lang-plaintext">data_type variable_name;
</code></pre>
<p>Let's take the following example:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

    <span class="hljs-keyword">int</span> age;
}
</code></pre>
<p>In the example above, I declared a variable named <code>age</code> that will hold integer values.</p>
<h4 id="heading-what-are-the-naming-conventions-for-variables-in-c">What Are the Naming Conventions for Variables in C?</h4>
<p>When it comes to variable names, they must begin either with a letter or an underscore.</p>
<p>For example, <code>age</code> and <code>_age</code> are valid variable names.</p>
<p>Also, they can contain any uppercase or lowercase letters, numbers, or an underscore character. There can be no other special symbols besides an underscore.</p>
<p>Lastly, variable names are case-sensitive. For example, <code>age</code> is different from <code>Age</code>.</p>
<h3 id="heading-how-to-initialize-variables-in-c">How to Initialize Variables in C</h3>
<p>Once you've declared a variable, it is a good practice to intialize it, which involves assigning an initial value to the variable.</p>
<p>The general syntax for initialzing a variable looks like this:</p>
<pre><code class="lang-plaintext">data_type variable_name = value;
</code></pre>
<p>The assignment operator, <code>=</code>, is used to assign the <code>value</code> to the <code>variable_name</code>.</p>
<p>Let's take the previous example and assign <code>age</code> a value:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

    <span class="hljs-keyword">int</span> age;

    age = <span class="hljs-number">29</span>;
}
</code></pre>
<p>I initialized the variable <code>age</code> by assigning it an integer value of <code>29</code>.</p>
<p>With that said, you can combine the initialization and declaration steps instead of performing them separately:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

    <span class="hljs-comment">// declaration + initialization</span>
    <span class="hljs-keyword">int</span> age = <span class="hljs-number">29</span>;
}
</code></pre>
<h3 id="heading-how-to-update-variable-values-in-c">How to Update Variable Values in C</h3>
<p>The values of variables can change.</p>
<p>For example, you can change the value of <code>age</code> without having to specify its type again.</p>
<p>Here is how you would change its value from <code>29</code> to <code>30</code>:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

    <span class="hljs-comment">// the variable age with its original value</span>
    <span class="hljs-keyword">int</span> age = <span class="hljs-number">29</span>;

    <span class="hljs-comment">// changing the value of age</span>
    <span class="hljs-comment">// the new value will be 30</span>
    age = <span class="hljs-number">30</span>;
}
</code></pre>
<p>Note that the data type of the new value being assigned must match the declared data type of the variable.</p>
<p>If it doesn't, the program will not run as expected. The compiler will raise an error during compilation time.</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

    <span class="hljs-keyword">int</span> age = <span class="hljs-number">29</span>;

    <span class="hljs-comment">/*
    trying to assign a floating-point value
    to a variable with type int
    will cause an error in your program
    */</span>
    age = <span class="hljs-number">29.5</span>;
}
</code></pre>
<h3 id="heading-what-are-the-basic-data-types-in-c">What Are the Basic Data Types in C?</h3>
<p>Data types specify the type of form that information can have in C programs. And they determine what kind of operations can be performed on that information.</p>
<p>There are various built-in data types in C such as <code>char</code>, <code>int</code>, and <code>float</code>.</p>
<p>Each of the data types requires different allocation of memory.</p>
<p>Before exploring each one in more detail, let’s first go over the difference between signed and unsigned data types in C.</p>
<p>Signed data types can represent both positive and negative values.</p>
<p>On the other hand, unsigned data types can represent only non-negative values (zero and positive values).</p>
<p>Wondering when to use signed and when to use unsigned data types?</p>
<p>Use signed data types when you need to represent both positive and negative values, such as when working with numbers that can have positive and negative variations.</p>
<p>And use unsigned data types when you want to ensure that a variable can only hold non-negative values, such as when dealing with quantities.</p>
<p>Now, let's look at C data types in more detail.</p>
<h4 id="heading-what-is-the-char-data-type-in-c">What Is the <code>char</code> Data Type in C?</h4>
<p>The most basic data type in C is <code>char</code>.</p>
<p>It stands for "character" and it is one of the simplest and most fundamental data types in the C programming language.</p>
<p>You use it to store a single individual character such as an uppercase and lowercase letter of the ASCII (American Standard Code for Information Interchange) chart.</p>
<p>Some examples of <code>char</code>s are <code>'a'</code> and <code>'Z'</code>.</p>
<p>It can also store symbols such as <code>'!'</code>, and digits such as <code>'7'</code>.</p>
<p>Here is an example of how to create a variable that will hold a <code>char</code> value:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

    <span class="hljs-keyword">char</span> initial = <span class="hljs-string">'D'</span>;

 }
</code></pre>
<p>Notice how I used single quotation marks around the single character.</p>
<p>This is because you can't use double quotes when working with <code>char</code>s.</p>
<p>Double quotes are used for strings.</p>
<p>Regarding memory allocation, a signed <code>char</code> lets you store numbers ranging from <code>[-128 to 127</code>], and uses at least 1 byte (or 8 bits) of memory.</p>
<p>An unsigned char stores numbers ranging from <code>[0 to 255]</code>.</p>
<h4 id="heading-what-is-the-int-data-type-in-c">What Is the <code>int</code> Data Type in C?</h4>
<p>An <code>int</code> is a an integer, which is also known as a whole number.</p>
<p>It can hold a positive or negative value or <code>0</code>, but it can't hold numbers that contain decimal points (like <code>3.5</code>).</p>
<p>Some examples of integers are <code>0</code>, <code>-3</code>,and <code>9</code>.</p>
<p>Here is how you create a variable that will hold an <code>int</code> value:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

    <span class="hljs-keyword">int</span> age = <span class="hljs-number">29</span>;
 }
</code></pre>
<p>When you declare an <code>int</code>, the computer allocates at least 2 bytes (or 16 bits) of memory.</p>
<p>With that said, on most modern systems, an <code>int</code> typically allocates 4 bytes (or 32 bits) of memory.</p>
<p>The range of available numbers for a signed <code>int</code> is <code>[-32,768 to 32,767]</code> when it takes up 2 bytes and <code>[-2,147,483,648 to 2,147,483,647]</code> when it takes up 4 bytes of memory.</p>
<p>The range of numbers for an unsigned <code>int</code> doesn't include any of the negative numbers in the range mentioned for signed <code>int</code>s.</p>
<p>So, the range of numbers for unsigned <code>ints</code> that take up 2 bytes of memory is <code>[0 to 65,535]</code> and the range is <code>[0 to 4,294,967,295]</code> for those that take up 4 bytes.</p>
<p>To represent smaller numbers, you can use another data type – the <code>short int</code>. It typically takes up 2 bytes (or 16 bits) of memory.</p>
<p>A signed <code>short int</code> allows for numbers in a range from <code>[-32,768 to 32,767]</code>.</p>
<p>An unsigned <code>short int</code> allows for numbers in a range from <code>[0 to 65,535]</code>.</p>
<p>Use a <code>short</code> when you want to work with smaller integers, or when memory optimisation is critically important.</p>
<p>If you need to work with larger integers, you can also use other data types like <code>long int</code> or <code>long long int</code>, which provide a larger range and higher precision.</p>
<p>A <code>long int</code> typically takes up at least 4 bytes of memory (or 32 bits).</p>
<p>The values for a signed <code>long int</code> range from <code>[-2,147,483,648 to 2,147,483,647]</code>.</p>
<p>And the values for an unsigned <code>long int</code> range from <code>[0 to 4,294,967,295]</code>.</p>
<p>The <code>long long int</code> data type is able to use even larger numbers than a <code>long int</code>. It usually takes up 8 bytes (or 64 bits) of memory.</p>
<p>A signed <code>long long int</code> allows for a range from <code>[-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807]</code></p>
<p>And an unsigned <code>long long int</code> has a range of numbers from <code>[0 to 18,446,744,073,709,551,615]</code>.</p>
<h4 id="heading-what-is-the-float-data-type-in-c">What Is The <code>float</code> Data Type in C?</h4>
<p>The <code>float</code> data type is used to hold numbers with a decimal value (which are also known as real numbers).</p>
<p>It holds 4 bytes (or 32 bits) of memory and it is a single-precision floating-point type.</p>
<p>Here is how you create a variable that will hold a <code>float</code> value:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

   <span class="hljs-keyword">float</span> temperature = <span class="hljs-number">68.5</span>;
 }
</code></pre>
<p>A <code>double</code> is a floating point value and is the most commonly used floating-point data type in C.</p>
<p>It holds 8 bytes (or 64 bits) of memory, and it is a double-precision floating-point type.</p>
<p>Here is how you create a variable that will hold a <code>double</code> value:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

    <span class="hljs-keyword">double</span> number = <span class="hljs-number">3.14159</span>;
 }
</code></pre>
<p>When choosing which floating-point data type to use, consider the trade-off between memory usage and precision.</p>
<p>A <code>float</code> has less precision that a <code>double</code> but consumes less memory.</p>
<p>Use a <code>float</code> when memory usage is a concern (such as when working with a system with limited resources) or when you need to perform calculations where high precision is not critical.</p>
<p>If you require higher precision and accuracy for your calculations and memory usage is not critical, you can use a <code>double</code>.</p>
<h3 id="heading-what-are-format-codes-in-c">What Are Format Codes in C?</h3>
<p>Format codes are used in input and output functions, such as <code>scanf()</code> and <code>printf()</code>, respectively.</p>
<p>They act as placeholders and substitutes for variables.</p>
<p>Specifically, they specify the expected format of input and output.</p>
<p>They tell the program how to format or interpret the data being passed to or read from the <code>scanf()</code> and <code>printf()</code> functions.</p>
<p>The syntax for format codes is the <code>%</code> character and the format specifier for the data type of the variable.</p>
<p>Let's take the following example:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span>
</span>{
    <span class="hljs-keyword">int</span> age = <span class="hljs-number">29</span>;

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"My age is %i\n"</span>, age);  <span class="hljs-comment">// My age is 29</span>
}
</code></pre>
<p>In the example above, <code>age</code> is the variable in the program. It is of type <code>int</code>.</p>
<p>The format code – or placeholder – for integer values is <code>%i</code>. This indicates that an integer should be printed.</p>
<p>In the program's output, <code>%i</code> is replaced with the value of <code>age</code>, which is <code>29</code>.</p>
<p>Here is a table with the format specifiers for each data type:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>FORMAT SPECIFIER</td><td>DATA TYPE</td></tr>
</thead>
<tbody>
<tr>
<td>%c</td><td>char</td></tr>
<tr>
<td>%c</td><td>unsigned char</td></tr>
<tr>
<td>%i, &amp;d</td><td>int</td></tr>
<tr>
<td>%u</td><td>unsigned int</td></tr>
<tr>
<td>%hi, %hd</td><td>short int</td></tr>
<tr>
<td>%hu</td><td>unsigned short int</td></tr>
<tr>
<td>%li or %ld</td><td>long int</td></tr>
<tr>
<td>%lu</td><td>unsigned long int</td></tr>
<tr>
<td>%lli or %lld</td><td>long long int</td></tr>
<tr>
<td>%llu</td><td>unsigned long long int</td></tr>
<tr>
<td>%f</td><td>float</td></tr>
<tr>
<td>%lf</td><td>double</td></tr>
<tr>
<td>%Lf</td><td>long double</td></tr>
</tbody>
</table>
</div><h3 id="heading-how-to-recieve-user-input-using-the-scanf-function">How to Recieve User Input Using the <code>scanf()</code> Function</h3>
<p>Earlier you saw how to print something to the console using the <code>printf()</code> function.</p>
<p>But what happens when you want to receive user input? This is where the <code>scanf()</code> function comes in.</p>
<p>The <code>scanf()</code> function reads user input, which is typically entered via a keyboard.</p>
<p>The user enters a value, presses the Enter key, and the value is saved in a variable.</p>
<p>The general syntax for using <code>scanf()</code> looks something similar to the following:</p>
<pre><code class="lang-c"><span class="hljs-built_in">scanf</span>(<span class="hljs-string">"format_string"</span>, &amp;variable);
</code></pre>
<p>Let's break it down:</p>
<ul>
<li><p><code>format_string</code> is the string that lets the computer know what to expect. It specifies the expected format of the input. For example, is it a word, a number, or something else?</p>
</li>
<li><p><code>&amp;variable</code> is the pointer to the variable where you want to store the value gathered from the user input.</p>
</li>
</ul>
<p>Let's take a look at an example of <code>scanf()</code> in action:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

  <span class="hljs-keyword">int</span> number;

  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Please enter your age: "</span>);

  <span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%i"</span>, &amp;number);

  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Your age is %i\n"</span>, number);
}
</code></pre>
<p>In the example above, I first have to include the <code>stdio.h</code> header file, which provides input and output functions in C.</p>
<p>Then, in the <code>main()</code> function, I declare a variable named <code>number</code> that will hold integer values. This variable will store the user input.</p>
<p>Then, I prompt the user to enter a number using the <code>printf()</code> function.</p>
<p>Next, I use <code>scanf()</code> to read and save the value that the user enters.</p>
<p>The format specifier <code>%i</code> lets the computer known that it should expect an integer input.</p>
<p>Note also the <code>&amp;</code> symbol before the variable name. Forgetting to add it will cause an error.</p>
<p>Lastly, after receiving the input, I display the received value to the console using another <code>printf()</code> function.</p>
<h3 id="heading-what-are-constants-in-c">What are Constants in C?</h3>
<p>As you saw earlier on, variable values can be changed throughout the life of a program.</p>
<p>With that said, there may be times when you don’t want a value to be changed. This is where constants come in handy.</p>
<p>In C, a constant is a variable with a value that cannot be changed after declaration and during the program's execution.</p>
<p>You can create a constant in a similar way to how you create variables.</p>
<p>The differences between constants and variables is that with constants you have to use the <code>const</code> keyword before mentioning the data type.</p>
<p>And when working with constants, you should always specify a value.</p>
<p>The general syntax for declaring constants in C looks like this:</p>
<pre><code class="lang-plaintext">const data_type constant_name = value;
</code></pre>
<p>Here, <code>data_type</code> represents the data type of the constant, <code>constant_name</code> is the name you choose for the constant, and <code>value</code> is the value of the constant.</p>
<p>It is also best practice to use all upper case letters when declaring a constant’s name.</p>
<p>Let’s see an example of how to create a constant in C:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

    <span class="hljs-keyword">const</span> <span class="hljs-keyword">int</span> LUCKY_NUM = <span class="hljs-number">7</span>;

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"My lucky number is: %i\n"</span>, LUCKY_NUM);
}
</code></pre>
<p>In this example, <code>LUCKY_NUM</code> is defined as a constant with a value of <code>7</code>.</p>
<p>The constant's name, <code>LUCKY_NUM</code>, is in uppercase letters, as this is a best practice and convention that improves the readability of your code and distinguishes constants from variables.</p>
<p>Once defined, it cannot be modified in the program.</p>
<p>If you try to change its value, the C compiler will generate an error indicating that you are attempting to modify a constant.</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

    <span class="hljs-keyword">const</span> <span class="hljs-keyword">int</span> LUCKY_NUM = <span class="hljs-number">7</span>;

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"My lucky number is: %i\n"</span>, LUCKY_NUM);

    LUCKY_NUM = <span class="hljs-number">13</span>; <span class="hljs-comment">// this will cause an error</span>

}
</code></pre>
<h2 id="heading-chapter-3-operators">Chapter 3: Operators</h2>
<p>Operators are essential building blocks in all programming languages.</p>
<p>They let you perform various operations on variables and values using symbols.</p>
<p>And they let you compare variables and values against each other for decision-making computatons.</p>
<p>In this chapter, you will learn about the most common operators in C programming.</p>
<p>You will first learn about arithmetic operators, which allow you to perform basic mathematical calculations.</p>
<p>You will then learn about relational (also known as comparisson operators), which help you compare values.</p>
<p>And you will learn about logical operators, which allow you to make decisions based on conditions.</p>
<p>After understanding these fundamental operators, you'll learn about some additional operators, such as assignment operators, and increment and decrement operators.</p>
<p>By the end of this chapter, you will have a solid grasp of how to use different operators to manipulate data.</p>
<h3 id="heading-what-are-the-arithmetic-operators-in-c">What Are the Arithmetic Operators in C?</h3>
<p>Arithmetic operators are used to perform basic arithmetic operations on numeric data types.</p>
<p>Operations include addition, subtraction, multiplication, division, and calculating the remainder after division.</p>
<p>These are the main arithmetic operators in C:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Operator</td><td>Operation</td></tr>
</thead>
<tbody>
<tr>
<td>+</td><td>Addition</td></tr>
<tr>
<td>-</td><td>Subtraction</td></tr>
<tr>
<td>*</td><td>Multiplication</td></tr>
<tr>
<td>/</td><td>Division</td></tr>
<tr>
<td>%</td><td>Remainder after division (modulo)</td></tr>
</tbody>
</table>
</div><p>Let's see examples of each one in action.</p>
<h4 id="heading-how-to-use-the-addition-operator">How to Use the Addition (<code>+</code>) Operator</h4>
<p>The addition operator adds two operands together and returns their sum.</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

    <span class="hljs-keyword">int</span> a = <span class="hljs-number">5</span>;

    <span class="hljs-keyword">int</span> b = <span class="hljs-number">3</span>;

    <span class="hljs-keyword">int</span> sum = a + b;

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Sum: %i\n"</span>, sum); <span class="hljs-comment">// Output: Sum: 8</span>
}
</code></pre>
<h4 id="heading-how-to-use-the-subtraction-operator">How to Use the Subtraction (<code>-</code>) Operator</h4>
<p>The subtraction operator subtracts the second operand from the first operand and returns their difference.</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

    <span class="hljs-keyword">int</span> a = <span class="hljs-number">10</span>; 

    <span class="hljs-keyword">int</span> b = <span class="hljs-number">5</span>;

    <span class="hljs-keyword">int</span> difference = a - b;

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Difference: %i\n"</span>, difference); <span class="hljs-comment">// Output: Difference: 5</span>
}
</code></pre>
<h4 id="heading-how-to-use-the-multiplication-operator">How to Use the Multiplication (<code>*</code>) Operator</h4>
<p>The multiplication operator multiplies two operands and returns their product.</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

    <span class="hljs-keyword">int</span> a = <span class="hljs-number">4</span>;

    <span class="hljs-keyword">int</span> b = <span class="hljs-number">3</span>;

    <span class="hljs-keyword">int</span> product = a * b;

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Product: %i\n"</span>, product); <span class="hljs-comment">// Output: Product: 12</span>
}
</code></pre>
<h4 id="heading-how-to-use-the-division-operator">How to Use the Division (<code>/</code>) Operator</h4>
<p>The division operator divides the first operand by the second operand and returns their quotient.</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

    <span class="hljs-keyword">int</span> a = <span class="hljs-number">10</span>;

    <span class="hljs-keyword">int</span> b = <span class="hljs-number">2</span>;

    <span class="hljs-keyword">int</span> quotient = a / b;

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Quotient: %i\n"</span>, quotient); <span class="hljs-comment">// Output: Quotient: 5</span>
}
</code></pre>
<h4 id="heading-how-to-use-the-modulo-operator">How to Use the Modulo (<code>%</code>) Operator</h4>
<p>The modulo operator returns the remainder of the first operand when divided by the second operand.</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

    <span class="hljs-keyword">int</span> a = <span class="hljs-number">10</span>;

    <span class="hljs-keyword">int</span> b = <span class="hljs-number">3</span>;

    <span class="hljs-keyword">int</span> remainder = a % b;

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Remainder: %i\n"</span>, remainder); <span class="hljs-comment">// Output: Remainder: 1</span>
}
</code></pre>
<p>The modulo operator is commonly used to determine whether an integer is even or odd.</p>
<p>If the remainder of the operation is <code>1</code>, then the integer is odd. If there is no remainder, then the integer is even.</p>
<h3 id="heading-what-are-the-relational-operators-in-c">What Are The Relational Operators in C?</h3>
<p>Relational operators are used to compare values and return a result.</p>
<p>The result is a Boolean value. A Boolean value is either <code>true</code> (represented by <code>1</code>) or <code>false</code> (represented by <code>0</code>).</p>
<p>These operators are commonly used in decision-making statements such as <code>if</code> statements, and <code>while</code> loops.</p>
<p>These are the relational operators in C:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Operator</td><td>Name of Operator</td></tr>
</thead>
<tbody>
<tr>
<td>\==</td><td>Equal to</td></tr>
<tr>
<td>!=</td><td>Not equal to</td></tr>
<tr>
<td>\&gt;</td><td>Greater than</td></tr>
<tr>
<td>&lt;</td><td>Less than</td></tr>
<tr>
<td>\&gt;=</td><td>Greater than or equal to</td></tr>
<tr>
<td>&lt;=</td><td>Less than or equal to</td></tr>
</tbody>
</table>
</div><p>Let’s see an example of each one in action.</p>
<h4 id="heading-how-to-use-the-equal-to-operator">How to Use the Equal to (<code>==</code>) Operator</h4>
<p>The equal to operator checks if two values are equal.</p>
<p>It essentially asks the question, "Are these two values equal?"</p>
<p>Note that you use the comparisson operator (two equal signs – <code>==</code>) and not the assignment operator (<code>=</code>) which is used for assigning a value to a variable.</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

    <span class="hljs-keyword">int</span> a = <span class="hljs-number">5</span>;

    <span class="hljs-keyword">int</span> b = <span class="hljs-number">5</span>;

    <span class="hljs-keyword">int</span> result = (a == b);

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Result: %i\n"</span>, result); <span class="hljs-comment">// Output: Result: 1</span>
}
</code></pre>
<p>The result is <code>1</code> (true), because <code>a</code> and <code>b</code> are equal.</p>
<h4 id="heading-how-to-use-the-not-equal-to-operator">How to Use the Not equal to (<code>!=</code>) Operator</h4>
<p>The not equal to operator checks if two values are NOT equal.</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

    <span class="hljs-keyword">int</span> a = <span class="hljs-number">5</span>; 

    <span class="hljs-keyword">int</span> b = <span class="hljs-number">3</span>;

    <span class="hljs-keyword">int</span> result = (a != b);

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Result: %i\n"</span>, result); <span class="hljs-comment">// Output: Result: 1</span>
}
</code></pre>
<p>The result is <code>1</code> (true), because <code>a</code> and <code>b</code> are not equal.</p>
<h4 id="heading-how-to-use-the-greater-than-gt-operator">How to Use the Greater than (<code>&gt;</code>) Operator</h4>
<p>This operator compares two values to check if one is greater than the other.</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

    <span class="hljs-keyword">int</span> a = <span class="hljs-number">10</span>;

    <span class="hljs-keyword">int</span>  b = <span class="hljs-number">5</span>;

    <span class="hljs-keyword">int</span> result = (a &gt; b);

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Result: %i\n"</span>, result); <span class="hljs-comment">// Output: Result: 1</span>
}
</code></pre>
<p>The result is <code>1</code> (true), because <code>a</code> is greater than <code>b</code>.</p>
<h4 id="heading-how-to-use-the-less-than-lt-operator">How to Use the Less than (<code>&lt;</code>) Operator</h4>
<p>This operator compares two values to check if one is less than the other.</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

    <span class="hljs-keyword">int</span> a = <span class="hljs-number">10</span>;

    <span class="hljs-keyword">int</span> b = <span class="hljs-number">5</span>;

    <span class="hljs-keyword">int</span> result = (a &lt; b);

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Result: %i\n"</span>, result); <span class="hljs-comment">// Output: Result: 0</span>
}
</code></pre>
<p>The result is <code>0</code> (false), because <code>a</code> is not less than <code>b</code>.</p>
<h4 id="heading-how-to-use-the-greater-than-or-equal-to-gt-operator">How to Use the Greater than or Equal to (<code>&gt;=</code>) Operator</h4>
<p>This operator compares two values to check if one is greater than or equal to the other.</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

    <span class="hljs-keyword">int</span> a = <span class="hljs-number">5</span>;

    <span class="hljs-keyword">int</span>  b = <span class="hljs-number">5</span>;

    <span class="hljs-keyword">int</span> result = (a &gt;= b);

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Result: %i\n"</span>, result); <span class="hljs-comment">// Output: Result: 1</span>
}
</code></pre>
<p>The result is <code>1</code> (true), because <code>a</code> is equal to <code>b</code>.</p>
<h4 id="heading-how-to-use-the-less-than-or-equal-to-lt-operator">How to Use the Less than or equal to (<code>&lt;=</code>) Operator</h4>
<p>This operator compares two values to check if one is less than or equal the other.</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

    <span class="hljs-keyword">int</span> a = <span class="hljs-number">1</span>;

    <span class="hljs-keyword">int</span> b = <span class="hljs-number">5</span>;

    <span class="hljs-keyword">int</span> result = (a &lt;= b);

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Result: %i\n"</span>, result); <span class="hljs-comment">// Output: Result: 1</span>
}
</code></pre>
<p>The result is <code>1</code> (true), because <code>a</code> is less than <code>b</code>.</p>
<h3 id="heading-logical-operators">Logical Operators</h3>
<p>Logical operators operate on Boolean values and return a Boolean value.</p>
<p>Here are the logical operators used in C:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Operator</td><td>Name of Operator</td></tr>
</thead>
<tbody>
<tr>
<td><code>&amp;&amp;</code></td><td>Logical AND</td></tr>
<tr>
<td>`</td><td></td></tr>
<tr>
<td><code>!</code></td><td>Logical NOT</td></tr>
</tbody>
</table>
</div><p>Let's go into more detail on each one in the following sections.</p>
<h4 id="heading-how-to-use-the-and-ampamp-operator">How to Use the AND (<code>&amp;&amp;</code>) Operator</h4>
<p>The logical AND (<code>&amp;&amp;</code>) operator checks whether all operands are <code>true</code>.</p>
<p>The result is <code>true</code> only when all operands are <code>true</code>.</p>
<p>Here is the truth table for the AND (<code>&amp;&amp;</code>) operator when you are working with two operands:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>FIRST OPERAND</td><td>SECOND OPERAND</td><td>RESULT</td></tr>
</thead>
<tbody>
<tr>
<td>true</td><td>true</td><td>true</td></tr>
<tr>
<td>true</td><td>false</td><td>false</td></tr>
<tr>
<td>false</td><td>true</td><td>false</td></tr>
<tr>
<td>false</td><td>false</td><td>false</td></tr>
</tbody>
</table>
</div><p>Let's take the following example:</p>
<p>The result of <code>(10 == 10) &amp;&amp; (20 == 20)</code> is <code>true</code> because both operands are <code>true</code>.</p>
<p>Let's look at another example:</p>
<p>The result of <code>(10 == 20) &amp;&amp; (20 == 20)</code> is <code>false</code> because one of the operands is <code>false</code>.</p>
<p>When the first operand is <code>false</code>, the second operand is not evaluated (since there's no point - it's already determined that the first operand is false, so the result can only be <code>false</code>).</p>
<h4 id="heading-how-to-use-the-or-operator">How to Use the OR (<code>||</code>) Operator</h4>
<p>The logical OR (<code>||</code>) operator checks if at least one of the operands is <code>true</code>.</p>
<p>The result is <code>true</code> only when at least one of the operands is <code>true</code>.</p>
<p>Here is the truth table for the OR (<code>||</code>) operator when you are working with two operands:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>FIRST OPERAND</td><td>SECOND OPERAND</td><td>RESULT</td></tr>
</thead>
<tbody>
<tr>
<td>true</td><td>true</td><td>true</td></tr>
<tr>
<td>true</td><td>false</td><td>true</td></tr>
<tr>
<td>false</td><td>true</td><td>true</td></tr>
<tr>
<td>false</td><td>false</td><td>false</td></tr>
</tbody>
</table>
</div><p>Let's look at an example:</p>
<p>The result of <code>(10 == 20) || (20 == 20)</code> is <code>true</code> because one of the operands is <code>true</code>.</p>
<p>Let's look at another example:</p>
<p>The result of <code>(20 == 20) || (10 == 20)</code> is <code>true</code> because one of the operands is <code>true</code></p>
<p>If the first operand is <code>true</code>, then the second operator is not evaluated.</p>
<h3 id="heading-how-to-use-the-not-operator">How to Use the NOT (<code>!</code>) Operator</h3>
<p>The logical NOT (<code>!</code>) operator negates the operand.</p>
<p>If the operand is <code>true</code>, it returns <code>false</code>.</p>
<p>And if it is <code>false</code>, it returns <code>true</code>.</p>
<p>You may want to use the NOT operator when when you want to flip the value of a condition and return the opposite of what the condition evaluates to.</p>
<p>Here is the truth table for the NOT(<code>!</code>) operator:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>OPERAND</td><td>RESULT</td></tr>
</thead>
<tbody>
<tr>
<td>true</td><td>false</td></tr>
<tr>
<td>false</td><td>true</td></tr>
</tbody>
</table>
</div><p>Let's look at an example:</p>
<p>The result of <code>!(10 == 10)</code> is <code>false</code>.</p>
<p>The condition <code>10 == 10</code> is <code>true</code>, but the <code>!</code> operator negates it so the result is <code>false</code>.</p>
<p>And let's look at another example:</p>
<p>The result of <code>!(10 == 20)</code> is <code>true</code>.</p>
<p>The condition <code>10 == 20</code> is false, but the <code>!</code> operator negates it.</p>
<h3 id="heading-what-is-the-assignement-operator-in-c">What Is the Assignement Operator in C?</h3>
<p>The assignment operator is used to assign a value to a variable.</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

    <span class="hljs-comment">// declare an integer variable named num</span>
    <span class="hljs-keyword">int</span> num;

    <span class="hljs-comment">// assign the value 10 to num</span>
    num = <span class="hljs-number">10</span>;

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"num: %i\n"</span>, num); <span class="hljs-comment">// Output: num: 10</span>

}
</code></pre>
<p>In the example above, the value <code>10</code> is assigned to the variable <code>num</code> using the assignment operator.</p>
<p>The assignment operator works by evaluating the expression on the right-hand side and then storing its result in the variable on the left-hand side.</p>
<p>The type of data assigned should match the data type of the variable.</p>
<h4 id="heading-how-to-use-compound-assignment-operators">How to Use Compound Assignment Operators</h4>
<p>Compound assignment operators are shorthand notations.</p>
<p>They allow you to modify a variable by performing an operation on it and then storing the result of the operation back into the same variable in a single step.</p>
<p>This can make your code more concise and easier to read.</p>
<p>Some common compound assignment operators in C include:</p>
<ul>
<li><p><code>+=</code>: Addition and assignment</p>
</li>
<li><p><code>=</code>: Subtraction and assignment</p>
</li>
<li><p><code>=</code>: Multiplication and assignment</p>
</li>
<li><p><code>/=</code>: Division and assignment</p>
</li>
<li><p><code>%=</code>: Modulo and assignment</p>
</li>
</ul>
<p>Let’s see an example of how the <code>+=</code> operator works:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

  <span class="hljs-keyword">int</span> num = <span class="hljs-number">10</span>;

  num += <span class="hljs-number">5</span>; 

  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Num: %i\n"</span>, num); <span class="hljs-comment">// Num: 15</span>
}
</code></pre>
<p>In the example above, I created a variable named <code>num</code> and assigned it an initial value of <code>10</code>.</p>
<p>I then wanted to increment the variable by <code>5</code>. To do this, I used the <code>+=</code> compound operator.</p>
<p>The line <code>num += 5</code> increments the value of <code>num</code> by 5, and the result (15) is stored back into <code>num</code> in one step.</p>
<p>Note that the <code>num += 5;</code> line works exactly the same as doing <code>num = num + 5</code>, which would mean <code>num = 10 + 5</code>, but with fewer lines of code.</p>
<h3 id="heading-what-are-the-increment-and-decrement-operators-in-c">What Are the Increment and Decrement Operators in C?</h3>
<p>The increment <code>++</code> and decrement <code>--</code> operators increment and decrement a variable by one, respectively.</p>
<p>Let’s look at an example of how to use the <code>++</code> operator:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

  <span class="hljs-keyword">int</span> num = <span class="hljs-number">10</span>;
  num++;

  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Num: %i\n"</span>, num); <span class="hljs-comment">// Num: 11</span>

}
</code></pre>
<p>The initial value of the variable <code>num</code> is <code>10</code>.</p>
<p>By using the <code>++</code> increment operator, the value of <code>num</code> is set to <code>11</code>.</p>
<p>This is like perfoming <code>num = num + 1</code> but with less code.</p>
<p>The shorthand for decrementing a variable by one is <code>--</code>.</p>
<p>If you wanted to decrement <code>num</code> by one, you would do the following:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

  <span class="hljs-keyword">int</span> num = <span class="hljs-number">10</span>;
  num--;

  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Num: %i\n"</span>, num); <span class="hljs-comment">// Num: 9</span>

}
</code></pre>
<p>The initial value of the variable <code>num</code> is <code>10</code>.</p>
<p>By using the <code>--</code> increment operator, the value of <code>num</code> is now set to <code>9</code>.<br>This is like perfoming <code>num = num - 1</code>.</p>
<h2 id="heading-chapter-4-conditional-statements">Chapter 4: Conditional Statements</h2>
<p>The examples you have seen so far all execute line by line, from top to bottom.</p>
<p>They are not flexible and dynamic and do not adapt according to user behavior or specific situations.</p>
<p>In this chapter, you will learn how to make decisions and control the flow of a program.</p>
<p>You get to set the rules on what happens next in your programs by setting conditions using conditional statements.</p>
<p>Conditional statements take a specific action based on the result of a comparisson that takes place.</p>
<p>The program will decide what the next steps should be based on whether the conditions are met or not.</p>
<p>Certain parts of the program may not run depending on the results or depending on certain user input. The user can go down different paths depending on the various forks in the road that come up during a program's life.</p>
<p>First, you will learn about the <code>if</code> statement – the foundational building block of decision-making in C.</p>
<p>You will also learn about the <code>else if</code> and <code>else</code> statements that are added to the <code>if</code> statement to provide additional flexibility to the program.</p>
<p>You will then learn about the ternary operator which allows you to condense decision-making logic into a single line of code and improve the readability of your program.</p>
<h3 id="heading-how-to-create-an-if-statement-in-c">How to Create an <code>if</code> statement in C</h3>
<p>The most basic conditional statement in C is the <code>if</code> statement.</p>
<p>It makes a decision based on a condition.</p>
<p>If the given condition evaluates to <code>true</code> only then is the code inside the <code>if</code> block executed.</p>
<p>If the given condition evaluates to <code>false</code>, the code inside the <code>if</code> block is ignored and skipped.</p>
<p>The general syntax for an <code>if</code> statement in C is the following:</p>
<pre><code class="lang-c"><span class="hljs-keyword">if</span> (condition) {
  <span class="hljs-comment">// run this code if condition is true</span>
}
</code></pre>
<p>Let's look at an example:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

    <span class="hljs-comment">// variable age</span>
   <span class="hljs-keyword">int</span> age;

   <span class="hljs-comment">// prompt user to enter their age</span>
   <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Please enter your age: "</span>);

   <span class="hljs-comment">// store user's answer in the variable</span>
   <span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%i"</span>, &amp;age);

    <span class="hljs-comment">// check if age is less than 18</span>
    <span class="hljs-comment">// if it is, then and only then, print a message to the console</span>

   <span class="hljs-keyword">if</span> (age &lt; <span class="hljs-number">18</span>) {
       <span class="hljs-built_in">printf</span>(<span class="hljs-string">"You need to be over 18 years old to continue\\n"</span>);
   }
}
</code></pre>
<p>In the above code, I created a variable named <code>age</code> that holds an integer value.</p>
<p>I then prompted the user to enter their age and stored the answer in the variable <code>age</code>.</p>
<p>Then, I created a condition that checks whether the value contained in the variable <code>age</code> is less than 18.</p>
<p>If so, I want a message printed to the console letting the user know that to proceed, the user should be at least 18 years of age.</p>
<p>When asked for my age and I enter <code>16</code>, I'd get the following output:</p>
<pre><code class="lang-plaintext">#output

Please enter your age: 16
You need to be over 18 years old to continue
</code></pre>
<p>The condition (<code>age &lt; 18</code>) evaluates to <code>true</code> so the code in the <code>if</code> block executes.</p>
<p>Then, I re-compile and re-run the program.</p>
<p>This time, when asked for my age, say I enter <code>28</code>, but I don't get any output:</p>
<pre><code class="lang-plaintext">#output

Please enter your age: 28
</code></pre>
<p>This is because the condition evaluates to <code>false</code> and therefore the body of the <code>if</code> block is skipped.</p>
<p>I have also not specified what should happen in the case that the user's age is greater than 18.</p>
<p>To specify what happens in case the user's age is greater than 18, I can use an <code>if else</code> statement.</p>
<h3 id="heading-how-to-create-an-if-else-statement-in-c">How to Create an <code>if else</code> statement in C</h3>
<p>You can add an <code>else</code> clause to an <code>if</code> statement to provide code that will execute only when the <code>if</code> statement evaluates to <code>false</code>.</p>
<p>The <code>if else</code> statement essentially means that "<code>if</code> this condition is true do the following thing, <code>else</code> do this thing instead".</p>
<p>If the condition inside the parentheses evaluates to <code>true</code>, the code inside the <code>if</code> block will execute.</p>
<p>But if that condition evaluates to <code>false</code>, the code inside the <code>else</code> block will execute.</p>
<p>The <code>else</code> keyword is the solution for when the <code>if</code> condition is false and the code inside the <code>if</code> block doesn't run. It provides an alternative.</p>
<p>The general syntax looks like this:</p>
<pre><code class="lang-c"><span class="hljs-keyword">if</span> (condition) {
  <span class="hljs-comment">// run this code if condition is true</span>
} <span class="hljs-keyword">else</span> {
  <span class="hljs-comment">// if the condition above is false, run this code</span>
}
</code></pre>
<p>Now, let's revisit the example from the previous section, and specify what should happen if the user's age is greater than 18:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{
   <span class="hljs-keyword">int</span> age;

   <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Please enter your age: "</span>);

   <span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%i"</span>, &amp;age);


    <span class="hljs-comment">// if the condition in the parentheses is true the code inside the curly braces will execute</span>
    <span class="hljs-comment">// otherwise it is skipped</span>
    <span class="hljs-comment">// and the code in the else block will execute</span>

   <span class="hljs-keyword">if</span> (age &lt; <span class="hljs-number">18</span>) {
       <span class="hljs-built_in">printf</span>(<span class="hljs-string">"You need to be over 18 years old to continue\n"</span>);
   } <span class="hljs-keyword">else</span> {
      <span class="hljs-built_in">printf</span>(<span class="hljs-string">"You are over 18 so you can continue \n"</span>);
  }

   }
</code></pre>
<p>If the condition is <code>true</code> the code in the <code>if</code> block runs:</p>
<pre><code class="lang-plaintext">#output

Please enter your age: 14
You need to be over 18 years old to continue
</code></pre>
<p>If the condition is <code>false</code> the code in the <code>if</code> block is skipped and the code in the <code>else</code> block runs instead:</p>
<pre><code class="lang-plaintext">#output

Please enter your age: 45
You are over 18 so you can continue
</code></pre>
<h3 id="heading-how-to-create-an-else-if-statement-in-c">How to Create an <code>else if</code> statement in C</h3>
<p>But what happens when you want to have more than one condition to choose from?</p>
<p>If you wish to chose between more than one option you can introduce an <code>else if</code> statement.</p>
<p>An <code>else if</code> statement essentially means that "If this condition is true, do the following. If it isn't, do this instead. However, if none of the above are true and all else fails, finally do this."</p>
<p>The general syntax looks something like the following:</p>
<pre><code class="lang-plaintext">if (condition) {
   // if condition is true run this code
} else if(another_condition) {
   // if the above condition was false and this condition is true,
   // run the code in this block
} else {
   // if the two above conditions are false run this code
}
</code></pre>
<p>Let's see how an <code>else if</code> statement works.</p>
<p>Say you have the following example:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{
   <span class="hljs-keyword">int</span> age;

   <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Please enter your age: "</span>);

   <span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%i"</span>, &amp;age);

   <span class="hljs-keyword">if</span> (age &lt; <span class="hljs-number">18</span>) {
       <span class="hljs-built_in">printf</span>(<span class="hljs-string">"You need to be over 18 years old to continue\n"</span>);
   }  <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (age &lt; <span class="hljs-number">21</span>) {
       <span class="hljs-built_in">printf</span>(<span class="hljs-string">"You need to be over 21\n"</span>);
   } <span class="hljs-keyword">else</span> {
      <span class="hljs-built_in">printf</span>(<span class="hljs-string">"You are over 18 and older than 21 so you can continue \n"</span>);
  }

   }
</code></pre>
<p>If the first <code>if</code> statement is true, the rest of the block will not run:</p>
<pre><code class="lang-plaintext">#output

Please enter your age: 17
You need to be over 18 years old to continue
</code></pre>
<p>If the first <code>if</code> statement is false, then the program moves on to the next condition.</p>
<p>If that is true the code inside the <code>else if</code> block executes and the rest of the block doesn't run:</p>
<pre><code class="lang-plaintext">#output

Please enter your age: 20
You are need to be over 21
</code></pre>
<p>If both of the previous conditions are all false, then the last resort is the <code>else</code> block which is the one to execute:</p>
<pre><code class="lang-plaintext">#output

Please enter your age: 22
You are over 18 and older than 21 so you can continue
</code></pre>
<h3 id="heading-how-to-use-the-ternary-operator-in-c">How to Use the Ternary Operator in C</h3>
<p>The ternary operator (also known as the conditional operator) allows you to write an <code>if else</code> statement with fewer lines of code.</p>
<p>It can provide a way of writing more readable and concise code and comes in handy when writing simple conditional expressions.</p>
<p>You would want to use it when you are making making simple decisions and want to keep your code concise and on one line.</p>
<p>However, it's best to stick to a regular <code>if-else</code> statement when you are dealing with more complex decisions as the ternary operator could make your code hard to read.</p>
<p>The general syntax for the ternary operator looks something similar to the following:</p>
<pre><code class="lang-plaintext">condition ? expression_if_true : expression_if_false;
</code></pre>
<p>Let's break it down:</p>
<ul>
<li><p><code>condition</code> is the condition you want to evaluate. This condition will evaluate to either <code>true</code> of <code>false</code></p>
</li>
<li><p><code>?</code> separates the condition from the two possible expressions</p>
</li>
<li><p><code>expression_if_true</code> is executed if the <code>condition</code> evaluates to <code>true</code></p>
</li>
<li><p><code>:</code> separates the <code>expression_if_true</code> from the <code>expression_if_false</code></p>
</li>
<li><p><code>expression_if_false</code> is executed if the <code>condition</code> evaluates to <code>false</code>.</p>
</li>
</ul>
<p>Let's take a look at an example:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

    <span class="hljs-keyword">int</span> x = <span class="hljs-number">10</span>;

    <span class="hljs-keyword">int</span> y = (x &gt; <span class="hljs-number">5</span>) ? <span class="hljs-number">100</span> : <span class="hljs-number">200</span>;

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"x: %i\n"</span>, x); <span class="hljs-comment">// x: 10</span>

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"y: %i\n"</span>, y);  <span class="hljs-comment">// y: 100</span>
   }
</code></pre>
<p>In the example above, the condition is <code>(x &gt; 5)</code>.</p>
<p>If <code>x</code> is greater than 5, the condition evaluates to <code>true</code>. And when the condition is <code>true</code>, the value assigned to <code>y</code> will be <code>100</code>.</p>
<p>If the condition evaluates to <code>false</code>, the value assigned to <code>y</code> will be <code>200</code>.</p>
<p>So, since <code>x</code> is greater than 5 (<code>x = 10</code>), <code>y</code> is assigned the value <code>100</code>.</p>
<h2 id="heading-chapter-5-loops">Chapter 5: Loops</h2>
<p>In this chapter you will learn about loops, which are essential for automating repetitive tasks without having to write the same code multiple times.</p>
<p>Loops allow you to execute a specific block of code instructions repeatedly over and over again until a certain condition is met.</p>
<p>You will learn about the different types of loops, such as the <code>for</code> , <code>while</code> and <code>do-while</code> loops, and understand their syntax and when you should use each one.</p>
<p>You will also learn about the <code>break</code> statement, which allows you to control the execution flow within loops in specific ways.</p>
<h3 id="heading-how-to-create-a-for-loop-in-c">How to Create a <code>for</code> Loop in C</h3>
<p>A <code>for</code> loop allows you to execute a block of code repeatedly based on a specified condition.</p>
<p>It's useful when you know how many times you want to repeat a certain action.</p>
<p>The general syntax for a <code>for</code> loop looks like this:</p>
<pre><code class="lang-plaintext">for (initialization; condition; increment/decrement) {
    // Code to be executed in each iteration
}
</code></pre>
<p>Let's break it down:</p>
<ul>
<li><p><code>initialization</code> is the step where you initialize a loop control variable. It's typically used to set the starting point for your loop.</p>
</li>
<li><p><code>condition</code> is the condition that is evaluated before each iteration. If the condition is <code>true</code>, the loop continues. If it's <code>false</code>, the loop terminates. The loop will run as long as the condition remains true.</p>
</li>
<li><p><code>increment/decrement</code> is the part responsible for changing the loop control variable after each iteration. It can be an increment (<code>++</code>), a decrement (<code>--</code>), or any other modification.</p>
</li>
<li><p><code>Code to be executed in each iteration</code> is the block of code inside the <code>for</code> loop's body that gets executed in each iteration if the condition is <code>true</code>.</p>
</li>
</ul>
<p>Let's see an example of how a <code>for</code> loop works.</p>
<p>Say you want to print the numbers from 1 to 5 to the console:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt;= <span class="hljs-number">5</span>; i++) {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Iteration %i\n"</span>, i);
    }

}
</code></pre>
<p>Output:</p>
<pre><code class="lang-plaintext">Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
</code></pre>
<p>In the example above, I first initialize the loop control variable <code>i</code> with a value of <code>1</code>.</p>
<p>The condition <code>i &lt;= 5</code> is true, so the loop's body is executed and <code>"Iteration 1"</code> is printed.</p>
<p>After each iteration, the value of <code>i</code> is incremented by <code>1</code>. So, <code>i</code> is incremented to <code>2</code>.</p>
<p>The condition is still <code>true</code>, so <code>"Iteration 2"</code> is printed.</p>
<p>The loop will continue as long as <code>i</code> is less than or equal to <code>5</code>.</p>
<p>When <code>i</code> becomes <code>6</code>, the condition evaluates to <code>false</code> and the loop terminates.</p>
<h3 id="heading-how-to-create-a-while-loop-in-c">How to Create a <code>while</code> Loop in C</h3>
<p>As you saw in the previous section, a <code>for</code> loop is used when you know the exact number of iterations you want the loop to perform.</p>
<p>The <code>while</code> loop is useful when you want to repeat an action based on a condition but don't know the exact number of iterations beforehand.</p>
<p>Here is the general syntax of a <code>while</code> loop:</p>
<pre><code class="lang-plaintext">while (condition) {
    // Code to be executed in each iteration
}
</code></pre>
<p>With a <code>while</code> loop, the condition is evaluated before each iteration. If the condition is <code>true</code>, the loop continues. If it's false, the loop terminates.</p>
<p>The <code>while</code> loop will continue as long as the condition evaluates to <code>true</code>.</p>
<p>Something to note with <code>while</code> loops is that the code in the loop's body is not guaranteed to run even at least one time if a condition is not met.</p>
<p>Let's see an example of how a <code>while</code> loop works:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{

    <span class="hljs-keyword">int</span> count = <span class="hljs-number">1</span>;

    <span class="hljs-keyword">while</span> (count &lt;= <span class="hljs-number">5</span>) {

        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Iteration %i\n"</span>, count);

        count++;
    }

}
</code></pre>
<p>Output:</p>
<pre><code class="lang-plaintext">Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
</code></pre>
<p>In the example above, I first initialize a variable <code>count</code> with a value of <code>1</code>.</p>
<p>Before it runs any code, the <code>while</code> loop checks a condition.</p>
<p>The condition <code>count &lt;= 5</code> is <code>true</code> because count is initially <code>1</code>. So, the loop's body is executed and <code>"Iteration 1"</code> is printed.</p>
<p>Then, <code>count</code> is incremented to <code>2</code>.</p>
<p>The condition is still <code>true</code>, so <code>"Iteration 2"</code> is printed.</p>
<p>The loop will continue as long as count is less than or equal to 5.</p>
<p>This process continues until count becomes <code>6</code>, at which point the condition becomes <code>false</code>, and the loop terminates.</p>
<p>Something to be aware of when working with <code>while</code> loops is accidentally creating an infinite loop:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt; </span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span>
</span>{

    <span class="hljs-keyword">while</span>(<span class="hljs-literal">true</span>)
    {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Hello world"</span>);
    }
}
</code></pre>
<p>In this case the condition always evaluates to <code>true</code>.</p>
<p>After printing the line of code inside the curly braces, it continuously checks wether it should run the code again.</p>
<p>As the answer is always yes (since the condition it needs to check is always true each and every time), it runs the code again and again and again.</p>
<p>The way to stop the program and escape from the endless loop is running <code>Ctrl C</code> in the terminal.</p>
<h3 id="heading-how-to-create-a-do-while-loop-in-c">How to Create a <code>do-while</code> Loop in C</h3>
<p>As mentioned in the previous section, the code in the <code>while</code> loop's body is not guaranteed to run even at least one time if the condition is not met.</p>
<p>A <code>do-while</code> loop executes a block of code repeatedly for as long as a condition remains <code>true</code>.</p>
<p>However, in contrast to a <code>while</code> loop, it is guaranteed to run at least once, regardless of whether the condition is <code>true</code> or <code>false</code> from the beginning.</p>
<p>So, the <code>do-while</code> loop is useful when you want to ensure that the loop's body is executed at least once before the condition is checked.</p>
<p>The general syntax for a <code>do-while</code> loop looks like this:</p>
<pre><code class="lang-plaintext">do {
    // Code to be executed in each iteration
} while (condition);
</code></pre>
<p>Let's take a look at an example that demonstrates how a <code>do-while</code> loop works:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{

    <span class="hljs-keyword">int</span> count = <span class="hljs-number">1</span>;

    <span class="hljs-keyword">do</span> {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Iteration %i\n"</span>, count);

        count++;

    } <span class="hljs-keyword">while</span> (count &lt;= <span class="hljs-number">5</span>);

}
</code></pre>
<p>Output:</p>
<pre><code class="lang-plaintext">Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
</code></pre>
<p>In the example above I initialize a variable <code>count</code> with a value of <code>1</code>.</p>
<p>A <code>do-while</code> loop first does something and then checks a condition.</p>
<p>So, the block of code inside the loop is executed at least one time.</p>
<p>The string <code>"Iteration 1"</code> is printed and then <code>count</code> is incremented to <code>2</code>.</p>
<p>The condition <code>count &lt;= 5</code> is then checked and it evaluates to <code>true</code>, so the loop continues.</p>
<p>The loop will continue as long as <code>count</code> is less than or equal to 5.</p>
<p>After the iteration where <code>count</code> is <code>6</code>, the condition becomes <code>false</code>, and the loop terminates.</p>
<h3 id="heading-how-to-use-the-break-statement-in-c">How to Use the <code>break</code> Statement in C</h3>
<p>The <code>break</code> statement is used to immediately exit a loop and terminate its execution.</p>
<p>It's a control flow statement that allows you to interrupt the normal loop execution and move on to the code after the loop.</p>
<p>The <code>break</code> statement is especially useful when you want to exit a loop under specific conditions, even if the loop's termination condition hasn't been met.</p>
<p>You might use it when you encounter a certain value, or when a specific condition is met.</p>
<p>Here's how to use a <code>break</code> statement in a loop:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">int</span> target = <span class="hljs-number">5</span>;

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt;= <span class="hljs-number">10</span>; i++) {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Current value: %i\n"</span>, i);

        <span class="hljs-keyword">if</span> (i == target) {
            <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Target value reached. Exiting loop.\n"</span>);
            <span class="hljs-keyword">break</span>; <span class="hljs-comment">// Exit the loop</span>
        }
    }

}
</code></pre>
<p>Output:</p>
<pre><code class="lang-plaintext">Current value: 1
Current value: 2
Current value: 3
Current value: 4
Current value: 5
Target value reached. Exiting loop.
</code></pre>
<p>In the example above, a <code>for</code> loop is set to iterate from <code>1</code> to <code>10</code>.</p>
<p>Inside the loop, the current value of <code>i</code> is printed on each iteration.</p>
<p>There is also an <code>if</code> statement that checks if the current value of <code>i</code> matches the target value, which is set to <code>5</code>.</p>
<p>If <code>i</code> matches the target value, the <code>if</code> statement is triggered and a message is printed.</p>
<p>As a result, the <code>break</code> statement exits the current loop immediately and prematurely.</p>
<p>The program will continue executing the code that is after the loop.</p>
<h2 id="heading-chapter-6-arrays">Chapter 6: Arrays</h2>
<p>Arrays offer a versatile and organized way to store multiple pieces of related data that are arranged in an ordered sequence.</p>
<p>They allow you to store multiple values of the same data type under a single identifier and perform repetitive tasks on each element.</p>
<p>In this chapter, you will learn how to declare and initialize arrays. You will also learn how to access individual elements within an array using index notation and modify them.</p>
<p>In addition, you will learn how to use loops to iterate through array elements and perform operations on each element.</p>
<h3 id="heading-how-to-declare-and-initialize-an-array-in-c">How to Declare and Initialize an Array in C</h3>
<p>To declare an array in C, you first specify the data type of the elements the array will store.</p>
<p>This means you can create arrays of type <code>int</code>, <code>float</code>, <code>char</code>, and so on.</p>
<p>You then specify the array's name, followed by the array's size in square brackets.</p>
<p>The size of the array is the number of elements that it can hold. This number must be a positive integer.</p>
<p>Keep in mind that arrays have a fixed size, and once declared, you cannot change it later on.</p>
<p>Here is the general syntax for declaring an array:</p>
<pre><code class="lang-c">data_type array_name[array_size];
</code></pre>
<p>Here is how to declare an array of integers:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{

   <span class="hljs-keyword">int</span> grades[<span class="hljs-number">5</span>];
}
</code></pre>
<p>In the example above, I created an array named <code>grades</code> that can store <code>5</code> <code>int</code> numbers.</p>
<p>After declaring an array, you can initialize it with initial values.</p>
<p>To do this, use the assignment operator, <code>=</code>, followed by curly braces, <code>{}</code>.</p>
<p>The curly braces will enclose the values, and each value needs to be separated by a comma.</p>
<p>Here is how to initialize the <code>grades</code> array:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{

   <span class="hljs-keyword">int</span> grades[<span class="hljs-number">5</span>] = {<span class="hljs-number">50</span>, <span class="hljs-number">75</span>, <span class="hljs-number">100</span>, <span class="hljs-number">67</span>, <span class="hljs-number">90</span>};
}
</code></pre>
<p>Keep in mind that the number of values should match the array size, otherwise you will encounter errors.</p>
<p>Something to note here is that you can also partially initialize the array:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{

   <span class="hljs-keyword">int</span> grades[<span class="hljs-number">5</span>] = {<span class="hljs-number">50</span>, <span class="hljs-number">75</span>, <span class="hljs-number">100</span>};
}
</code></pre>
<p>In this case, the remaining two elements will be set to <code>0</code>.</p>
<p>Another way to initialize arrays is to omit the array's length inside the square brackets and only assign the initial values, like so:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{

   <span class="hljs-keyword">int</span> grades[] = {<span class="hljs-number">50</span>, <span class="hljs-number">75</span>, <span class="hljs-number">100</span>, <span class="hljs-number">67</span>, <span class="hljs-number">90</span>};
}
</code></pre>
<p>In this example, the array's size is <code>5</code> because I assigned it <code>5</code> values.</p>
<h4 id="heading-how-to-find-the-length-of-an-array-in-c-using-the-sizeof-operator">How to Find the Length of an Array in C Using the <code>sizeof()</code> Operator</h4>
<p>The <code>sizeof</code> operator comes in handy when you need to calculate the size of an array.</p>
<p>Let's see an example of the <code>sizeof</code> operator in action:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{

    <span class="hljs-keyword">int</span> grades[] = {<span class="hljs-number">50</span>, <span class="hljs-number">75</span>, <span class="hljs-number">100</span>, <span class="hljs-number">67</span>, <span class="hljs-number">90</span>};

    <span class="hljs-comment">// calculate the size of the array</span>
    <span class="hljs-keyword">int</span> array_size = <span class="hljs-keyword">sizeof</span>(grades);

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Size of array: %i bytes\n"</span>, array_size);
}
</code></pre>
<p>Output:</p>
<pre><code class="lang-plaintext">Size of array: 20 bytes
</code></pre>
<p>In the example above, <code>sizeof(grades)</code> calculates the total size of the array in bytes.</p>
<p>In this case, the array has five integers.</p>
<p>As mentioned in a previous chapter, on most modern systems an <code>int</code> typically occupies 4 bytes of memory. Therefore, the total size is <code>5 x 4 = 20</code> bytes of memory for the entire array.</p>
<p>Here is how you can check how much memory each <code>int</code> occupies using the <code>sizeof</code> operator:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{

    <span class="hljs-keyword">int</span> grades[] = {<span class="hljs-number">50</span>, <span class="hljs-number">75</span>, <span class="hljs-number">100</span>, <span class="hljs-number">67</span>, <span class="hljs-number">90</span>};

    <span class="hljs-comment">// calculate the size of a single array element</span>
    <span class="hljs-keyword">int</span> element_size = <span class="hljs-keyword">sizeof</span>(grades[<span class="hljs-number">0</span>]);

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Size of a single element: %i bytes\n"</span>, element_size);

}
</code></pre>
<p>Output:</p>
<pre><code class="lang-plaintext">Size of a single element: 4 bytes
</code></pre>
<p>The <code>sizeof(grades[0])</code> calculates the size of a single element in bytes.</p>
<p>By dividing the total size of the array by the size of a single element, you can calculate the number of elements in the array, which is equal to the array's length:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{

    <span class="hljs-keyword">int</span> grades[] = {<span class="hljs-number">50</span>, <span class="hljs-number">75</span>, <span class="hljs-number">100</span>, <span class="hljs-number">67</span>, <span class="hljs-number">90</span>};

     <span class="hljs-keyword">int</span> array_size = <span class="hljs-keyword">sizeof</span>(grades);

     <span class="hljs-keyword">int</span> element_size = <span class="hljs-keyword">sizeof</span>(grades[<span class="hljs-number">0</span>]);

     <span class="hljs-comment">// calculate the length of the array</span>
     <span class="hljs-keyword">int</span> length = array_size / element_size;

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Length of the array: %i elements\n"</span>, length);

}
</code></pre>
<p>Output:</p>
<pre><code class="lang-plaintext">Length of the array: 5 elements
</code></pre>
<h3 id="heading-how-to-access-array-elements-in-c">How to Access Array Elements in C</h3>
<p>You can access each element in an array by specifying its index or its position in the array.</p>
<p>Note that in C, indexing starts at <code>0</code> instead of <code>1</code>.</p>
<p>So, the index of the first element is <code>0</code>, the index of the second element is <code>1</code>, and so on.</p>
<p>The last element in an array has an index of <code>array_size - 1</code>.</p>
<p>To access individual elements in the array, you specify the array's name followed by the element's index number inside square brackets (<code>[]</code>).</p>
<pre><code class="lang-plaintext">array_name[index];
</code></pre>
<p>Let's take a look at the following example:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{

   <span class="hljs-keyword">int</span> grades[] = {<span class="hljs-number">50</span>, <span class="hljs-number">75</span>, <span class="hljs-number">100</span>, <span class="hljs-number">67</span>, <span class="hljs-number">90</span>};

   <span class="hljs-comment">// Access each array element using index notation</span>

   <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Element at index 0: %i\n"</span>, grades[<span class="hljs-number">0</span>]);  

   <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Element at index 1: %i\n"</span>, grades[<span class="hljs-number">1</span>]);  

   <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Element at index 2: %i\n"</span>, grades[<span class="hljs-number">2</span>]); 

   <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Element at index 3: %i\n"</span>, grades[<span class="hljs-number">3</span>]); 

   <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Element at index 4: %i\n"</span>, grades[<span class="hljs-number">4</span>]); 
}
</code></pre>
<p>Output:</p>
<pre><code class="lang-plaintext">Element at index 0: 50
Element at index 1: 75
Element at index 2: 100
Element at index 3: 67
Element at index 4: 90
</code></pre>
<p>In the example above, to access each item from the integer array <code>grades</code>, I have to specify the array's name along with the item's position in the array inside square brackets.</p>
<p>Remember that the index starts from <code>0</code>, so <code>grades[0]</code> gives you the first element, <code>grades[1]</code> gives you the second element, and so on.</p>
<p>Note that if you try to access an element with an index number that is higher than <code>array_size - 1</code>, the compiler will return a random number:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{

    <span class="hljs-keyword">int</span> grades[] = {<span class="hljs-number">50</span>, <span class="hljs-number">75</span>, <span class="hljs-number">100</span>, <span class="hljs-number">67</span>, <span class="hljs-number">90</span>};


    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Element at index 5: %d\n"</span>, grades[<span class="hljs-number">5</span>]);  

}
</code></pre>
<p>Output:</p>
<pre><code class="lang-plaintext">Element at index 5: 220312136
</code></pre>
<h3 id="heading-how-to-modify-array-elements-in-c">How to Modify Array Elements in C</h3>
<p>Once you know how to access array elements, you can then modify them.</p>
<p>The general syntax for modifying an array element looks like this:</p>
<pre><code class="lang-plaintext">array_name[index] = new_value;
</code></pre>
<p>You can change the value of an element by assigning a new value to it using its index.</p>
<p>Let's take the <code>grades</code> array from earlier on:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{

   <span class="hljs-keyword">int</span> grades[] = {<span class="hljs-number">50</span>, <span class="hljs-number">75</span>, <span class="hljs-number">100</span>, <span class="hljs-number">67</span>, <span class="hljs-number">90</span>};
}
</code></pre>
<p>Here is how you would change the value <code>75</code> to <code>85</code>:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{

   <span class="hljs-keyword">int</span> grades[] = {<span class="hljs-number">50</span>, <span class="hljs-number">75</span>, <span class="hljs-number">100</span>, <span class="hljs-number">67</span>, <span class="hljs-number">90</span>};

   grades[<span class="hljs-number">1</span>] = <span class="hljs-number">85</span>; <span class="hljs-comment">// changing the value at index 1 to 85</span>

   <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Element at index 1: %i\n"</span>, grades[<span class="hljs-number">1</span>]); 
}
</code></pre>
<p>Output:</p>
<pre><code class="lang-plaintext">Element at index 1: 85
</code></pre>
<p>When modifying arrays, keep in mind that the new value must match the declared data type of the array.</p>
<h3 id="heading-how-to-loop-through-an-array-in-c">How to Loop Through an Array in C</h3>
<p>By looping through an array, you can access and perform operations on each element sequentially.</p>
<p>The <code>for</code> loop is commonly used to iterate through arrays.</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{

    <span class="hljs-keyword">int</span> grades[] = {<span class="hljs-number">50</span>, <span class="hljs-number">75</span>, <span class="hljs-number">100</span>, <span class="hljs-number">67</span>, <span class="hljs-number">90</span>};

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">5</span>; i++) {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Element at index %i: %i\n"</span>, i, grades[i]);
    }
}
</code></pre>
<p>Output:</p>
<pre><code class="lang-plaintext">Element at index 0: 50
Element at index 1: 75
Element at index 2: 100
Element at index 3: 67
Element at index 4: 90
</code></pre>
<p>When using a <code>for</code> loop to loop through an array, you have to specify the index as the loop variable, and then use the index to access each array element.</p>
<p>The <code>%i</code> placeholders are replaced with the current index <code>i</code> and the value at that index in the grades array, respectively.</p>
<p>You can also use a <code>while</code> loop to iterate through an array:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{

    <span class="hljs-keyword">int</span> grades[] = {<span class="hljs-number">50</span>, <span class="hljs-number">75</span>, <span class="hljs-number">100</span>, <span class="hljs-number">67</span>, <span class="hljs-number">90</span>};

    <span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>;

    <span class="hljs-keyword">while</span> (i &lt; <span class="hljs-number">5</span>) {

        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Element at index %i: %i\n"</span>, i, grades[i]);
        i++;
    }
}
</code></pre>
<p>Output:</p>
<pre><code class="lang-plaintext">Element at index 0: 50
Element at index 1: 75
Element at index 2: 100
Element at index 3: 67
Element at index 4: 90
</code></pre>
<p>When using a <code>while</code> loop to loop through an array, you will need an index variable, <code>int i = 0</code>, to keep track of the current position in the array.</p>
<p>The loop checks the condition <code>(i &lt; 5)</code> and prints the index of the grade as well as the actual grade value.</p>
<p>After each grade is shown, the variable <code>i</code> is increased by one, and the loop continues until it has shown all the grades in the list.</p>
<p>A <code>do-while</code> works in a similar way to the <code>while</code> loop, but it is useful when you want to ensure that the loop body is executed at least once before checking the condition:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{

     <span class="hljs-keyword">int</span> grades[] = {<span class="hljs-number">50</span>, <span class="hljs-number">75</span>, <span class="hljs-number">100</span>, <span class="hljs-number">67</span>, <span class="hljs-number">90</span>};

    <span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>;

    <span class="hljs-keyword">do</span> {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Element at index %i: %i\n"</span>, i, grades[i]);

        i++;
    } <span class="hljs-keyword">while</span> (i &lt; <span class="hljs-number">5</span>);
}
</code></pre>
<p>You can also use the <code>sizeof</code> operator to loop through an array.</p>
<p>This method is particularly useful to ensure your loop doesn't exceed the array's length:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{

    <span class="hljs-keyword">int</span> grades[] = {<span class="hljs-number">50</span>, <span class="hljs-number">75</span>, <span class="hljs-number">100</span>, <span class="hljs-number">67</span>, <span class="hljs-number">90</span>};

    <span class="hljs-keyword">int</span> length = <span class="hljs-keyword">sizeof</span>(grades) / <span class="hljs-keyword">sizeof</span>(grades[<span class="hljs-number">0</span>]);

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; length; i++) {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Element at index %i: %i\n"</span>, i, grades[i]);
    }

}
</code></pre>
<p>The line <code>int length = sizeof(grades) / sizeof(grades[0]);</code> calculates the length of the <code>grades</code> array.</p>
<p>The length is calculated by dividing the total size (in bytes) of the array by the size of a single element <code>grades[0]</code>. The result is stored in the <code>length</code> variable.</p>
<p>The loop then iterates through the array using this <code>length</code> value.</p>
<p>For each iteration, it prints the index <code>i</code> and the value of the grade at that index <code>grades[i]</code>.</p>
<h2 id="heading-chapter-7-strings">Chapter 7: Strings</h2>
<p>In the previous chapter, you learned the basics of arrays in C.</p>
<p>Now, it's time to learn about strings – a special kind of array.</p>
<p>Strings are everywhere in programming. They are used to represent names, messages, passwords, and more.</p>
<p>In this chapter, you will learn about strings in C and how they are stored as arrays of characters.</p>
<p>You'll also learn the fundamentals of string manipulation.</p>
<p>Specifically, you will learn how to find a string's length and how to copy, concatenate, and compare strings in C.</p>
<h3 id="heading-what-are-strings-in-c">What Are Strings in C?</h3>
<p>A string is a sequence of characters, like letters, numbers, or symbols, that are used to represent text.</p>
<p>In C, strings are actually arrays of characters. And each character in the string has a specific position within the array.</p>
<p>Another unique characteristic of strings in C is that at the end of every one, there is a hidden <code>\0</code> character called the 'null terminator'.</p>
<p>This terminator lets the computer know where the string ends.</p>
<p>So, the string '<code>Hello</code>' in C is stored as '<code>Hello\0</code>' in memory.</p>
<h3 id="heading-how-to-create-strings-in-c">How to Create Strings in C</h3>
<p>One way to create a string in C is to initialize an array of characters.</p>
<p>The array will contain the characters that make up the string.</p>
<p>Here is how you would initialize an array to create the string 'Hello':</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
  <span class="hljs-keyword">char</span> word[<span class="hljs-number">6</span>] = {<span class="hljs-string">'H'</span>, <span class="hljs-string">'e'</span>, <span class="hljs-string">'l'</span>, <span class="hljs-string">'l'</span>, <span class="hljs-string">'o'</span>, <span class="hljs-string">'\0'</span>};

}
</code></pre>
<p>Note how I specified that the array should store <code>6</code> characters despite <code>Hello</code> being only <code>5</code> characters long. This is due to the null operator.</p>
<p>Make sure to include the null terminator, <code>\0</code>, as the last character to signify the end of the string.</p>
<p>Let's look at how you would create the string 'Hello world':</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
  <span class="hljs-keyword">char</span> phrase[<span class="hljs-number">12</span>] = {<span class="hljs-string">'H'</span>, <span class="hljs-string">'e'</span>, <span class="hljs-string">'l'</span>, <span class="hljs-string">'l'</span>, <span class="hljs-string">'o'</span>, <span class="hljs-string">' '</span>, <span class="hljs-string">'w'</span>, <span class="hljs-string">'o'</span>, <span class="hljs-string">'r'</span>, <span class="hljs-string">'l'</span>, <span class="hljs-string">'d'</span>, <span class="hljs-string">'\0'</span>};
}
</code></pre>
<p>In this example, there is a space between the word 'Hello' and the word 'world'.</p>
<p>So, the array must include a blank space character.</p>
<p>To print the string, you use the <code>printf()</code> function, the <code>%s</code> format code and the name of the array:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
  <span class="hljs-keyword">char</span> phrase[] = {<span class="hljs-string">'H'</span>, <span class="hljs-string">'e'</span>, <span class="hljs-string">'l'</span>, <span class="hljs-string">'l'</span>, <span class="hljs-string">'o'</span>, <span class="hljs-string">' '</span>, <span class="hljs-string">'w'</span>, <span class="hljs-string">'o'</span>, <span class="hljs-string">'r'</span>, <span class="hljs-string">'l'</span>, <span class="hljs-string">'d'</span>, <span class="hljs-string">'\0'</span>};

  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%s\n"</span>, phrase);

}
</code></pre>
<p>Another way to create a string in C is to use a string literal.</p>
<p>In this case, you create an array of characters and then assign the string by enclosing it in double quotes:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
  <span class="hljs-keyword">char</span> word[] = <span class="hljs-string">"Hello"</span>;

}
</code></pre>
<p>With string literals, the null terminator (<code>\0</code>) is implied.</p>
<p>Creating strings with string literals is easier, as you don't need to add the null terminator at the end. This method is also much more readable and requires less code.</p>
<p>However, you may want to use character arrays when you want to modify the string's content. String literals are read-only, meaning the content is fixed.</p>
<h3 id="heading-how-to-manipulate-strings-in-c">How to Manipulate Strings in C</h3>
<p>C provides functions that allow you to perform operations on strings, such as copying, concatenating, and comparing, to name a few.</p>
<p>To use these functions, you first need to include the <code>string.h</code> header file by adding the line <code>#include &lt;string.h&gt;</code> at the top of your file.</p>
<h4 id="heading-how-to-find-the-length-of-a-string-in-c">How to Find the Length of a String in C</h4>
<p>To calculate the length of a string, use the <code>strlen()</code> function:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;string.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
  <span class="hljs-keyword">char</span> phrase[] = <span class="hljs-string">"Hello"</span>;

  <span class="hljs-keyword">int</span> length = <span class="hljs-built_in">strlen</span>(phrase);

  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"String length: %i\n"</span>, length);

}
</code></pre>
<p>Output:</p>
<pre><code class="lang-plaintext">String length: 5
</code></pre>
<p>The <code>strlen()</code> function will return the number of characters that make up the string.</p>
<p>Note that the result does not include the null terminator, <code>\0</code>.</p>
<h4 id="heading-how-to-copy-a-string-in-c">How to Copy a String in C</h4>
<p>To copy one string into another one, you can use the <code>strcpy()</code> function.</p>
<p>You may want to copy a string in C when you need to make changes to it without modifying it. It comes in handy when you need to keep the original string's content intact.</p>
<p>The general syntax for the <code>strcpy()</code> function looks like this:</p>
<pre><code class="lang-plaintext">strcpy(destination_string, original_string);
</code></pre>
<p>The <code>strcpy()</code> function copies <code>original_string</code> into <code>destination_string</code>, including the null terminator (<code>'\0'</code>).</p>
<p>One thing to note here is that you need to make sure the destination array has enough space for the original string:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;string.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{

    <span class="hljs-keyword">char</span> original[] = <span class="hljs-string">"Hello"</span>;

    <span class="hljs-keyword">char</span> destination[<span class="hljs-number">20</span>]; <span class="hljs-comment">// Make sure this array is big enough</span>

    <span class="hljs-built_in">strcpy</span>(destination, original);

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Copied string: %s\n"</span>, destination);
}
</code></pre>
<p>Output:</p>
<pre><code class="lang-plaintext">Copied string: Hello
</code></pre>
<p>The <code>strcpy()</code> function copies the original string into an empty array and returns the copied string, which also includes the null terminator character (<code>'\0'</code>).</p>
<h4 id="heading-how-to-concatenate-strings-in-c">How to Concatenate Strings in C</h4>
<p>You can concatenate (add) two strings together by using the <code>strcat()</code> function.</p>
<p>The general syntax for the <code>strcat()</code> function looks something like the following:</p>
<pre><code class="lang-plaintext">strcat(destination_string, original_string);
</code></pre>
<p>The <code>strcat()</code> function takes the <code>original</code> string and adds it to the end of <code>destination</code> string.</p>
<p>Make sure that the <code>destination_string</code> has enough memory for the <code>original_string</code>.</p>
<p>Something to note here is that <code>strcat()</code> does not create a new string.</p>
<p>Instead, it modifies the original <code>destination_string</code>, by including the <code>original_string</code> at the end.</p>
<p>Let's see an example of how <code>strcat()</code> works:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;string.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{

  <span class="hljs-keyword">char</span> greeting[<span class="hljs-number">50</span>] = <span class="hljs-string">"Hello, "</span>;

  <span class="hljs-keyword">char</span> name[] = <span class="hljs-string">"Dionysia"</span>;

  <span class="hljs-built_in">strcat</span>(greeting, name);

  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Message: %s\n"</span>, greeting);

}
</code></pre>
<p>Output:</p>
<pre><code class="lang-plaintext">Message: Hello, Dionysia
</code></pre>
<h4 id="heading-how-to-compare-strings-in-c">How to Compare Strings in C</h4>
<p>To compare two strings for equality, you can use the <code>strcmp()</code> function.</p>
<p>The general syntax for the <code>strcmp()</code> function looks like this:</p>
<pre><code class="lang-plaintext">strcmp(string1, string2);
</code></pre>
<p>The <code>strcmp()</code> function compares <code>string1</code> with <code>string2</code> and returns an integer.</p>
<p>If the return value of <code>strcmp()</code> is <code>0</code>, then it means the two strings are the same:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;string.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{

  <span class="hljs-keyword">char</span> word1[] = <span class="hljs-string">"apples"</span>;
  <span class="hljs-keyword">char</span> word2[] = <span class="hljs-string">"apples"</span>;

  <span class="hljs-keyword">int</span> result = <span class="hljs-built_in">strcmp</span>(word1, word2);

  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Result: %i\n"</span>, result); <span class="hljs-comment">// Result: 0</span>

}
</code></pre>
<p>If the return value of <code>strcmp()</code> is less than <code>0</code>, then it means the first word comes before the second:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;string.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{

  <span class="hljs-keyword">char</span> word1[] = <span class="hljs-string">"apples"</span>;
  <span class="hljs-keyword">char</span> word2[] = <span class="hljs-string">"bananas"</span>;

  <span class="hljs-keyword">int</span> result = <span class="hljs-built_in">strcmp</span>(word1, word2);

  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Result: %i\n"</span>, result); <span class="hljs-comment">// Result: -1</span>

}
</code></pre>
<p>And if the return value of <code>strcmp()</code> is greater than <code>0</code>, then it means the first word comes after the second one:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;string.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{

  <span class="hljs-keyword">char</span> word1[] = <span class="hljs-string">"bananas"</span>;
  <span class="hljs-keyword">char</span> word2[] = <span class="hljs-string">"apples"</span>;

  <span class="hljs-keyword">int</span> result = <span class="hljs-built_in">strcmp</span>(word1, word2);

  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Result: %i\n"</span>, result); <span class="hljs-comment">// Result: 1</span>

}
</code></pre>
<h2 id="heading-further-learning-advanced-c-topics">Further learning: Advanced C Topics</h2>
<p>While this handbook has covered a wide range of topics, there is still so much to learn, as programming is so vast.</p>
<p>Once you have built a solid foundation with the basics of C programming, you may want to explore more advanced concepts.</p>
<p>You may want to move on to learning about functions, for example. They allow you to write instructions for a specific task and reuse that code throughout your program.</p>
<p>You may also want to learn about pointers. Pointers in C are like arrows that show you where a specific piece of information is stored in the computer's memory.</p>
<p>Then, you may want to move on to learning about structures. They're like custom data containers that allow you to group different types of information under one name.</p>
<p>Lastly, you may want to learn how to work with files. Working with files in C allows you to read from and write to files. This is useful for tasks like saving user data, reading configuration settings, or sharing data between different programs.</p>
<p>These suggestions are not a definitive guide – just a few ideas for you to continue your C programming learning journey.</p>
<p>If you are interested in learning more, you can check out the following freeCodeCamp resources:</p>
<ul>
<li><p><a target="_blank" href="https://www.youtube.com/watch?v=KJgsSFOSQv0&amp;t=12372s">C Programming Tutorial for Beginners</a></p>
</li>
<li><p><a target="_blank" href="https://www.freecodecamp.org/news/learn-c-programming-classic-book-dr-chuck/">Learn C Programming Using the Classic Book by Kernighan and Ritchie</a></p>
</li>
<li><p><a target="_blank" href="https://www.freecodecamp.org/news/finally-understand-pointers-in-c/">Unlock the Mysteries of Pointers in C</a></p>
</li>
</ul>
<h2 id="heading-conclusion">Conclusion</h2>
<p>This marks the end of this introduction to the C programming language.</p>
<p>Thank you so much for sticking with it and making it until the end.</p>
<p>You learned how to work with variables, various data types, and operators.</p>
<p>You also learned how to write conditional statements and loops. And you learned the basics of working with arrays and strings.</p>
<p>Hopefully, you have gained a good understanding of some of the fundamentals of C programming, got some inspiration on what to learn next, and are excited to continue your programming journey.</p>
<p>Happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Full Stack Engineer – Career Guide ]]>
                </title>
                <description>
                    <![CDATA[ Full-stack engineering roles have been growing in popularity over the last decade and are among the most sought-after positions in the tech job market. But what exactly are full-stack engineers? What do they do on a day-to-day basis? And how can you ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/full-stack-engineer-career-guide/</link>
                <guid isPermaLink="false">66b1e3ff41fdb67461b85267</guid>
                
                    <category>
                        <![CDATA[ Back end development  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Career development  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Front-end Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ full stack ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Software Engineering ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Dionysia Lemonaki ]]>
                </dc:creator>
                <pubDate>Wed, 10 May 2023 17:27:43 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/05/nubelson-fernandes-UcYBL5V0xWQ-unsplash.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Full-stack engineering roles have been growing in popularity over the last decade and are among the most sought-after positions in the tech job market.</p>
<p>But what exactly are full-stack engineers? What do they do on a day-to-day basis? And how can you become a full-stack engineer yourself?</p>
<p>In this article, I will go over the definition of full-stack engineering and cover some of the tasks that full-stack engineers work on. I will also mention some of the skills you will need to learn to become a software engineer.</p>
<h2 id="heading-what-is-a-full-stack-engineer-definition-of-full-stack-engineering">What Is A Full Stack Engineer? Definition of Full-Stack Engineering</h2>
<p>Modern web applications consist of two layers: the front-end and the back-end.</p>
<p>The front-end, also known as the client side, consists of the content, the presentation and layout of that content, and elements of interactivity. It includes all the visible parts a user views on a screen and can interact with.</p>
<p>The back-end, also known as the server side, consists of a server running the code with the necessary logic to receive, handle, and process requests, as well as a database for storing user data securely. It includes all the behind-the-scenes processes a user is not directly aware of. </p>
<p>A stack is a collection of technologies and refers to the combination of software, tools,  programming languages, frameworks, and data storage technologies that work together to build and run web applications.</p>
<p>There are many technology stacks, and each stack uses the same programming language throughout. </p>
<p>One of the most popular stacks for the JavaScript language is the MERN stack, which stands for MongoDB, Express, React, and NodeJs. </p>
<p>MongoDB is a document database, Express is a back-end web application framework for Node.js, React is a front-end JavaScript library, and NodeJS is a back-end JavaScript runtime environment.</p>
<p>A full-stack engineer is an engineer that knows how to build a web application from start to finish, including the front-end parts, the back-end parts, and the infrastructure it lives on. They work across the entire stack and understand each part of it.</p>
<h3 id="heading-what-does-a-full-stack-engineer-do-tasks-and-responsibilities-of-full-stack-engineers">What Does A Full Stack Engineer Do? Tasks and Responsibilities of Full Stack Engineers</h3>
<p>Full-stack engineers work on a broad set of problems and typically have end-to-end ownership of projects – from conceptualization to deployment. </p>
<p>Some of the everyday responsibilities may include:</p>
<ul>
<li>Gather project requirements by communicating with clients and stakeholders to understand the vision of the software product.</li>
<li>Brainstorm and collaborate with the design team and review design prototypes before coding them into a product.</li>
<li>Solve HTML, CSS, and JavaScript issues for the client-facing side of the application and work with front-end frameworks used within the application.</li>
<li>Ensure the web application is responsive for end-users and works on most devices.</li>
<li>Follow accessibility best practices.</li>
<li>Manage and maintain databases and servers to ensure the client-side works efficiently and optimally.</li>
<li>Ensure application security, maintenance, performance, uptime, and scalability.</li>
<li>Monitor software and write tests to ensure the code is working as intended. Find and fix bugs in the code to keep the software optimized.</li>
<li>Write clean, well-designed, and efficient code that adheres to industry best practices.</li>
<li>Keep up to date with new technological advances to improve business needs.</li>
<li>Create a Minimal Viable Product to showcase to stakeholders and communicate with decision-makers.</li>
<li>Implement new features after gathering feedback.</li>
<li>Review code written by other engineers and provide constructive feedback.</li>
<li>Read and write documentation that outlines the software development process.</li>
</ul>
<p>With that said, the tasks and responsibilities will depend on the size of the company.</p>
<p>For example, a small company may have only one full-stack developer who handles the entire application. A larger company may have front-end and back-end developers to work on specific tasks within their domain and expertise.</p>
<h3 id="heading-what-is-the-average-salary-for-full-stack-engineers">What Is the Average Salary for Full Stack Engineers?</h3>
<p>Software engineers, including full-stack engineers, generally command comfortable salaries.</p>
<p>According to the <a target="_blank" href="https://www.bls.gov/ooh/computer-and-information-technology/software-developers.htm">U.S Bureau of Labor Statistics</a>, the median salary for software engineers is $109,020 per year.</p>
<p>Specifically for full-stack engineers, <a target="_blank" href="https://www.glassdoor.com/Salaries/full-stack-engineer-salary-SRCH_KO0,19.htm">Glassdoor</a> lists the average salary at around $120,300 per year.</p>
<p><a target="_blank" href="https://www.indeed.com/career/full-stack-developer/salaries">Indeed</a> lists the average salary as $120,749. And the <a target="_blank" href="https://survey.stackoverflow.co/2022/#salary-united-states">Stack Overflow developer survey</a> lists that the average salary for full-stack developers is $140,000.</p>
<p>Note that these salaries are for full-stack engineers based in the USA. The compensation will depend on your location and years of experience.</p>
<p>Google the average salary for software engineers in your location with the same experience as you to help you get a better idea.</p>
<h2 id="heading-how-to-become-a-software-engineer-skills-required-for-full-stack-engineers">How To Become A Software Engineer – Skills Required for Full Stack Engineers</h2>
<p>In the following sections, I will go over just a few of the technologies you would need to learn to become a full-stack engineer.</p>
<p>Becoming a full-stack engineer takes time, and you can't be an expert in all the tools and technologies available – it is more about having well-rounded knowledge of technology in general and knowing enough to solve problems using code.</p>
<h3 id="heading-learn-internet-and-web-fundamentals">Learn Internet and Web Fundamentals</h3>
<p>As a full-stack engineer, you will benefit from understanding how the internet works and being familiar with some web terms, such as DNS and IP addresses.</p>
<p>To learn more about DNS, IP addresses, and how the internet works, check out the following resources:</p>
<ul>
<li><a target="_blank" href="https://www.freecodecamp.org/news/how-the-web-works-a-primer-for-newcomers-to-web-development-or-anyone-really-b4584e63585c/">How the Web Works: A Primer for Newcomers to Web Development (or anyone, really)</a></li>
<li><a target="_blank" href="https://www.freecodecamp.org/news/how-the-web-works-part-ii-client-server-model-the-structure-of-a-web-application-735b4b6d76e3/">How the Web Works Part II: Client-Server Model &amp; the Structure of a Web Application</a></li>
<li><a target="_blank" href="https://www.freecodecamp.org/news/what-is-dns/">What is DNS? Domain Name System, DNS Server, and IP Address Concepts Explained</a></li>
</ul>
<p>You will also need to know HTTP (HyperText Transfer Protocol), the foundation of the World Wide Web, as it manages the communication between clients (such as web browsers) and servers on the web.</p>
<p>Specifically, you need to know about HTTP request methods (such as GET, POST, PUT, PATCH, and DELETE) and HTTP response codes (such as 200, 404, and 500).</p>
<p>To learn more about HTTP, check out the following resources:</p>
<ul>
<li><a target="_blank" href="https://www.freecodecamp.org/news/what-is-http/">What is HTTP? Protocol Overview for Beginners</a></li>
<li><a target="_blank" href="https://www.freecodecamp.org/news/how-the-internet-works/">How HTTP Works and Why it's Important – Explained in Plain English</a></li>
<li><a target="_blank" href="https://www.freecodecamp.org/news/http-networking-protocol-course/">Master the HTTP Networking Protocol</a></li>
<li><a target="_blank" href="https://www.freecodecamp.org/news/http-request-methods-explained/">HTTP Request Methods – Get vs Put vs Post Explained with Code Examples</a></li>
</ul>
<h3 id="heading-learn-front-end-web-development-fundamentals">Learn Front-end Web Development Fundamentals</h3>
<p>Only three front-end web languages run in all modern web browsers: HTML, CSS, and JavaScript.</p>
<p>HTML (short for HyperText Markup Language) defines the structure and content on a web page, such as text, links, forms, images, videos, and so on. </p>
<p>To learn HTML, check out <a target="_blank" href="https://www.freecodecamp.org/news/learn-html-beginners-course/">this course</a>.</p>
<p>CSS (short for Cascading Style Sheets) styles the HTML content and makes it aesthetically pleasing – it determines the look and feel of a web page.  </p>
<p>CSS is responsible for sizing, displaying, laying out, and presenting elements on a page. CSS is also responsible for web pages being usable on all screen sizes. </p>
<p>To learn more about CSS, check out <a target="_blank" href="https://www.freecodecamp.org/news/learn-css-in-this-free-6-hour-video-course/">this course</a>, which covers Flexbox and Grid – two important CSS topics. Once you understand the basics, you could learn a CSS framework, like <a target="_blank" href="https://www.freecodecamp.org/news/learn-tailwind-css/">Tailwind CSS</a>.</p>
<p>JavaScript is a dynamic scripting programming language designed to run in the browser. </p>
<p>It is the only programming language you can use for front-end web development, making it an essential part of web development. It is used together with HTML and CSS to create interactive web pages. </p>
<p>To learn more about JavaScript, checkout out <a target="_blank" href="https://www.freecodecamp.org/news/full-javascript-course-for-beginners/">this course</a>.</p>
<h3 id="heading-learn-git-and-github">Learn Git and GitHub</h3>
<p>Git and GitHub are a core part of the development workflow and are tools used in every software development job.</p>
<p>Git is a distributed version control system that offers a way to make changes to your projects, back up those changes, keep track of them, and even go back to them if needed. It also lets you collaborate with other team members at the same time.</p>
<p>GitHub is an online hosting service that makes it easier to use Git and is a place for you and your team to upload and review code.</p>
<p>To learn more about Git and GitHub, check out <a target="_blank" href="https://www.freecodecamp.org/news/git-and-github-crash-course/">this course</a>.</p>
<h3 id="heading-learn-a-front-end-library-and-framework">Learn A Front-end Library and Framework</h3>
<p>Once you understand the core concepts of JavaScript, you can move on to learning one of JavaScript's front-end libraries and frameworks. </p>
<p>A front end library is pre-written reusable code containing various functions, methods, or objects you can use in your JavaScript project to perform tasks and implement specific functionalities. And a web framework is a tool that makes it easier and faster to create web applications. </p>
<p>According to the <a target="_blank" href="https://survey.stackoverflow.co/2022/#technology-most-popular-technologies">Stack Overflow Survey for 2022</a>, the most popular and commonly used JavaScript library to learn is ReactJS. </p>
<p>To learn more about ReactJS, check out <a target="_blank" href="https://www.freecodecamp.org/news/free-react-course-2022/">this course</a>.</p>
<p>With that said, there are other frameworks to work with and consider learning, such as <a target="_blank" href="https://www.freecodecamp.org/news/vue-3-full-course/">Vue</a>, <a target="_blank" href="https://www.freecodecamp.org/news/learn-angular-full-course/">Angular</a> and <a target="_blank" href="https://www.freecodecamp.org/news/learn-svelte-complete-course/">Svelte</a>. Each has its way of organizing and writing code, as well as its own benefits and limitations.</p>
<p>Make sure you have a solid foundation in JavaScript before learning these tools.</p>
<h3 id="heading-learn-back-end-web-development">Learn Back-end Web Development</h3>
<p>As a full-stack web developer, you need to know front-end technologies and back-end tools, so you will also need to be able to work with server-side scripting programming languages.</p>
<p>There are many to choose from, such as Python, Ruby, and Java, to name a few.</p>
<p>And although JavaScript is used widely in front-end development, in recent years, it is also used for back-end web development with the help of NodeJS.</p>
<p>NodeJS is a JavaScript runtime that provides back-end functionality and is designed to build dynamic scalable web applications.</p>
<p>First, <a target="_blank" href="https://www.freecodecamp.org/news/what-is-npm-a-node-package-manager-tutorial-for-beginners/">learn how to use NPM</a>, a Node package manager for installing and managing local dependencies for JavaScript packages. It is designed specifically for use with NodeJS.</p>
<p>To learn the basics of NodeJS, check out <a target="_blank" href="https://www.freecodecamp.org/news/nodejs-course/">this course</a>.</p>
<p>You can pair NodeJS with the ExpressJS server-side web framework to create full-stack web applications. To learn back-end development with NodeJS and ExpressJS, check out <a target="_blank" href="https://www.freecodecamp.org/news/free-8-hour-node-express-course/">this course</a>.</p>
<h3 id="heading-learn-database-management-systems-and-sql">Learn Database Management Systems and SQL</h3>
<p>As a full-stack engineer, you will be working with databases as most web applications you will be working with will have a database. A lot of your time will be spent writing database queries to fetch the data you need.</p>
<p>A database is a storage container – a place to store all the data used in your project, such as user data.</p>
<p>There are two main types of databases:</p>
<ul>
<li>SQL or <a target="_blank" href="https://www.freecodecamp.org/news/what-is-a-relational-database-rdbms-definition/">Relational databases</a>, also referred to as SQL databases, which store data in a structured, organized, tabular format. </li>
<li>Non-relational or <a target="_blank" href="https://www.freecodecamp.org/news/learn-nosql-in-3-hours/">NoSQL databases</a>, which don't store data in tables. </li>
</ul>
<p>A database has a program called a database management system (DBMS), which serves as an interface between the database, allowing users or programs to 
retrieve, update, and manage the data.</p>
<p>To learn about Relational Databases,  check out  <a target="_blank" href="https://www.freecodecamp.org/learn/relational-database/">freeCodeCamp's Relational Database Course</a>.</p>
<p>To communicate with relational databases and manipulate stored data, you query them using a query language such as SQL (short for Structured Query Language). To learn more about SQL, check out <a target="_blank" href="https://www.freecodecamp.org/news/learn-sql-in-10-minutes/">this resource</a></p>
<p>And if you want to get started with a NoSQL database management system, MongoDB is a great place to start. To get started with MongoDB, check out <a target="_blank" href="https://learn.mongodb.com/">these courses</a>.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Hopefully, you found this article helpful and have a better understanding of what full-stack engineers do.</p>
<p>In this article, we went over the definition of full-stack engineering and covered some of the tasks full-stack engineers work on. </p>
<p>We also covered some of the tools and languages you would need to know to become a full-stack engineer yourself. </p>
<p>Thank you for reading!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ AWS Cloud Practitioner Salary – Amazon Certification Guide ]]>
                </title>
                <description>
                    <![CDATA[ More and more companies are making a shift towards cloud services and cloud-based systems these days, resulting in cloud computing being one of the most in-demand tech skills. AWS is currently the most popular cloud service provider, as it owns aroun... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/aws-cloud-practitioner-salary-2023-amazon-certification-guide/</link>
                <guid isPermaLink="false">66b1e3c66f537a6f7e7212bb</guid>
                
                    <category>
                        <![CDATA[ AWS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Cloud ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Cloud Services ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Dionysia Lemonaki ]]>
                </dc:creator>
                <pubDate>Wed, 03 May 2023 06:51:34 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/04/ethan-yrGn-av5WF8-unsplash.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>More and more companies are making a shift towards cloud services and cloud-based systems these days, resulting in cloud computing being one of the most in-demand tech skills.</p>
<p>AWS is currently the most popular cloud service provider, as it owns around 33% of the cloud market.</p>
<p>Companies and organizations are looking for candidates with AWS skills. A way to showcase your AWS knowledge is by obtaining an AWS certification.</p>
<p>In this article, I will list the current AWS certifications and go into more detail about the Cloud Practitioner certification.</p>
<p>Let's get into it!</p>
<h2 id="heading-what-is-aws-amazon-web-services-explained">What Is AWS? Amazon Web Services Explained</h2>
<p>Amazon Web Services (or AWS for short) is a cloud computing platform offered by Amazon.</p>
<p>The platform provides on-demand cloud computing services such as hosting for servers, storage, database management, networking, and security, to name just a few of them.</p>
<p>Many businesses use AWS, including big companies such as Airbnb, Netflix, LinkedIn, and Twitter. With that said, it is also used for personal projects.</p>
<h2 id="heading-what-is-an-aws-certification">What Is an AWS Certification?</h2>
<p>An AWS certification is a credible way to demonstrate to an employer that you have the specific technical skills and competence to design, build, deploy, migrate, operate, and maintain well-architected AWS systems.</p>
<p>Obtaining an AWS certification and gaining cloud architecture expertise is a great way to kickstart a new career in tech and open doors in a fast-growing industry.</p>
<h2 id="heading-an-overview-of-the-different-aws-certification-types-levels-of-aws-certifications">An Overview of the Different AWS Certification Types – Levels of AWS Certifications</h2>
<p>Currently, there are four types of AWS certifications.</p>
<p>There is one <strong>foundational</strong> level certification – the <a target="_blank" href="https://aws.amazon.com/certification/certified-cloud-practitioner/?ch=sec&amp;sec=rmg&amp;d=1">Cloud Practitioner</a> certification.</p>
<p>There are three <strong>associate</strong> level certifications – the <a target="_blank" href="https://aws.amazon.com/certification/certified-solutions-architect-associate/?ch=sec&amp;sec=rmg&amp;d=1">Solutions Architect</a>, the <a target="_blank" href="https://aws.amazon.com/certification/certified-developer-associate/?ch=sec&amp;sec=rmg&amp;d=1">Developer</a> and the <a target="_blank" href="https://aws.amazon.com/certification/certified-sysops-admin-associate/?ch=sec&amp;sec=rmg&amp;d=1">SysOps Administrator</a> certifications.</p>
<p>There are also two <strong>professional</strong> level certifications – the <a target="_blank" href="https://aws.amazon.com/certification/certified-solutions-architect-professional/?ch=sec&amp;sec=rmg&amp;d=1">Solutions Architect</a> and <a target="_blank" href="https://aws.amazon.com/certification/certified-devops-engineer-professional/?ch=sec&amp;sec=rmg&amp;d=1">DevOps Engineer</a> certifications.</p>
<p>And lastly, there are six <strong>specialty</strong> level certifications – the <a target="_blank" href="https://aws.amazon.com/certification/certified-advanced-networking-specialty/?ch=sec&amp;sec=rmg&amp;d=1">Advanced Networking</a>, <a target="_blank" href="https://aws.amazon.com/certification/certified-data-analytics-specialty/?ch=sec&amp;sec=rmg&amp;d=1">Data Analytics</a>, <a target="_blank" href="https://aws.amazon.com/certification/certified-database-specialty/?ch=sec&amp;sec=rmg&amp;d=1">Database</a>, <a target="_blank" href="https://aws.amazon.com/certification/certified-machine-learning-specialty/?ch=sec&amp;sec=rmg&amp;d=1">Machine Learning</a>, <a target="_blank" href="https://aws.amazon.com/certification/certified-security-specialty/?ch=sec&amp;sec=rmg&amp;d=1">Security</a> and <a target="_blank" href="https://aws.amazon.com/certification/certified-sap-on-aws-specialty/?ch=sec&amp;sec=rmg&amp;d=1">SAP on AWS</a> certifications.</p>
<p>Choosing an AWS certification will depend on your experience level, professional goals, and interests.</p>
<p>Now, let's go into more detail on the fundamental AWS certification – the Cloud Practitioner certification.</p>
<h2 id="heading-what-does-an-aws-cloud-practitioner-do">What Does an AWS Cloud Practitioner Do?</h2>
<p>An AWS cloud practitioner is responsible for the organization's cloud computing architecture. They resolve scalability challenges and handle high-risk issues.</p>
<p>The practitioner understands AWS's core design principles and best practices for architecture.</p>
<p>They know how to design, build, deploy, and monitor applications on the cloud within AWS platforms.</p>
<p>They gather insights into end-users' problems and pain points, leverage software and hardware systems to address those problems, and come up with solutions.</p>
<h2 id="heading-what-is-the-average-salary-for-an-aws-cloud-practitioner">What Is the Average Salary for an AWS Cloud Practitioner?</h2>
<p>According to data from Glassdoor, the estimated total pay for a cloud practitioner in the USA for 2023 is around $91,038 per year, with an average salary of $83,679 per year.</p>
<p>With that said, compensation is relative and will depend on your field and chosen industry, demand, previous experience, skills, and location. The average salary for a cloud practitioner may be higher in different regions in the USA and lower in other countries.</p>
<h2 id="heading-aws-cloud-practitioner-certification-prerequisites">AWS Cloud Practitioner Certification Prerequisites</h2>
<p>The AWS Cloud Practitioner certification is a great place to start your cloud computing learning journey and a new career in the cloud.</p>
<p>It is an entry-level certification intended to provide fundamental knowledge and a general overview of AWS and its infrastructure. It doesn't require any previous experience or specific prerequisites.</p>
<p>With that said, having an understanding of the AWS platform and cloud computing terminology and concepts can be helpful during your learning process.</p>
<p>To understand the core concepts of cloud computing and AWS, you can read through the <a target="_blank" href="https://aws.amazon.com/getting-started/cloud-essentials/">AWS Cloud Essentials Guide</a> by AWS.</p>
<h2 id="heading-aws-cloud-practitioner-certification-curriculum">AWS Cloud Practitioner Certification Curriculum</h2>
<p>The four major topics you will learn while studying for the AWS Cloud Practitioner certification fall into the following categories:</p>
<ul>
<li><p><strong>Cloud concepts</strong> (26%), such as learning about cloud computing topics, how cloud-based applications work and scale, the AWS core infrastructure, and its architectural principles.</p>
</li>
<li><p><strong>Security and compliance</strong> (25%), such as learning security and compliance best practices for the AWS platform.</p>
</li>
<li><p><strong>Technology</strong> (33%), such as learning about AWS services and tools and their use cases.</p>
</li>
<li><p><strong>Billing and pricing</strong> (16%), such as learning about AWS billing, pricing models, support, and account management.</p>
</li>
</ul>
<p>To learn more about the curriculum, AWS provides a <a target="_blank" href="https://d1.awsstatic.com/training-and-certification/docs-cloud-practitioner/AWS-Certified-Cloud-Practitioner_Exam-Guide.pdf">complete exam guide</a>, which covers the key modules in detail.</p>
<h2 id="heading-aws-cloud-practitioner-certification-study-materials">AWS Cloud Practitioner Certification Study Materials</h2>
<p>You first need to create an <a target="_blank" href="https://aws.amazon.com/resources/create-account/">AWS account</a>.</p>
<p>AWS provides a <a target="_blank" href="https://aws.amazon.com/free/?all-free-tier&amp;all-free-tier.sort-by=item.additionalFields.SortRank&amp;all-free-tier.sort-order=asc&amp;awsf.Free%20Tier%20Types=*all&amp;awsf.Free%20Tier%20Categories=*all">free 12-month subscription</a> to become familiar with the AWS console and its services.</p>
<p>When it comes to courses, you can use the <a target="_blank" href="https://aws.amazon.com/training/learn-about/cloud-practitioner/">AWS cloud essentials learning plan</a> built by AWS, which goes over the recommended curriculum. This course covers AWS cloud, services, pricing, and security.</p>
<p>freeCodeCamp also offers an extensive <a target="_blank" href="https://www.freecodecamp.org/news/aws-certified-cloud-practitioner-certification-study-course-pass-the-exam/">13-hour study course</a>.</p>
<p>It's also a good idea to go through some <a target="_blank" href="https://d1.awsstatic.com/training-and-certification/docs-cloud-practitioner/AWS-Certified-Cloud-Practitioner_Sample-Questions.pdf">practice questions</a> and <a target="_blank" href="https://aws.amazon.com/certification/certification-prep/">practice exams</a> by the official page on Amazon to test whether you are prepared enough for the certification exam.</p>
<h2 id="heading-aws-cloud-practitioner-certification-exam-details">AWS Cloud Practitioner Certification Exam Details</h2>
<p>Once you've created your AWS certification account, to schedule the AWS Cloud Practitioner exam, sign in to <a target="_blank" href="https://www.aws.training/">aws.training</a> and click "Certification", where you can register and schedule the exam online or at a location near you.</p>
<p>The exam level is foundational, and the exam code is CLF-C01.</p>
<p>The exam costs $100 and is available in English, French, German, Indonesian, Italian, Japanese, Korean, Portuguese, Simplified Chinese, Spanish, and Traditional Chinese.</p>
<p>The exam is 90 minutes long and consists of 65 multiple-choice, multiple-response questions. The minimum passing score is 700 points.</p>
<p>Lastly, the AWS certifications are valid for three years, and you will need to renew them once they expire.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Hopefully, you found this article helpful and have a better understanding of what the AWS Cloud Practitioner certification entails.</p>
<p>Thank you for reading, and best of luck on your exam.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Python Copy File – Copying Files to Another Directory ]]>
                </title>
                <description>
                    <![CDATA[ When working with Python, there may be times when you need to copy a file. Copying files comes in handy when you need to create a backup. In this article, you will learn how to copy a file in Python using the shutil module and its different methods. ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/python-copy-file-copying-files-to-another-directory/</link>
                <guid isPermaLink="false">66b1e46c6500ee13f33bd584</guid>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Dionysia Lemonaki ]]>
                </dc:creator>
                <pubDate>Thu, 20 Apr 2023 14:16:43 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/04/maksym-kaharlytskyi-Q9y3LRuuxmg-unsplash.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>When working with Python, there may be times when you need to copy a file. Copying files comes in handy when you need to create a backup.</p>
<p>In this article, you will learn how to copy a file in Python using the <code>shutil</code> module and its different methods.</p>
<p>The <code>shutil</code> (short for shell utility) module in Python lets you manipulate files and directories and perform file and directory operations.</p>
<p>Let's get into it!</p>
<h2 id="heading-how-to-copy-a-file-using-the-shutilcopyfile-method-in-python">How To Copy A File Using The <code>shutil.copyfile()</code> Method In Python</h2>
<p>To copy the contents of a file into another file, use the <code>shutil.copyfile()</code> method.</p>
<p>Let's look at the following example:</p>
<pre><code class="lang-python"><span class="hljs-comment"># import module</span>
<span class="hljs-keyword">import</span> shutil

<span class="hljs-comment"># copy the contents of the demo.py file to  a new file called demo1.py</span>
shutil.copyfile(<span class="hljs-string">'./demo.py'</span>, <span class="hljs-string">'./demo1.py'</span>)
</code></pre>
<p>I first import the module with the <code>import shutil</code> statement.</p>
<p>Then, I use the <code>shutil.copyfile()</code> method which has the following syntax:</p>
<pre><code class="lang-python">shutil.copyfile(<span class="hljs-string">'source_file'</span>, <span class="hljs-string">'destination_file'</span>)
</code></pre>
<p>Let's break it down:</p>
<ul>
<li><code>source_file</code> is the path to the file I want to copy – in this case, the file is the <code>demo.py</code> file in my current working directory (<code>./</code>).</li>
<li><code>destination_file</code> is the path to the new file I want to create. In this case, I want to copy the contents of the source file into a new file, <code>demo1.py</code>, in my current working directory. The destination cannot be a directory – it must be a file name.</li>
</ul>
<p>When I run the code from the example above, a new file named <code>demo1.py</code> gets created in my current working directory with a copy of <code>demo.py</code>'s contents. If the destination file already exists, it gets replaced.</p>
<p>Note that the <code>shutil.copyfile()</code> method only copies the contents of the source file.  </p>
<p>No file metadata (such as creation dates and modification times) or file permissions are copied over to the specified destination file. </p>
<p>So, the <code>shutil.copyfile()</code> method is useful when you want to rename the file you are copying and are not concerned about saving file permissions and metadata.</p>
<h2 id="heading-how-to-copy-a-file-using-shutilcopy-method-in-python">How To Copy A File Using <code>shutil.copy()</code> Method In Python</h2>
<p>To copy a file to another directory, use the <code>shutil.copy()</code> method.</p>
<p>Let’s look at the following example:</p>
<pre><code class="lang-python"><span class="hljs-comment"># import the module</span>
<span class="hljs-keyword">import</span> shutil

<span class="hljs-comment"># Specify the path of the file you want to copy</span>
file_to_copy = <span class="hljs-string">'./demo.py'</span>

<span class="hljs-comment"># Specify the path of the destination directory you want to copy to</span>
destination_directory = <span class="hljs-string">'./projects'</span>

<span class="hljs-comment"># Use the shutil.copy() method to copy the file to the destination directory</span>
shutil.copy(file_to_copy, destination_directory)
</code></pre>
<p>I first import the module using the <code>import shutil</code> statement.</p>
<p>Then, I specify the path of the file I want to copy and save it in a variable named <code>file_to_copy</code>. In this case, I want to copy the <code>demo.py</code> file in my current working directory.</p>
<p>Next, I specify the directory I want to copy the file and save it in a variable named <code>destination_directory</code>. In this case, I want to save the file in the <code>projects</code> directory in my current working directory.</p>
<p>Lastly, I use the <code>shutil.copy()</code> method which takes two arguments: </p>
<ul>
<li>The path of the file you want to copy – in this case, the variable <code>file_to_copy</code>.</li>
<li>The file or directory you want to copy the file into – in this case,  the variable <code>destination_directory</code>.</li>
</ul>
<p>When I run the code from the example above, the <code>shutil.copy()</code> method creates a copy of the <code>demo.py</code> file in the <code>projects</code> directory.</p>
<p>Keep in mind that if a file with the same name already exists in the destination directory, the existing file gets overwritten by the new file.</p>
<p>Another thing to keep in mind is that the <code>shutil.copy()</code> method copies file permissions, but it doesn’t copy metadata over to the destination directory.</p>
<h2 id="heading-how-to-copy-a-file-using-the-shutilcopy2-method-in-python">How To Copy A File Using The <code>shutil.copy2()</code> Method In Python</h2>
<p>The <code>shutil.copy2()</code> method works similarly to the <code>shutil.copy()</code> method. </p>
<p>The only difference between <code>shutil.copy()</code> and <code>shutil.copy2()</code> method is that <code>shutil.copy2()</code> preserves the original file metadata when copying.</p>
<pre><code class="lang-python"><span class="hljs-comment"># import the module</span>
<span class="hljs-keyword">import</span> shutil

<span class="hljs-comment"># Specify the path of the file you want to copy</span>
file_to_copy = <span class="hljs-string">'./demo.py'</span>

<span class="hljs-comment"># Specify the path of the destination directory you want to copy the file into</span>
destination_directory = <span class="hljs-string">'./projects'</span>

<span class="hljs-comment"># Use the shutil.copy2() method to copy the file to the destination directory</span>
shutil.copy2(file_to_copy, destination_directory)
</code></pre>
<h2 id="heading-how-to-copy-a-file-using-the-shutilcopyfileobj-method-in-python">How To Copy A File Using The <code>shutil.copyfileobj()</code> Method In Python</h2>
<p>To copy the contents of a file object to another specified destination file object, use the <code>shutil.copyfileobj()</code> method. This method takes two file objects as arguments – a source file object and a destination file object. The destination cannot be a directory.</p>
<p>Let's take the following example:</p>
<pre><code class="lang-python"><span class="hljs-comment"># import module</span>
<span class="hljs-keyword">import</span> shutil

<span class="hljs-comment"># you have to open the source  file in binary mode with 'rb'</span>
source_file =  open(<span class="hljs-string">'demo.py'</span>, <span class="hljs-string">'rb'</span>)

<span class="hljs-comment"># you have to open the destination file in binary mode with 'wb'</span>
destination_file = open(<span class="hljs-string">'project.py'</span>, <span class="hljs-string">'wb'</span>)

<span class="hljs-comment"># use the shutil.copyobj() method to copy the contents of source_file to destination_file</span>
shutil.copyfileobj(source_file, destination_file)
</code></pre>
<p>In the example above, the <code>shutil.copyobj()</code> method copies the contents of <code>demo.py</code> to the <code>project.py</code> file.</p>
<p>Keep in mind that this method does not preserve file permissions and it doesn't copy metadata.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>And there you have it! You now know how to copy files in Python using the <code>shutil</code> module and the methods it offers.</p>
<p>To help you choose which method to use, refer to the following table that summarises what each method does.</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Method</td><td>Preserves permissions?</td><td>Copies metadata?</td><td>Can destination be a directory?</td><td>Accepts file object?</td></tr>
</thead>
<tbody>
<tr>
<td><code>shutil.copyfile()</code></td><td>No</td><td>No</td><td>No</td><td>No</td></tr>
<tr>
<td><code>shutil.copy()</code></td><td>Yes</td><td>No</td><td>Yes</td><td>No</td></tr>
<tr>
<td><code>shutil.copy2()</code></td><td>Yes</td><td>Yes</td><td>Yes</td><td>No</td></tr>
<tr>
<td><code>shutil.copyfileobj()</code></td><td>No</td><td>No</td><td>No</td><td>Yes</td></tr>
</tbody>
</table>
</div><p>To learn more about Python, check out freeCodeCamp's <a target="_blank" href="https://www.freecodecamp.org/news/python-programming-course/">Python for beginners course</a>.</p>
<p>Thanks for reading, and happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Become a Software Engineer – Detailed Roadmap ]]>
                </title>
                <description>
                    <![CDATA[ Software engineers are in high demand these days. They generally command comfortable salaries, and can have a good work-life balance. But what do software engineers do? And how can you become one yourself? In this article, I will explain the tasks an... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-become-a-software-engineer-2023-roadmap/</link>
                <guid isPermaLink="false">66b1e40ee97f6cb83ae127b1</guid>
                
                    <category>
                        <![CDATA[ learn to code ]]>
                    </category>
                
                    <category>
                        <![CDATA[ software development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Software Engineering ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Dionysia Lemonaki ]]>
                </dc:creator>
                <pubDate>Thu, 13 Apr 2023 16:15:24 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/04/pexels-christina-morillo-1181675.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Software engineers are in high demand these days. They generally command comfortable salaries, and can have a good work-life balance.</p>
<p>But what do software engineers do? And how can you become one yourself?</p>
<p>In this article, I will explain the tasks and responsibilities of software engineers, and I will go over some of the areas they most commonly specialize in.</p>
<p>I will also list some of the most important skills needed if you want to become a software engineer yourself.</p>
<p>Here is what we will cover:</p>
<ol>
<li><p><a class="post-section-overview" href="#intro">What is software engineering?</a></p>
</li>
<li><p><a class="post-section-overview" href="#tasks">What does a software engineer do?</a></p>
</li>
<li><p><a class="post-section-overview" href="#areas">What are the software engineering specializations?</a></p>
</li>
<li><p><a class="post-section-overview" href="#career-choice">Why should you choose a career in software engineering?</a></p>
</li>
<li><p><a class="post-section-overview" href="#roadmap">How to become a software engineer</a></p>
<ol>
<li><p><a class="post-section-overview" href="#education">Choose your education environment – university degree vs bootcamp vs self-directed learning</a></p>
</li>
<li><p><a class="post-section-overview" href="#learn-how-to-learn">Learn how to learn</a></p>
</li>
<li><p><a class="post-section-overview" href="#programming-language">Learn a programming language</a></p>
</li>
<li><p><a class="post-section-overview" href="#data-structures-algorithms">Learn data structures and algorithms</a></p>
</li>
<li><p><a class="post-section-overview" href="#database">Learn database architecture and SQL</a></p>
</li>
<li><p><a class="post-section-overview" href="#tools">Learn how to use software engineering tools</a></p>
</li>
<li><p><a class="post-section-overview" href="#soft-skills">Build your non-technical skills</a></p>
</li>
</ol>
</li>
</ol>
<p>Let's get started!</p>
<h2 id="heading-what-is-software-engineering-definition-of-software-engineering">What Is Software Engineering? Definition of Software Engineering</h2>
<p>Every day, you likley visit various web pages and use web and mobile applications.</p>
<p>These modern tools have hundreds or even thousands of lines of code behind them. And those lines of code have been written by a software engineer.</p>
<p>Software engineering is the process of designing, building, testing, deploying, and maintaining customer-facing software products, complex computer information systems, and useful tools. Software engineers do this using software engineering methodologies, computer science principles, and programming languages.</p>
<p>Software engineers write code for hardware, operating systems, embedded systems, networks, enterprise applications, websites, or video games.</p>
<p>They come up with strategic solutions that meet the needs of end-users and the business they work for. The code they write solves a particular real-world problem.</p>
<h2 id="heading-what-does-a-software-engineer-do-tasks-and-responsibilities-of-software-engineers">What Does a Software Engineer Do? Tasks and Responsibilities of Software Engineers</h2>
<p>In general, here are some of the tasks and responsibilities of software engineers on a day-to-day basis:</p>
<ul>
<li><p>Gather and analyze the needs and requirements of users and understand how they use the software.</p>
</li>
<li><p>Organise user requirements into individual categories to implement the user suggestions and feedback.</p>
</li>
<li><p>Use programming languages to design and build software systems and applications that meet user requirements.</p>
</li>
<li><p>Create efficient, reliable, secure, accessible, and easy-to-use software.</p>
</li>
<li><p>Test, debug, troubleshoot, and maintain existing software systems.</p>
</li>
<li><p>Optimise code for quality, performance, speed, and scalability.</p>
</li>
<li><p>Solve problems that will arise and help the business achieve its goal(s).</p>
</li>
<li><p>Create flowcharts, documentation, and technical specifications that outline the whole process from start to finish to help share findings and solutions with other team members.</p>
</li>
<li><p>Consult with members from different teams in the organization.</p>
</li>
<li><p>Present new features and updates to stakeholders and customers.</p>
</li>
</ul>
<p>That said, the tasks and responsibilities depend on the area the software engineer specializes in.</p>
<h2 id="heading-what-are-the-software-engineering-specializations-software-engineering-specialization-areas">What Are The Software Engineering Specializations? Software Engineering Specialization Areas</h2>
<p>Software engineering is a diverse field, meaning there are many areas a software engineer can specialize in.</p>
<p>Here are some of the most common specializations in software engineering:</p>
<h3 id="heading-front-end-web-development">Front-end web development</h3>
<p><a target="_blank" href="https://www.freecodecamp.org/news/how-to-become-a-frontend-developer/">Front-end web development</a> is an area that involves designing, building, and testing the User Experience (UX), User Interface (UI), and the parts of websites and web applications users see and interact.</p>
<p>Front-end developers use use languages such as HTML, CSS, JavaScript and their associated frameworks and libraries.</p>
<h3 id="heading-back-end-web-development">Back-end web development</h3>
<p><a target="_blank" href="https://www.freecodecamp.org/news/front-end-developer-vs-back-end-developer-definition-and-meaning-in-practice/">Back-end web development</a>, also known as server-side development, is an area that involves building and maintaining the parts of websites and web applications that users don't see and interact with – essentially, the behind-the-scenes functionality of websites.</p>
<p>It deals with databases, web architecture, servers, user authentication, authorization, handling and processing user requests, Application Programming Interfaces(APIs), and ensuring the appropriate logic is in place for the front-end to have what it needs to perform efficiently.</p>
<h3 id="heading-full-stack-web-development">Full-stack web development</h3>
<p><a target="_blank" href="https://www.freecodecamp.org/news/learn-full-stack-development-html-css-javascript-node-js-mongodb/">Full-stack web development</a> is an area that involves designing, building, testing, and deploying both the front-end and back-end of websites and web applications from start to finish.</p>
<h3 id="heading-mobile-development">Mobile development</h3>
<p><a target="_blank" href="https://www.freecodecamp.org/news/what-is-mobile-app-development/">Mobile development</a> is an area that involves designing and building software applications that work on mobile devices such as smartphones and tablets running iOs and Android operating systems.</p>
<h3 id="heading-game-development">Game development</h3>
<p><a target="_blank" href="https://www.freecodecamp.org/news/what-is-game-development/">Game development</a> is an area that involves designing and building games that run a variety of platforms, such as PCs, game consoles, web browsers, and mobile phones.</p>
<h3 id="heading-desktop-application-development">Desktop application development</h3>
<p><a target="_blank" href="https://www.freecodecamp.org/news/build-a-sudoku-java-desktop-application/">Desktop application development</a> is an area that involves building software applications that run on your local desktop computer and have a graphical user interface (GUI) such as a word processor or image editor.</p>
<h3 id="heading-operating-system-development">Operating system development</h3>
<p><a target="_blank" href="https://www.freecodecamp.org/news/what-is-an-os-operating-system-definition-for-beginners/">Operating system development</a> is an area that involves developing the hardware and platform for other software applications to run on such as Linux, macOS, and Windows.</p>
<h3 id="heading-devops">DevOps</h3>
<p><a target="_blank" href="https://www.freecodecamp.org/news/devops-engineering-course-for-beginners/">DevOps</a> is an area that involves using processes, methodologies, and tools that automate, manage, and improve the infrastructure and operations of software applications throughout the software development life cycle.</p>
<h3 id="heading-cloud-computing">Cloud computing</h3>
<p><a target="_blank" href="https://www.freecodecamp.org/news/what-is-cloud-computing-beginners-guide/">Cloud computing</a> is an area that involves designing and developing cloud-based services to build scalable and reliable systems.</p>
<h3 id="heading-cybersecurity">Cybersecurity</h3>
<p><a target="_blank" href="https://www.freecodecamp.org/news/10-tools-you-should-know-as-a-cybersecurity-engineer/">Cybersecurity</a> is an area that involves ensuring the security of software applications and systems and protecting them from hacking, malware, threats, vulnerabilities, and different types of cybercrimes.</p>
<h2 id="heading-why-should-you-choose-a-career-in-software-engineering-reasons-to-pursue-software-engineering-as-a-career">Why Should You Choose a Career in Software Engineering? Reasons to Pursue Software Engineering as a Career</h2>
<p>You might want to choose a career in software engineering for a few reasons.</p>
<p>First of all, technology evolves at a fast pace and is constantly changing – there are new developments pretty much every other day.</p>
<p>Software engineers constantly learn new things to stay up to date with the latest changes. They are life-long learners.</p>
<p>If you are curious and enjoy learning new skills and ways of doing things and don't like remaining stagnant and doing monotonous tasks, you might enjoy playing around with the new technologies and tools that emerge.</p>
<p>Another indicator that software engineering might be the right career choice is that you enjoy solving difficult problems.</p>
<p>Software engineers are problem solvers. They use logic and creativity to solve the problems of their employer or client.</p>
<p>If you are rational, analytical, and methodical, you might enjoy the process of solving complex logical problems and building useful things.</p>
<p>You might also want to choose a career in software engineering because software engineers are in high demand.</p>
<p>The <a target="_blank" href="https://www.bls.gov/ooh/computer-and-information-technology/software-developers.htm">U.S. Bureau of Labor Statistics</a> predicts a 25% increase in growth for the job outlook until 2031, which is much faster than average.</p>
<p>Many industries need software engineers, including government agencies, nonprofit organizations, startups, consulting firms, educational institutions, finance and healthcare companies, as well as retail and entertainment businesses, to name just a few. Almost all companies are tech companies nowadays.</p>
<p>Software engineers also generally command comfortable salaries.</p>
<p>According to the <a target="_blank" href="https://www.bls.gov/ooh/computer-and-information-technology/software-developers.htm">U.S Bureau of Labor Statistics</a>, the median salary for software engineers is $109,020 per year.</p>
<p>That said, the salary will depend on your location and years of experience.</p>
<p>Google the average salary for software engineers in your location who have the same level of experience as you to get a better idea.</p>
<p>Another reason you might want a career in software engineering is that it allows for remote work, flexibility in where you work from, flexible working hours, and a good work/life balance.</p>
<p>To learn more about finding a remote developer role, give <a target="_blank" href="https://www.freecodecamp.org/news/remote-work-how-to-find-remote-working-jobs-from-home/">this article</a> a read.</p>
<h2 id="heading-how-to-become-a-software-engineer-key-technical-and-soft-skills-for-software-engineers">How to Become a Software Engineer – Key Technical and Soft Skills for Software Engineers</h2>
<p>In the following sections, I will go over how to become a software engineer and list some of the technical and soft skills you will need to learn. I will also provide some resources for you to get started.</p>
<p>Please keep in mind that this is not an exhaustive list of the skills you will need, but serves as some key suggestions of what might be helpful for your learning journey.</p>
<h3 id="heading-choose-your-education-environment-university-degree-vs-bootcamp-vs-self-directed-learning">Choose Your Education Environment – University Degree VS Bootcamp VS Self-Directed Learning</h3>
<p>Software engineering requires a lot of studying and learning.</p>
<p>So, the first step to becoming a software engineer is to think about your education options and what route you want to take.</p>
<h4 id="heading-computer-science-degree">Computer science degree</h4>
<p>One of the options, and the more traditional one for becoming a software engineer, is to obtain a four-year university degree and <a target="_blank" href="https://www.freecodecamp.org/news/what-you-learn-in-a-4-year-computer-science-degree-35a95457cb06/">major in Computer Science</a> or a related math and science degree program.</p>
<p>A <a target="_blank" href="https://www.freecodecamp.org/news/how-to-choose-a-computer-science-degree-program-2c67687bfb2e/">Computer Science degree</a> from an accredited university is highly respected by certain employers and considered an impressive (and for some, still necessary) credential on your résumé.</p>
<p>With that said, getting a degree is an investment and a pricy route to take – university degrees are not cheap.</p>
<p>But according to the <a target="_blank" href="https://survey.stackoverflow.co/2022/#developer-profile-education">Stack Overflow Developer survey for 2022</a>, most developers (87%) have a post-secondary education of some kind, having some college or more.</p>
<p>While <a target="_blank" href="https://www.freecodecamp.org/news/do-you-need-a-computer-science-degree-to-work-in-tech/">you don't necessarily need a degree</a> to become a software engineer, and employers care whether you have the necessary skills and experience, it may be a worthwhile investment and open up more job opportunities.</p>
<h4 id="heading-coding-bootcamp">Coding bootcamp</h4>
<p>Another option is to enroll in a <a target="_blank" href="https://www.freecodecamp.org/news/coding-bootcamp-handbook/">software engineering bootcamp</a>.</p>
<p>Bootcamps are intensive training programs and learning environments that teach you the necessary technical skills to land a software engineering job. The average duration of a bootcamp is around twenty to thirty weeks.</p>
<p>It is typically project-based learning, and by the end of the program, you will have a portfolio of work to showcase to prospective employers.</p>
<p>With that said, it is still an expensive route to take. Some bootcamps charge up to five-figure sums just for a couple of months of training, and it also may not be the right environment for you and your life circumstances.</p>
<h4 id="heading-teach-yourself-to-code">Teach yourself to code</h4>
<p>Another option is to <a target="_blank" href="https://www.freecodecamp.org/news/my-best-tips-for-new-developers-advice-from-a-mostly-self-taught-software-engineer-9cb2f6238177/">learn to code on your own</a> and create your self-directed learning plan using either free or paid resources such as books, video tutorials, and interactive courses.</p>
<p>Learning on your own is a great option if you are busy, have life responsibilities such as taking care of family, or cannot quit your full-time job to pursue a career change.</p>
<p>You can learn at your own time and pace and create your individualized schedule according to the spare time you have.</p>
<p>You can get started with <a target="_blank" href="https://www.freecodecamp.org/">freeCodeCamp's curriculum</a>, which is free and project-based.</p>
<p>In summary, while for many years getting a formal degree has been considered the standard way to work as a software engineer, there are other avenues these days to choose from, such as attending a bootcamp or self-teaching.</p>
<h3 id="heading-learn-how-to-learn-develop-effective-study-habits">Learn How To Learn – Develop Effective Study Habits</h3>
<p>No matter what style of education you choose, you will need to learn how to learn effectively to make the most out of your studies and solidify your learning.</p>
<p>Learning how to learn allows you to <a target="_blank" href="https://ncase.me/remember/">pick up anything faster and make it stick</a>, which will serve you well as a software engineer. The profession requires constant learning on the job.</p>
<p>There are learning techniques that, despite feeling intuitively right, do more harm than good and are ineffective since they require low cognitive effort.</p>
<p>Those ineffective techniques are passively re-reading, taking notes, and highlighting/underlining.</p>
<p>The two most effective and cognitively demanding learning strategies for retaining information are <a target="_blank" href="https://en.wikipedia.org/wiki/Testing_effect">active recall</a> and <a target="_blank" href="https://en.wikipedia.org/wiki/Spaced_repetition">spaced repetition</a>.</p>
<p>Active recall involves quizzing yourself and recalling the main ideas of what you just read/learned.</p>
<p>The brain makes stronger connections when it retrieves information, not when it receives information – the retrieval process enhances deep learning.</p>
<p>Spaced repetition is a practice that flattens the forgetting curve we naturally have as humans. You are bound to forget the things you learn.</p>
<p>Spaced repetition involves frequently reviewing what you learned – this helps build your knowledge over time.</p>
<p>A good tool for practicing spaced repetition is using <a target="_blank" href="https://apps.ankiweb.net/">Anki</a>, a flashcard app with an algorithm behind it that surfaces the content at intervals – just when you are about to forget it.</p>
<p>To learn more about learning and creating effective study habits, check out the <a target="_blank" href="https://www.coursera.org/learn/learning-how-to-learn">Learning how to learn course on Coursera</a>.</p>
<h3 id="heading-learn-a-programming-language">Learn a Programming Language</h3>
<p>As a software engineer, you will need an in-depth understanding of at least one or sometimes two or more programming languages.</p>
<p>You will need to understand concepts such as variables, data types, conditional statements, boolean logic, loops, arrays, operators, and input/output, to name just a few.</p>
<p>There are hundreds of programming languages to choose from. And each one has its specific use cases.</p>
<p>The programming language you choose to learn will depend on the area of software engineering you want to specialize in.</p>
<p>Some of the most popular ones are the following:</p>
<ul>
<li><p><a target="_blank" href="https://www.freecodecamp.org/news/what-is-javascript-javascript-code-explained-in-plain-english/">JavaScript</a>, – a scripting language that runs in a web browser and is used to create both the front-end and back-end of web applications.</p>
</li>
<li><p><a target="_blank" href="https://www.freecodecamp.org/news/what-is-python-used-for-10-coding-uses-for-the-python-programming-language/">Python</a> – a general-purpose server-side language, commonly used for back-end web development.</p>
</li>
<li><p><a target="_blank" href="https://www.freecodecamp.org/news/java-for-backend-web-development/">Java</a>, – an all-purpose language commonly used for developing applications for various platforms, including Internet and Android applications.</p>
</li>
<li><p><a target="_blank" href="https://www.freecodecamp.org/news/how-to-learn-the-c-programming-language/">C++</a> – a popular language for developing operating systems, applications, and games.</p>
</li>
</ul>
<p>To learn more about those languages, check out the following resources to get started:</p>
<ul>
<li><p><a target="_blank" href="https://www.freecodecamp.org/news/full-javascript-course-for-beginners/">Full JavaScript Course for Beginners</a></p>
</li>
<li><p><a target="_blank" href="https://www.freecodecamp.org/news/python-programming-course/">Free Python Programming Course</a></p>
</li>
<li><p><a target="_blank" href="https://www.freecodecamp.org/news/learn-java-free-java-courses-for-beginners/">Learn Java – Free Java Courses for Beginners</a></p>
</li>
<li><p><a target="_blank" href="https://www.freecodecamp.org/news/learn-c-with-free-31-hour-course/">Learn C++ Programming for Beginners – Free 31-Hour Course</a></p>
</li>
</ul>
<h3 id="heading-learn-data-structures-and-algorithms">Learn Data Structures and Algorithms</h3>
<p>Data structures and algorithms are topics that will likely come up often during your software engineering job interviews.</p>
<p>A solid foundation in data structures and algorithms allows you to become better at problem-solving, find the most efficient and practical solutions to problems, and write more scalable and maintainable code.</p>
<p>Data Structures are storage areas for storing and organizing data elements so that computers can perform calculations with those data elements more precisely and efficiently.</p>
<p>An algorithm is a series of well-defined and precise step-by-step instructions. These instructions tell a computer how to perform a particular task that will solve a specific problem.</p>
<p>An algorithm receives some input and produces some output that solves the problem at hand.</p>
<p>To learn more about data structures and algorithms, check out <a target="_blank" href="https://www.freecodecamp.org/news/algorithms-and-data-structures-free-treehouse-course/">this course</a>.</p>
<h3 id="heading-learn-database-architecture-and-sql">Learn Database Architecture and SQL</h3>
<p>Knowing how to store, maintain, manage, and design the architecture of a database will come in useful as a developer. You'll need to know how to perform database operations, namely CRUD operations (which stands for Create, Read, Update, Delete), and write basic SQL queries. After all, complex and large-scale software applications need to handle large amounts of data.</p>
<p>You may want to learn about the different types of databases, such as the differences between relational databases and non-relational (also known as NoSQL databases).</p>
<p>And you may want to study SQL as well. SQL, which stands for Structured Query Language, is a query language for querying and manipulating data in relational databases.</p>
<p>To learn more about handling databases and writing SQL queries, check out the <a target="_blank" href="https://www.freecodecamp.org/learn/relational-database/">relational database certification</a> by freeCodeCamp.</p>
<h3 id="heading-learn-how-to-use-software-engineering-tools">Learn How to Use Software Engineering Tools</h3>
<p>As a software engineer, you'll use various tools in your day-to-day work.</p>
<p>Some of the ones you will use the most are the following:</p>
<ul>
<li><p><a target="_blank" href="https://www.freecodecamp.org/news/git-for-professionals/">Git</a> is a version control system and collaborative tool. With Git, you can work with other developers on the same project and track changes in the code.</p>
</li>
<li><p><a target="_blank" href="https://www.freecodecamp.org/news/git-and-github-crash-course/">GitHub</a> is a popular hosting service that allows you to share your code with the world.</p>
</li>
<li><p>The <a target="_blank" href="https://www.freecodecamp.org/news/command-line-for-beginners/">command line</a> is an application that allows you to interact with your underlying operating system and your filesystem using text-based commands.</p>
</li>
<li><p>An <a target="_blank" href="https://www.freecodecamp.org/news/what-is-an-ide-for-beginners/">IDE (or Integrated Development Environment)</a> is a software application that allows you to write code efficiently and productively. Specifically, it is where you write, edit, run, test, and debug your code. IDEs offer features like syntax highlighting that make code easier to read and catch errors and keyboard shortcuts that save time.</p>
</li>
</ul>
<h3 id="heading-build-your-non-technical-skills">Build Your Non-Technical Skills</h3>
<p>Although developing your technical skills is necessary to become a professional software engineer, you cannot neglect your non-technical (often referred to as soft) skills.</p>
<p>The stereotype of software engineers working in isolation, alone in a room, and not talking to anyone is far from reality.</p>
<p>As a software engineer, you will typically be collaborating with others and be a part of a team. You need strong social and interpersonal skills to be a valuable team member.</p>
<p><a target="_blank" href="https://www.freecodecamp.org/news/how-to-cultivate-great-communication-skills-as-a-dev-and-kick-bad-habits-to-the-curb-d62a075700f5/">Effectively communicating</a> your ideas in a way that is clear and easy to understand is necessary, as you will often work with other team members who won't have the same level of technical knowledge as you.</p>
<p>And as you will build software applications for a diverse audience, you need to empathize with your end users and understand the kind of problems they may face. Having <a target="_blank" href="https://www.freecodecamp.org/news/how-empathy-makes-you-a-better-software-engineer/">empathy</a> is about putting yourself in other people's shoes and being able to see other people's perspectives. This helps you become a more effective problem-solver and developer.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Hopefully, you found this article helpful and have a better understanding of what software engineering entails.</p>
<p>In this article, we went over the tasks and responsibilities of software engineers and some of the reasons why this career might be a good fit for you. You also saw some of the skills you will need to develop to become a software engineer.</p>
<p>Thank you for reading!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ JS Sum of an Array – How to Add the Numbers in a JavaScript Array ]]>
                </title>
                <description>
                    <![CDATA[ An array in JavaScript is an object that allows you to store an ordered collection of multiple values under a single variable name and manipulate those values in numerous ways. In this article, you will learn how to calculate the sum of all the numbe... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-add-numbers-in-javascript-arrays/</link>
                <guid isPermaLink="false">66b1e40c0968943127cc5f01</guid>
                
                    <category>
                        <![CDATA[ arrays ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Dionysia Lemonaki ]]>
                </dc:creator>
                <pubDate>Fri, 31 Mar 2023 17:45:12 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/03/pexels-chelsey-horne-4506938.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>An array in JavaScript is an object that allows you to store an ordered collection of multiple values under a single variable name and manipulate those values in numerous ways.</p>
<p>In this article, you will learn how to calculate the sum of all the numbers in a given array using a few different approaches.</p>
<p>Specifically, I will show you how to find the sum of all numbers in an array by using:</p>
<ul>
<li>A <code>for</code> loop</li>
<li>The <code>forEach()</code> method</li>
<li>The <code>reduce()</code> method</li>
</ul>
<p>Let's get started!</p>
<h2 id="heading-how-to-calculate-the-sum-of-an-array-using-a-for-loop-in-javascript">How to Calculate the Sum of an Array Using A <code>for</code> Loop in JavaScript</h2>
<p>One of the simplest ways of calculating the sum of all numbers in an array is using a <code>for</code> loop. It performs an iteration <code>n</code> number of times.</p>
<p>Let's look at the following example:</p>
<pre><code class="lang-js"><span class="hljs-comment">// create an array</span>
<span class="hljs-keyword">let</span> myNums = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>];

<span class="hljs-comment">// create a variable for the sum and initialize it</span>
<span class="hljs-keyword">let</span> sum = <span class="hljs-number">0</span>;

<span class="hljs-comment">// iterate over each item in the array</span>
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; myNums.length; i++ ) {
  sum += myNums[i];
}

<span class="hljs-built_in">console</span>.log(sum) <span class="hljs-comment">// 15</span>
</code></pre>
<p>In the example above, I first created an array named <code>myNums</code> that stores five values.</p>
<p>I then declared a variable named <code>sum</code> and initialized it with a value of <code>0</code> – this variable will store the result from the calculations in the <code>for</code> loop.</p>
<p>Next, I used the <code>for</code> loop to iterate over all the elements until the end of the <code>myNums</code> array.</p>
<p>I also reassigned the value of the <code>sum</code> variable using the addition assignment operator by adding its current value and the current array element.</p>
<p>To learn more about the <code>for</code> loop in JavaScript, give <a target="_blank" href="https://www.freecodecamp.org/news/javascript-for-loops/">this article</a> a read.</p>
<h2 id="heading-how-to-calculate-the-sum-of-an-array-using-the-foreach-method-in-javascript">How to Calculate the Sum of an Array Using the <code>forEach()</code> Method in JavaScript</h2>
<p>Another way to calculate the sum of an array is using JavaScript's built-in <code>forEach()</code> method. It iterates over an array and calls a function for each item. </p>
<p>Let's look at the following example:</p>
<pre><code class="lang-js"><span class="hljs-comment">// create an array</span>
<span class="hljs-keyword">const</span> myNums = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>];

<span class="hljs-comment">// create a variable for the sum and initialize it</span>
<span class="hljs-keyword">let</span> sum = <span class="hljs-number">0</span>;

<span class="hljs-comment">// calculate sum using forEach() method</span>
myNums.forEach( <span class="hljs-function"><span class="hljs-params">num</span> =&gt;</span> {
  sum += num;
})

<span class="hljs-built_in">console</span>.log(sum) <span class="hljs-comment">// 15</span>
</code></pre>
<p>In the example above, I created an array <code>myNums</code>. I also declared a variable named <code>sum</code> and initialized it with a value of <code>0</code>.</p>
<p>Then, I used the <code>forEach()</code> method to iterate over each item in the array.</p>
<p>On each iteration, I reassigned the value of the <code>sum</code> variable by adding its current value and the value of the current array element in each iteration.</p>
<p>To learn more about the <code>forEach()</code> function, give <a target="_blank" href="https://www.freecodecamp.org/news/javascript-foreach-how-to-loop-through-an-array-in-js/">this article</a> a read.</p>
<h2 id="heading-how-to-calculate-the-sum-of-an-array-using-the-reduce-method-in-javascript">How to Calculate the Sum of an Array Using the <code>reduce()</code> Method in JavaScript</h2>
<p>Another way of calculating the sum of an array is using the <code>reduce()</code> method, which got introduced with ES6.</p>
<p>The <code>reduce()</code> method reduces all elements in an array into a single value.</p>
<p>Let's look at the following example:</p>
<pre><code class="lang-js"><span class="hljs-comment">// create an array</span>
<span class="hljs-keyword">const</span> myNums = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>];

<span class="hljs-comment">// use reduce() method to find the sum</span>
<span class="hljs-keyword">var</span> sum = myNums.reduce(<span class="hljs-function">(<span class="hljs-params">accumulator, currentValue</span>) =&gt;</span> {
  <span class="hljs-keyword">return</span> accumulator + currentValue
},<span class="hljs-number">0</span>);

<span class="hljs-built_in">console</span>.log(sum) <span class="hljs-comment">// 15</span>
</code></pre>
<p>The <code>reduce()</code> method takes a user-defined callback function as its first required argument. The function gets called on each element in the array.</p>
<p>The callback function accepts the following two required parameters:</p>
<ul>
<li>The <code>accumulator</code>, which is the variable that stores the last returned value from the previous function call.</li>
<li>The <code>currentValue</code>, which represents the current array item.</li>
</ul>
<p>The second argument to the <code>reduce()</code> method is the <code>initialValue</code>, which is <code>0</code>. The <code>initialValue</code> represents the initial value of the <code>accumulator</code>.</p>
<p>To learn more about the <code>reduce()</code> method, give <a target="_blank" href="https://www.freecodecamp.org/news/reduce-f47a7da511a9/">this article</a> a read.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>And there you have it! You have learned three ways to calculate the sum of an array in JavaScript.</p>
<p>To learn more about JavaScript, check out <a target="_blank" href="https://www.freecodecamp.org/news/full-javascript-course-for-beginners/">this Full JavaScript Course for Beginners</a>.</p>
<p>Thanks for reading, and happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Python Get Current Directory – Print Working Directory PWD Equivalent ]]>
                </title>
                <description>
                    <![CDATA[ In this article, you will learn how to get the current working directory (another name for folder) in Python, which is the equivalent of using the pwd command. There are a couple of ways to get the current working directory in Python: By using the o... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/python-get-current-directory/</link>
                <guid isPermaLink="false">66b1e47f0968943127cc5f1c</guid>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Dionysia Lemonaki ]]>
                </dc:creator>
                <pubDate>Tue, 28 Mar 2023 16:27:30 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/03/pexels-pixabay-357514.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In this article, you will learn how to get the current working directory (another name for folder) in Python, which is the equivalent of using the <code>pwd</code> command.</p>
<p>There are a couple of ways to get the current working directory in Python:</p>
<ul>
<li>By using the <code>os</code> module and the <code>os.getcwd()</code> method.</li>
<li>By using the <code>pathlib</code> module and the <code>Path.cwd()</code> method.</li>
</ul>
<p>Let's get started!</p>
<h2 id="heading-how-to-get-the-current-directory-using-the-osgetcwd-method-in-python">How to Get The Current Directory Using the <code>os.getcwd()</code> Method in Python</h2>
<p>The <code>os</code> module, which is part of the standard Python library (also known as stdlib), allows you to access and interact with your operating system.</p>
<p>To use the <code>os</code> module in your project, you need to include the following line at the top of your Python file:</p>
<pre><code><span class="hljs-keyword">import</span> os
</code></pre><p>Once you have imported the <code>os</code> module, you have access to the <code>os.getcwd()</code> method, which allows you to get the full path of the current working directory. </p>
<p>Let's look at the following example:</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> os

<span class="hljs-comment"># get the current working directory</span>
current_working_directory = os.getcwd()

<span class="hljs-comment"># print output to the console</span>
print(current_working_directory)

<span class="hljs-comment"># output will look something similar to this on a macOS system</span>
<span class="hljs-comment"># /Users/dionysialemonaki/Documents/my-projects/python-project</span>
</code></pre>
<p>The output is a string that contains the absolute path to the current working directory – in this case, <code>python-project</code>. </p>
<p>To check the data type of the output, use the <code>type()</code> function like so:</p>
<pre><code class="lang-python">print(type(current_working_directory))

<span class="hljs-comment"># output</span>

<span class="hljs-comment"># &lt;class 'str'&gt;</span>
</code></pre>
<p>Note that the current working directory doesn't have a trailing forward slash, <code>/</code>.</p>
<p>Keep in mind also that output will vary depending on the directory you are running the Python script from as well as your Operating System.</p>
<h2 id="heading-how-to-get-the-current-directory-using-the-pathcwd-method-in-python">How to Get The Current Directory Using the <code>Path.cwd()</code> Method in Python</h2>
<p>In the previous section, you saw how to use the <code>os</code> module to get the current working directory. However, you can use the <code>pathlib</code> module to achieve the same result.</p>
<p>The <code>pathlib</code> module was introduced in the standard library in Python's 3.4 version and offers an object-oriented way to work with filesystem paths and handle files.</p>
<p>To use the <code>pathlib</code> module, you first need to import it at the top of your Python file:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> pathlib <span class="hljs-keyword">import</span> Path
</code></pre>
<p>Once you have imported the <code>pathlib</code> module, you can use the <code>Path.cwd()</code> class method, which allows you to get the current working directory.</p>
<p>Let's look at the following example:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> pathlib <span class="hljs-keyword">import</span> Path

<span class="hljs-comment"># get the current working directory</span>
current_working_directory = Path.cwd()

<span class="hljs-comment"># print output to the console</span>
print(current_working_directory)

<span class="hljs-comment"># output will look something similar to this on a macOS system</span>
<span class="hljs-comment"># /Users/dionysialemonaki/Documents/my-projects/python-project</span>
</code></pre>
<p>As you can see, the output is the same as the output I got when I used the <code>os.getcwd()</code> method. The only difference is the data type of the output:</p>
<pre><code>print(type(current_working_directory))

# output

# &lt;<span class="hljs-class"><span class="hljs-keyword">class</span> '<span class="hljs-title">pathlib</span>.<span class="hljs-title">PosixPath</span>'&gt;</span>
</code></pre><h2 id="heading-conclusion">Conclusion</h2>
<p>And there you have it! You now know how to get the full path to the current directory in Python using both the <code>os</code> and <code>pathlib</code> modules.</p>
<p>To learn more about Python, check out <a target="_blank" href="https://www.freecodecamp.org/news/python-programming-course/">freeCodeCamp's Python for beginners course</a>.</p>
<p>Thanks for reading, and happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Creating a Directory in Python – How to Create a Folder ]]>
                </title>
                <description>
                    <![CDATA[ In this article, you will learn how to create new directories (which is another name for folders) in Python.  You will also learn how to create a nested directory structure.  To work with directories in Python, you first need to include the os  modul... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/creating-a-directory-in-python-how-to-create-a-folder/</link>
                <guid isPermaLink="false">66b1e3e16f537a6f7e7212c4</guid>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Dionysia Lemonaki ]]>
                </dc:creator>
                <pubDate>Thu, 23 Mar 2023 13:39:03 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/03/pexels-pixabay-51191.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In this article, you will learn how to create new directories (which is another name for folders) in Python. </p>
<p>You will also learn how to create a nested directory structure. </p>
<p>To work with directories in Python, you first need to include the <code>os</code>  module in your project, which allows you to interact with your operating system.</p>
<p>The <code>os</code> module also lets you use the two methods we will cover in this article:</p>
<ul>
<li>the <code>os.mkdir()</code> method</li>
<li>the <code>os.makedirs()</code> method</li>
</ul>
<p>Let’s get into it!</p>
<h2 id="heading-how-to-create-a-single-directory-using-the-osmkdir-method-in-python">How To Create A Single Directory Using The <code>os.mkdir()</code> Method in Python</h2>
<p>As mentioned earlier, to work with directories in Python, you first need to include the <code>os</code> module.</p>
<p>To do so, add the following line of code to the top of your file:</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> os
</code></pre>
<p>The code above will allow you to use the <code>os.mkdir()</code> method to create a new single directory.</p>
<p>The <code>os.mkdir()</code> method accepts one argument – the path for the directory.</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> os

<span class="hljs-comment"># specify the path for the directory – make sure to surround it with quotation marks</span>
path = <span class="hljs-string">'./projects'</span>

<span class="hljs-comment"># create new single directory</span>
os.mkdir(path)
</code></pre>
<p>The code above will create a  <code>projects</code> directory in the current working directory.</p>
<p>Note that the <code>./</code> stands for the current working directory. You can omit this part and only write <code>projects</code> when specifying the path – the result will be the same!</p>
<h3 id="heading-how-to-handle-exceptions-when-using-the-osmkdir-method-in-python">How to Handle Exceptions When Using the <code>os.mkdir</code> Method in Python</h3>
<p>But what happens when the directory you are trying to create already exists?  A <code>FileExistsError</code> exception is raised:</p>
<pre><code>Traceback (most recent call last):
  File <span class="hljs-string">"main.py"</span>, line <span class="hljs-number">3</span>, <span class="hljs-keyword">in</span> &lt;<span class="hljs-built_in">module</span>&gt;
    os.mkdir(path)
<span class="hljs-attr">FileExistsError</span>: [Errno <span class="hljs-number">17</span>] File exists: <span class="hljs-string">'./projects'</span>
</code></pre><p>One way to handle this exception is to check if the file already exists using an <code>if..else</code> block:</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> os

path = <span class="hljs-string">'./projects'</span>

<span class="hljs-comment"># check whether directory already exists</span>
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> os.path.exists(path):
  os.mkdir(path)
  print(<span class="hljs-string">"Folder %s created!"</span> % path)
<span class="hljs-keyword">else</span>:
  print(<span class="hljs-string">"Folder %s already exists"</span> % path)
</code></pre>
<p>In the example above, I first checked whether the <code>./projects</code> directory already exists using the <code>os.path.exists()</code> method. </p>
<p>If it does, I will get the following output instead of a <code>FileExistsError</code>:</p>
<pre><code>Folder ./projects already exists
</code></pre><p>If the file doesn't exist, then a new <code>projects</code> folder gets created in the current working directory, and I get the following output:</p>
<pre><code>Folder ./projects created!
</code></pre><p>Alternatively, you can use a <code>try/except</code> block to handle exceptions:</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> os

path = <span class="hljs-string">'./projects'</span>

<span class="hljs-keyword">try</span>:
    os.mkdir(path)
    print(<span class="hljs-string">"Folder %s created!"</span> % path)
<span class="hljs-keyword">except</span> FileExistsError:
    print(<span class="hljs-string">"Folder %s already exists"</span> % path)
</code></pre>
<p>If a <code>projects</code> folder already exists in the current working directory, you will get the following output instead of an error message:</p>
<pre><code>Folder ./projects already exists
</code></pre><h2 id="heading-how-to-create-a-directory-with-subdirectories-using-the-osmakedirs-method-in-python">How To Create A Directory With Subdirectories Using The <code>os.makedirs()</code> Method in Python</h2>
<p>The <code>os.mkdir()</code> method does not let you create a subdirectory. Instead, it lets you create a single directory.</p>
<p>To create a nested directory structure (such as a directory inside another directory), use the <code>os.makedirs()</code> method.</p>
<p>The <code>os.makedirs()</code> accepts one argument – the entire folder path you want to create.</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> os

<span class="hljs-comment"># define the name of the directory with its subdirectories</span>
path = <span class="hljs-string">'./projects/games/game01'</span>

os.makedirs(path)
</code></pre>
<p>In the example above, I created a <code>projects</code> directory in the current working directory.</p>
<p>Inside projects, I created another directory, <code>games</code>. And inside <code>games</code>, I created yet another directory, <code>games01</code>.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>And there you have it! You now know how to create a single directory and a directory with subdirectories in Python.</p>
<p>To learn more about Python, check out freeCodeCamp's <a target="_blank" href="https://www.freecodecamp.org/news/python-programming-course/">Python for beginners course</a>.</p>
<p>Thanks for reading, and happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Python range() Function Example ]]>
                </title>
                <description>
                    <![CDATA[ In this article, you will learn how to use the range() function in Python with the help of code examples along the way. What is the range() Function in Python? range() Function Syntax Breakdown Python's built-in range() function is mainly used when w... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/python-range-function-example/</link>
                <guid isPermaLink="false">66b1e4909498308aedfb576b</guid>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Dionysia Lemonaki ]]>
                </dc:creator>
                <pubDate>Fri, 17 Mar 2023 11:55:33 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/03/pexels-christina-morillo-1181671.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In this article, you will learn how to use the <code>range()</code> function in Python with the help of code examples along the way.</p>
<h2 id="heading-what-is-the-range-function-in-python-range-function-syntax-breakdown">What is the <code>range()</code> Function in Python? <code>range()</code> Function Syntax Breakdown</h2>
<p>Python's built-in <code>range()</code> function is mainly used when working with <code>for</code> loops – you can use it to loop through certain blocks of code a specified number of times.</p>
<p>The <code>range()</code> function accepts three arguments – one is required, and two are optional.</p>
<p>By default, the syntax for the <code>range()</code> function looks similar to the following:</p>
<pre><code>range(stop)
</code></pre><p>The <code>stop</code> argument is <strong>required</strong>. </p>
<p>The <code>range()</code> function returns a sequence of numbers starting from <code>0</code>, incrementing by <code>1</code>, and ending at the value you specify as <code>stop</code> (non-inclusive). </p>
<p>But what if you want to iterate through a range of two numbers you specify and don't want to start the counting from <code>0</code>?</p>
<p>You can pass a second <strong>optional</strong> start argument, <code>start</code>, to specify the starting number. The syntax to do so looks like this:</p>
<pre><code>range(start, stop)
</code></pre><p>This syntax generates a sequence of numbers based on the <code>start</code> (inclusive) and <code>stop</code> (non-inclusive) values that increment by <code>1</code>.</p>
<p>Lastly, if you don't want the default increment to be <code>1</code>, you can specify a third <strong>optional</strong> argument, <code>step</code>. The syntax to do that looks like this:</p>
<pre><code>range(start, stop, step)
</code></pre><p>This syntax generates a sequence of numbers that starts counting at <code>start</code> (inclusive) and increments according to <code>step</code> until it reaches <code>stop</code> (non-inclusive).</p>
<h2 id="heading-how-to-use-the-range-function-with-only-the-stop-argument">How to Use the <code>range()</code> Function with Only the <code>stop</code> Argument</h2>
<p>When using only the <code>stop</code> argument with <code>range()</code>, the counting starts at <code>0</code> and increments by <code>1</code>.  The counting stops when you reach the value you specify as <code>stop</code>. </p>
<p>Keep in mind that the <code>stop</code> value you specify is not inclusive!</p>
<p>If you specify a <code>stop</code> argument of <code>5</code>, the range includes the numbers <code>0 - 4</code>  and not <code>0 - 5</code> – the counting will stop at <code>4</code> and not <code>5</code>.</p>
<p>Let's take a look at the example below:</p>
<pre><code class="lang-python"><span class="hljs-keyword">for</span> num <span class="hljs-keyword">in</span> range(<span class="hljs-number">5</span>):
    print(num)

<span class="hljs-comment"># output </span>

<span class="hljs-comment"># 0</span>
<span class="hljs-comment"># 1</span>
<span class="hljs-comment"># 2</span>
<span class="hljs-comment"># 3</span>
<span class="hljs-comment"># 4</span>
</code></pre>
<p>In this example, I specified a <code>range(5)</code>.</p>
<p>The function started counting from <code>0</code>, incremented by <code>1</code> on each iteration and ended at <code>4</code>.</p>
<h2 id="heading-how-to-use-the-range-function-with-the-start-and-stop-arguments">How to Use the <code>range()</code> Function with the <code>start</code> And <code>stop</code> Arguments</h2>
<p>If you want to have a range of two numbers, you use two arguments – <code>start</code> and <code>stop</code>. Keep in mind that the <code>start</code> value is inclusive, whereas the <code>stop</code> value is not.</p>
<p>If you want a range of values from 5 inclusive to 10 inclusive, you write a <code>range(5,11)</code> like so:</p>
<pre><code class="lang-python"><span class="hljs-keyword">for</span> num <span class="hljs-keyword">in</span> range(<span class="hljs-number">5</span>,<span class="hljs-number">11</span>):
  print(num)

<span class="hljs-comment"># output</span>

<span class="hljs-comment"># 5</span>
<span class="hljs-comment"># 6</span>
<span class="hljs-comment"># 7</span>
<span class="hljs-comment"># 8</span>
<span class="hljs-comment"># 9</span>
<span class="hljs-comment"># 10</span>
</code></pre>
<p>You can pass negative integer values to <code>range()</code> as well:</p>
<pre><code class="lang-python"><span class="hljs-keyword">for</span> num <span class="hljs-keyword">in</span> range(<span class="hljs-number">-5</span>, <span class="hljs-number">1</span>):
  print(num)

<span class="hljs-comment"># output</span>

<span class="hljs-comment"># -5</span>
<span class="hljs-comment"># -4</span>
<span class="hljs-comment"># -3</span>
<span class="hljs-comment"># -2</span>
<span class="hljs-comment"># -1</span>
<span class="hljs-comment"># 0</span>
</code></pre>
<p>Something to note here is that you cannot pass float values to <code>range()</code>.</p>
<p>In this example, when I pass two float values as arguments, an error gets raised:</p>
<pre><code class="lang-python"><span class="hljs-keyword">for</span> num <span class="hljs-keyword">in</span> range(<span class="hljs-number">5.2</span>, <span class="hljs-number">4.3</span>):
  print(num)

<span class="hljs-comment"># output</span>

<span class="hljs-comment"># Traceback (most recent call last):</span>
<span class="hljs-comment">#  File "main.py", line 1, in &lt;module&gt;</span>
<span class="hljs-comment">#    for num in range(5.2, 4.3):</span>
<span class="hljs-comment"># TypeError: 'float' object cannot be interpreted as an integer</span>
</code></pre>
<p>You can pass either negative or positive integers as <code>start</code> and <code>stop</code> arguments.</p>
<h2 id="heading-how-to-use-the-range-function-with-the-start-stop-and-step-arguments">How to Use the <code>range()</code> Function with the <code>start</code>, <code>stop</code>, And <code>step</code> Arguments</h2>
<p>By default, the increment value is <code>1</code> and is not specified. That said, you can change it by passing a <code>step</code> argument to the <code>range()</code> function.</p>
<p>Let's take a look at the following example:</p>
<pre><code class="lang-python"><span class="hljs-keyword">for</span> num <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>,<span class="hljs-number">21</span>,<span class="hljs-number">2</span>):
  print(num)

<span class="hljs-comment"># output</span>

<span class="hljs-comment"># 10</span>
<span class="hljs-comment"># 12</span>
<span class="hljs-comment"># 14</span>
<span class="hljs-comment"># 16</span>
<span class="hljs-comment"># 18</span>
<span class="hljs-comment"># 20</span>
</code></pre>
<p>In the example above, I generated a sequence of numbers from <code>10</code> to <code>20</code> and incremented the steps by <code>2</code>. I achieved this by specifying a step value of <code>2</code>.</p>
<p>Something to note is that <code>step</code> can be either a negative or positive number, but it cannot be <code>0</code>.</p>
<p>Here is how you can generate a range with a negative <code>step</code> argument:</p>
<pre><code class="lang-python"><span class="hljs-keyword">for</span> num <span class="hljs-keyword">in</span> range(<span class="hljs-number">20</span>, <span class="hljs-number">11</span>, <span class="hljs-number">-2</span>):
  print(num)

<span class="hljs-comment"># output</span>

<span class="hljs-comment"># 20</span>
<span class="hljs-comment"># 18</span>
<span class="hljs-comment"># 16</span>
<span class="hljs-comment"># 14</span>
<span class="hljs-comment"># 12</span>
</code></pre>
<p>The code above generates a sequence of numbers in reverse.</p>
<p>And look at what happens when the <code>step</code> is <code>0</code>:</p>
<pre><code class="lang-python"><span class="hljs-keyword">for</span> num <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>, <span class="hljs-number">21</span> <span class="hljs-number">0</span>):
  print(num)

<span class="hljs-comment"># output</span>

<span class="hljs-comment">#  File "main.py", line 1</span>
<span class="hljs-comment">#    for num in range(10, 21 0):</span>
                            ^
<span class="hljs-comment"># SyntaxError: invalid syntax</span>
</code></pre>
<h2 id="heading-how-to-create-a-list-of-numbers-using-the-range-function">How to Create A List of Numbers Using the <code>range()</code> Function</h2>
<p>You can create a list of numbers by passing the <code>range()</code> function as an argument to the <code>list()</code> constructor like so:</p>
<pre><code class="lang-python">my_numbers_list = list(range(<span class="hljs-number">5</span>))

print(my_numbers_list)

<span class="hljs-comment"># output</span>

<span class="hljs-comment"># [0, 1, 2, 3, 4]</span>
</code></pre>
<p>In the example above, I created a list of numbers from <code>0</code> to <code>4</code>.</p>
<h2 id="heading-how-to-use-the-len-function-with-range-in-python">How to use The <code>len()</code> Function with <code>range()</code> in Python</h2>
<p>Say you have a list of items and want to do something to the items depending on how long the list is.</p>
<p>For that, you could use <code>range()</code> and pass the length of your list as an argument to the function.</p>
<p>To calculate the length of a list, use the <code>len()</code> function.</p>
<pre><code class="lang-python">programming_languages = [<span class="hljs-string">"Python"</span>, <span class="hljs-string">"JavaScript"</span>, <span class="hljs-string">"Java"</span>, <span class="hljs-string">"C++"</span>]

programming_languages_length = len(programming_languages)

<span class="hljs-keyword">for</span> languages <span class="hljs-keyword">in</span> range(programming_languages_length):
  print(<span class="hljs-string">"Hello World"</span>)

<span class="hljs-comment"># output</span>

<span class="hljs-comment"># Hello World</span>
<span class="hljs-comment"># Hello World</span>
<span class="hljs-comment"># Hello World</span>
<span class="hljs-comment"># Hello World</span>
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>And there you have it! You now know how to use the <code>range()</code> function in Python.</p>
<p>To learn more about Python, check out freeCodeCamp's <a target="_blank" href="https://www.freecodecamp.org/news/python-programming-course/">Python for beginners course</a>.</p>
<p>Thanks for reading, and happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Integer Array in C – How to Declare Int Arrays with C Programming ]]>
                </title>
                <description>
                    <![CDATA[ In this article, you will learn how to work with arrays in C. I will first explain how to declare and initialize arrays.  Then, I will also go over how to access and change items in an array in C with the help of code examples along the way. ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-declare-integer-arrays-with-c-programming/</link>
                <guid isPermaLink="false">66b1e41941fdb67461b8526b</guid>
                
                    <category>
                        <![CDATA[ c programming ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Dionysia Lemonaki ]]>
                </dc:creator>
                <pubDate>Mon, 13 Mar 2023 17:22:07 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/03/nick-hillier-yD5rv8_WzxA-unsplash.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In this article, you will learn how to work with arrays in C.</p>
<p>I will first explain how to declare and initialize arrays. </p>
<p>Then, I will also go over how to access and change items in an array in C with the help of code examples along the way.</p>
<p>Let's get into it!</p>
<h2 id="heading-what-is-an-array-in-c-programming">What Is An Array in C Programming?</h2>
<p>An array is a data structure that stores multiple values in a single variable and in a sequential order that is easily accessible.</p>
<p>Arrays in C are a collection of values that store items of the same data type – an integer array holds only elements of the type <code>int</code>, a float array holds only elements of the type <code>float</code>, and so on.</p>
<h2 id="heading-how-to-declare-an-integer-array-in-c-programming">How to Declare an Integer Array in C Programming</h2>
<p>The general syntax for declaring an array in C looks as you can see it in the code snippet below:</p>
<pre><code>data_type array_name[array_size];
</code></pre><p>Let's take the following example:</p>
<pre><code class="lang-c"><span class="hljs-keyword">int</span> my_numbers[<span class="hljs-number">5</span>];
</code></pre>
<p>Let's break it down:</p>
<ul>
<li>I first defined the data type of the array, <code>int</code>.</li>
<li>I then specified the name, <code>my_numbers</code>, followed by a pair of opening and closing square brackets,<code>[]</code>.</li>
<li>Inside the square brackets, I defined the size (<code>5</code>), meaning the array can hold <code>5</code> integer values.</li>
<li>Finally, I ended the statement with a semicolon (<code>;</code>).</li>
</ul>
<p>Once you have set the array type and size during declaration, the array can't hold items of another type.</p>
<p>The array is also fixed in size, meaning you cannot add or remove items.</p>
<h2 id="heading-how-to-initialize-an-integer-array-in-c-programming">How to Initialize an Integer Array in C Programming</h2>
<p>There are a couple of ways you can initialize an integer array in C.</p>
<p>The first way is to initialize the array during declaration and insert the values inside a pair of opening and closing curly braces, <code>{}</code>. </p>
<p>The general syntax to do that looks like this:</p>
<pre><code>data_type array_name[array_size] = {value1, value2, value3, ...};
</code></pre><p>Let's take the array I declared in the previous section that can hold five integers and initialize it with some values:</p>
<pre><code class="lang-c"><span class="hljs-keyword">int</span> my_numbers[<span class="hljs-number">5</span>] = {<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>, <span class="hljs-number">50</span>};
</code></pre>
<p>In the example above, I placed five comma-separated values inside curly braces, and assigned those values to <code>my_numbers</code> through the assignment operator (<code>=</code>).</p>
<p>Something to note here is that when you specify the size of the array, you can assign less number of elements, like so:</p>
<pre><code class="lang-c"><span class="hljs-keyword">int</span> my_numbers[<span class="hljs-number">5</span>] = {<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>};
</code></pre>
<p>Although the size of the array is <code>5</code>,  I only placed three values inside it. </p>
<p>The array can hold two more items, and those remaining two positions have a default value of <code>0</code>.</p>
<p>Another way to initialize an array is to not specify the size, like so:</p>
<pre><code class="lang-c"><span class="hljs-keyword">int</span> my_numbers[] = {<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>, <span class="hljs-number">50</span>};
</code></pre>
<p>Even though I did not set the size of the array, the compiler knows its size because it knows the number of items stored inside it.</p>
<h2 id="heading-how-to-access-items-in-an-integer-array-in-c-programming">How to Access Items in an Integer Array in C Programming</h2>
<p>To access an element in an array, you have to specify the index of the element in square brackets after the array name.</p>
<p>The syntax to access an element looks like this:</p>
<pre><code>array_name[element_index]
</code></pre><p>In C and programming in general, an array index always starts at <code>0</code>, becuase in computer science, counting starts from <code>0</code>.</p>
<p>So, the first item in an array has an index of <code>0</code>, the second item has an index of <code>1</code>, the third item has an index of <code>2</code>, and so on.</p>
<p>Taking the same array from the previous section, here is how you would access the first element, that is, <code>10</code>:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
  <span class="hljs-keyword">int</span> my_numbers[] = {<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>, <span class="hljs-number">50</span>};
  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d\n"</span>,my_numbers[<span class="hljs-number">0</span>]);
  <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Keep in mind that the last element in an array has an index of <code>array_size -1</code> – it is always one less than the size of the array. So, in an array that holds five elements, the index of the last element is <code>4</code>.</p>
<p>If in an array of five items, you try to access the last element by using an index of <code>5</code>, the program will run, but the element is not available, and you will get undefined behavior:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
  <span class="hljs-keyword">int</span> my_numbers[] = {<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>, <span class="hljs-number">50</span>};
  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d\n"</span>,my_numbers[<span class="hljs-number">5</span>]);
  <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}

<span class="hljs-comment">// output</span>
<span class="hljs-comment">// -463152408</span>
</code></pre>
<h2 id="heading-how-to-change-items-in-an-integer-array-in-c-programming">How to Change Items in an Integer Array in C Programming</h2>
<p>To change the value of a specific element, specify its index number and, with the assignment operator, <code>=</code>, assign a new value:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
  <span class="hljs-keyword">int</span> my_numbers[] = {<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>, <span class="hljs-number">50</span>};
  my_numbers[<span class="hljs-number">0</span>] = <span class="hljs-number">11</span>;
  <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>In the example above, I changed the first item in the array from <code>10</code> to <code>11</code>.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>And there you have it! You now know the basics of working with arrays in C.</p>
<p>To learn more about C, give this <a target="_blank" href="https://www.freecodecamp.org/news/the-c-beginners-handbook/">C beginner's handbook</a> a read to become familiar with the basics of the language.</p>
<p>Thanks for reading, and happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
