<?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[ c programming - 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[ c programming - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Thu, 14 May 2026 04:33:08 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/c-programming/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ C Game Development with Raylib ]]>
                </title>
                <description>
                    <![CDATA[ Making games is one of the best ways to learn programming. It pushes you to think logically, stay organized, and solve real problems. Plus, it’s just fun. If you're looking to improve your C programming skills and learn how to create a complete game ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/c-game-development-with-raylib/</link>
                <guid isPermaLink="false">68408c368ab45ab43dad48c6</guid>
                
                    <category>
                        <![CDATA[ c programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ youtube ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Beau Carnes ]]>
                </dc:creator>
                <pubDate>Wed, 04 Jun 2025 18:11:02 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1749060645649/662c81ad-c0c8-4456-83dc-935a4b6d4cf9.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Making games is one of the best ways to learn programming. It pushes you to think logically, stay organized, and solve real problems. Plus, it’s just fun. If you're looking to improve your C programming skills and learn how to create a complete game from scratch, this new freeCodeCamp course is a great place to start. You’ll learn practical game dev techniques while building something you can actually play and show off.</p>
<p>In this course, you’ll create a fully functional match-3 puzzle game using C and the Raylib graphics library. It’s a hands-on project that covers everything from setting up a game window to adding smooth animations, sound effects, and scoring. If you’ve ever played games like Bejeweled or Candy Crush, this course walks you through making your own version of that, with a retro-style twist using ASCII tiles.</p>
<p>The course is taught by Erik Yuzwa, a developer with deep experience in both C programming and game development. Erik walks through the process clearly and step-by-step, so you don’t need to be a game dev expert to follow along. As long as you have some basic C knowledge, you’ll be able to keep up.</p>
<p>Here’s what the course covers:</p>
<ul>
<li><p><strong>Intro and setup</strong>: Get your project environment ready and learn what tools you'll be using</p>
</li>
<li><p><strong>New project structure</strong>: Create the base for your game files and organize your code</p>
</li>
<li><p><strong>Main game loop</strong>: Set up the logic that will run the game frame by frame</p>
</li>
<li><p><strong>Drawing and centering the tile board</strong>: Display the game grid and align it neatly on the screen</p>
</li>
<li><p><strong>Adding a background image</strong>: Give your game some visual polish</p>
</li>
<li><p><strong>Displaying the score</strong>: Keep track of points and show them to the player</p>
</li>
<li><p><strong>Loading a custom TTF font</strong>: Improve your game’s UI with better text</p>
</li>
<li><p><strong>Mouse input</strong>: Detect player clicks and connect them to gameplay actions</p>
</li>
<li><p><strong>Finding and removing tile matches</strong>: Build the core logic of the match-3 mechanic</p>
</li>
<li><p><strong>Animating tiles</strong>: Make the game feel smooth and responsive</p>
</li>
<li><p><strong>Swapping tiles and managing states</strong>: Handle player interactions and animations cleanly</p>
</li>
<li><p><strong>Adding sound and music</strong>: Use audio to give feedback and create atmosphere</p>
</li>
<li><p><strong>Score popups and burst effects</strong>: Make the game feel more dynamic with visual and point feedback</p>
</li>
<li><p><strong>Wrapping up</strong>: Final polish and review of what you built</p>
</li>
</ul>
<p>By the end of the course, you’ll have a complete game with real gameplay mechanics, not just a demo or prototype. You’ll also come away with a better understanding of how a game engine works at a basic level, how to structure C projects, and how to make a game feel smooth and fun.</p>
<p>The course is available for free on the <a target="_blank" href="https://youtu.be/P7PMA3X1tf8">freeCodeCamp.org YouTube channel</a> (1-hour watch).</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/P7PMA3X1tf8" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
 ]]>
                </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[ Unlock the Mysteries of Pointers in C ]]>
                </title>
                <description>
                    <![CDATA[ There are few things in the programming universe that seem as daunting to beginners as pointers in C. Many new programmers shy away from tackling this essential component of the C programming language, which can leave a critical gap in their knowledg... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/finally-understand-pointers-in-c/</link>
                <guid isPermaLink="false">66b2023b82069b4c678c98b5</guid>
                
                    <category>
                        <![CDATA[ c programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ youtube ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Beau Carnes ]]>
                </dc:creator>
                <pubDate>Thu, 15 Jun 2023 15:14:30 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/06/pointers.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>There are few things in the programming universe that seem as daunting to beginners as pointers in C. Many new programmers shy away from tackling this essential component of the C programming language, which can leave a critical gap in their knowledge.</p>
<p>We just posted a full course on the freeCodeCamp.org YouTube channel that will help you understand this important concept.</p>
<p>Jalal Bounaim developed this course. He is an experienced developer and created the course he would have wanted when he first started programming in C.</p>
<p>This course is tailored for absolute beginners, ensuring no prior knowledge of pointers is required. It systematically demystifies pointers, their use, and the significance they hold in C programming. Pointers, simply put, are variables that store the memory address of another variable – they "point" to the location of data in memory. By the end of the course, you'll have a robust understanding of these powerful tools, and be able to confidently use them in your own coding projects.</p>
<p>The course begins with a brief introduction and then proceeds to explain what a computer is, with an emphasis on CPU, RAM, and bytes. This foundational knowledge is key to understanding how memory works, which is central to the concept of pointers.</p>
<p>The course moves forward to cover a range of topics, including:</p>
<ul>
<li>Data types</li>
<li>Introduction to processes</li>
<li>Process memory layout</li>
<li>Variables in memory</li>
<li>An illustration of a naive change_value program</li>
<li>How to use pointers to change the value</li>
<li>The classic swap problem</li>
</ul>
<p>The course then delves into more specific aspects of pointers. You will learn the reason behind the identical syntax for pointer declaration and dereference, the advantages of passing by reference versus passing by value, and explore why pointers to different data types have the same size.</p>
<p>But that's not all! The course continues to address some commonly raised questions and confusions about pointers:</p>
<ul>
<li>Why do we need a pointer type if all pointers have the same size?</li>
<li>What exactly are void pointers, and why do they confuse many programmers?</li>
<li>What is malloc, and why is it handy? What does it have to do with void pointers?</li>
<li>Are arrays just pointers?</li>
</ul>
<p>Then, we'll dive into some of the more sophisticated aspects of pointers:</p>
<ul>
<li>Why arr[5] and 5[arr] are equivalent</li>
<li>Introduction to pointers to pointers with **argv</li>
<li>Understanding <em>argv[] or *</em>argv</li>
<li>Pointers to functions</li>
<li>Use case with pointers to functions</li>
</ul>
<p>Jalal created this course to be as comprehensive as possible, packed with examples that illustrate how pointers work in a variety of scenarios. Each concept is explained in easy-to-understand terms, ensuring you not only learn how to use pointers but also understand the theory behind them.</p>
<p>Whether you're a budding coder or a seasoned programmer wanting to fill in gaps in your understanding, our course on Pointers in C is sure to offer valuable insights that will take your coding abilities to the next level. So what are you waiting for? </p>
<p>Watch the full course on <a target="_blank" href="https://www.youtube.com/watch?v=MIL2BK02X8A">the freeCodeCamp.org YouTube channel</a> (2-hour watch).</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/MIL2BK02X8A" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Use Pointers in C Programming ]]>
                </title>
                <description>
                    <![CDATA[ If you are learning C programming, you have probably heard the term "pointer" before.  Pointers are one of the most important and powerful features of the C programming language. They allow us to manipulate memory directly, which can be very useful i... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/pointers-in-c-programming/</link>
                <guid isPermaLink="false">66ba61280013ba5d5012bcc1</guid>
                
                    <category>
                        <![CDATA[ c programming ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ valentine Gatwiri ]]>
                </dc:creator>
                <pubDate>Wed, 03 May 2023 19:46:04 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/05/pexels-eyu-p-belen-1428634.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>If you are learning C programming, you have probably heard the term "pointer" before. </p>
<p>Pointers are one of the most important and powerful features of the C programming language. They allow us to manipulate memory directly, which can be very useful in many programming scenarios.</p>
<p>In C, a pointer is simply a variable that holds a memory address. We can think of it as a way to refer to a specific location in memory. </p>
<h2 id="heading-how-to-declare-a-pointer">How to Declare a Pointer</h2>
<p>To declare a pointer variable in C, we use the asterisk <code>*</code> symbol before the variable name. There are two ways to declare pointer variables in C:</p>
<pre><code class="lang-c"><span class="hljs-keyword">int</span> *p;
</code></pre>
<pre><code class="lang-c"><span class="hljs-keyword">int</span>* p;
</code></pre>
<p>Both of these declarations are equivalent and they declare a pointer variable named "p" that can hold the memory address of an integer.</p>
<p>However, it's important to note that if you declare multiple variables in a single statement, you need to include the asterisk before each variable name to indicate that they are all pointers. For example:</p>
<pre><code class="lang-c"><span class="hljs-keyword">int</span> *p, *q, *r;
</code></pre>
<p>This declares three pointer variables named "p", "q", and "r" that can hold the memory address of an integer.</p>
<h2 id="heading-how-to-initialize-a-pointer">How to Initialize a Pointer</h2>
<p>When we declare a pointer variable, it does not automatically point to any particular memory location. To initialize a pointer to point to a specific variable or memory location, we use the ampersand <code>&amp;</code> operator to get the address of that variable. </p>
<p>For example, to initialize the pointer <code>p</code> to point to an integer variable called <code>x</code>, we would write:</p>
<pre><code class="lang-c"><span class="hljs-keyword">int</span> x = <span class="hljs-number">42</span>;
<span class="hljs-keyword">int</span> *p = &amp;x;
</code></pre>
<p>This sets the value of <code>p</code> to be the memory address of <code>x</code>.</p>
<h2 id="heading-how-to-dereference-a-pointer">How to Dereference a Pointer</h2>
<p>Once we have a pointer that points to a specific memory location, we can access or modify the value stored at that location by dereferencing the pointer. </p>
<p>To dereference a pointer, we use the asterisk <code>*</code> symbol again, but this time in front of the pointer variable itself. For example, to print the value of the integer that <code>p</code> points to, we would write:</p>
<pre><code class="lang-c"><span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d\n"</span>, *p);
</code></pre>
<h2 id="heading-what-does-pointer-to-a-pointer-mean">What Does "Pointer to a Pointer" Mean?</h2>
<p>A pointer can also point to another pointer variable. This is known as a "pointer to a pointer". We declare a pointer to a pointer by using two asterisks <code>**</code>. For example:</p>
<pre><code>int x = <span class="hljs-number">42</span>;
int *p = &amp;x;
int **q = &amp;p;
</code></pre><p>Here, <code>q</code> is a pointer to a pointer. It points to the address of the <code>p</code> variable, which in turn points to the address of the <code>x</code> variable</p>
<h2 id="heading-how-to-pass-pointers-to-functions">How to Pass Pointers to Functions</h2>
<p>We can pass pointers to functions as arguments, which allows the function to modify the value of the original variable passed in. This is known as "passing by reference". </p>
<p>To pass a pointer to a function, we simply declare the function parameter as a pointer. For example:</p>
<pre><code class="lang-c"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">increment</span><span class="hljs-params">(<span class="hljs-keyword">int</span> *p)</span> </span>{
    (*p)++;
}

<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> x = <span class="hljs-number">42</span>;
    <span class="hljs-keyword">int</span> *p = &amp;x;
    increment(p);
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d\n"</span>, x); <span class="hljs-comment">// prints 43</span>
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Here, the <code>increment</code> function takes a pointer to an integer (<code>int *p</code>) and increments the value of the integer by one (<code>(*p)++</code>). </p>
<p>In <code>main()</code>, we declare the integer <code>x</code> and a pointer <code>p</code> that points to <code>x</code>. We then call the <code>increment</code> function, passing in the <code>p</code> pointer. After the function call, <code>x</code> has been incremented to <code>43</code>.</p>
<h2 id="heading-how-to-use-pointers-for-dynamic-memory-allocation">How to Use Pointers for Dynamic Memory Allocation</h2>
<p>One of the most powerful uses of pointers in C is for dynamic memory allocation. This allows us to allocate memory at runtime, rather than at compile time. </p>
<p>We use the <code>malloc</code> function to dynamically allocate memory, and it returns a pointer to the allocated memory. For example:</p>
<pre><code class="lang-c"><span class="hljs-keyword">int</span> *p = (<span class="hljs-keyword">int</span>*)<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(<span class="hljs-keyword">int</span>));
</code></pre>
<p>Here, <code>p</code> is a pointer to an integer that has been allocated using <code>malloc</code>. The <code>sizeof</code> operator is used to determine the size of an integer in bytes.</p>
<p>After allocating memory, we can use the pointer variable like any other pointer. When we are finished with the memory, we should free it using the <code>free</code> function. For example:</p>
<pre><code class="lang-c"><span class="hljs-built_in">free</span>(p);
</code></pre>
<p>This frees up the memory that was allocated to <code>p</code>.</p>
<h2 id="heading-what-is-pointer-casting">What is Pointer Casting?</h2>
<p>Sometimes you may need to cast a pointer from one type to another. You can do this using the <code>(type *)</code> syntax. For example:</p>
<pre><code class="lang-c"><span class="hljs-keyword">double</span> *p = (<span class="hljs-keyword">double</span> *)<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(<span class="hljs-keyword">double</span>));
</code></pre>
<p>Here, <code>p</code> is cast to a pointer to a <code>double</code> type.</p>
<h2 id="heading-how-does-pointer-arithmetic-work">How Does Pointer Arithmetic Work?</h2>
<p>Because pointers hold memory addresses, we can perform arithmetic operations on them to move them to different memory locations. </p>
<p>For example, we can increment a pointer to move it to the next memory location. This is often used in array operations, where we use a pointer to access elements of an array. </p>
<p>For example, to print the first element of an integer array using a pointer, we could write:</p>
<pre><code class="lang-c"><span class="hljs-keyword">int</span> arr[] = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>};
<span class="hljs-keyword">int</span> *p = arr; <span class="hljs-comment">// p points to the first element of arr</span>
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d\n"</span>, *p); <span class="hljs-comment">// prints 1</span>
</code></pre>
<p>Here, <code>p</code> is set to point to the first element of the <code>arr</code> array, and <code>*p</code> dereferences the pointer to get the value of the first element (which is <code>1</code>).</p>
<h2 id="heading-how-to-use-pointer-arrays">How to Use Pointer Arrays</h2>
<p> We can also declare arrays of pointers in C. For example:</p>
<pre><code class="lang-c"><span class="hljs-keyword">int</span> *arr[<span class="hljs-number">3</span>];
</code></pre>
<p>This declares an array of three pointers to integers. Each element of the array can point to a separate integer variable.</p>
<h2 id="heading-pointer-arithmetic-and-arrays">Pointer Arithmetic and Arrays</h2>
<p>We can use pointer arithmetic to access elements of an array. For example:</p>
<pre><code class="lang-c"><span class="hljs-keyword">int</span> arr[] = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>};
<span class="hljs-keyword">int</span> *p = arr; <span class="hljs-comment">// p points to the first element of arr</span>
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d\n"</span>, *(p + <span class="hljs-number">1</span>)); <span class="hljs-comment">// prints 2</span>
</code></pre>
<p>Here, <code>p</code> is set to point to the first element of the <code>arr</code> array. We can use pointer arithmetic to access the second element of the array (<code>*(p + 1)</code>), which is <code>2</code>.</p>
<h2 id="heading-example-of-how-to-use-pointers">Example of How to Use Pointers</h2>
<p>Here's an example program that demonstrates some of the concepts we've discussed:</p>
<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

<span class="hljs-keyword">void</span> increment(int *p) {
    (*p)++;
}

int main() {
    int x = <span class="hljs-number">42</span>;
    int *p = &amp;x;
    printf(<span class="hljs-string">"x = %d\n"</span>, x); <span class="hljs-comment">// prints x = 42</span>
    increment(p);
    printf(<span class="hljs-string">"x = %d\n"</span>, x); <span class="hljs-comment">// prints x = 43</span>

    int *arr = (int *)malloc(<span class="hljs-number">3</span> * sizeof(int));
    arr[<span class="hljs-number">0</span>] = <span class="hljs-number">1</span>;
    arr[<span class="hljs-number">1</span>] = <span class="hljs-number">2</span>;
    arr[<span class="hljs-number">2</span>] = <span class="hljs-number">3</span>;
    int *q = arr;
    printf(<span class="hljs-string">"arr[0] = %d\n"</span>, *q); <span class="hljs-comment">// prints arr[0] = 1</span>
    q++;
    printf(<span class="hljs-string">"arr[1] = %d\n"</span>, *q); <span class="hljs-comment">// prints arr[1] = 2</span>
    q++;
    printf(<span class="hljs-string">"arr[2] = %d\n"</span>, *q); <span class="hljs-comment">// prints arr[2] = 3</span>
    free(arr);

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre><p>Output:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/Screenshot-from-2023-05-01-12-03-41.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>This program demonstrates several concepts related to pointers.</p>
<p>First, we declared an integer variable <code>x</code> and a pointer <code>p</code> that points to <code>x</code>. We called the <code>increment</code> function, passing in the <code>p</code> pointer. The <code>increment</code> function modifies the value of <code>x</code> by incrementing it by one. We then printed the value of <code>x</code> before and after the function call to demonstrate that <code>x</code> has been incremented.</p>
<p>Next, we used dynamic memory allocation to allocate an array of three integers. We set the values of the array elements using pointer arithmetic (<code>arr[0] = 1</code>, <code>arr[1] = 2</code>, etc.). We then declared a pointer <code>q</code> that points to the first element of the array. Furthermore, we used pointer arithmetic to access and print the values of each element of the array.</p>
<p>Finally, we freed the memory that was allocated to the array using the <code>free</code> function.</p>
<p>This program demonstrates how pointers can be used to modify the value of a variable, access elements of an array using pointer arithmetic, and dynamically allocate and free memory.</p>
<h2 id="heading-common-pointer-errors">Common Pointer Errors</h2>
<p>Pointers can be tricky to work with, and they can lead to some common errors. </p>
<p>One common error is using an uninitialized pointer. If you declare a pointer variable but do not initialize it to point to a valid memory location, you may get a segmentation fault or other error when you try to dereference the pointer. </p>
<p>Another common error is dereferencing a null pointer, which can also cause a segmentation fault.</p>
<p>Another error to be aware of is using the wrong type of pointer. For example, if you declare a pointer to an integer but then try to dereference it as a pointer to a character, you may get unexpected results or errors.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Pointers are a powerful tool in C programming, but they can be a bit tricky to work with. With practice and patience, you can master pointers and use them to manipulate memory and work with complex data structures. </p>
<p>Thank you for reading!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ strcmp in C – How to Compare Strings in C ]]>
                </title>
                <description>
                    <![CDATA[ Comparing strings is a common task in most programming languages. In C, you can use the strcmp function to handle string comparisons.  In this article, I will show you practical examples of the strcmp function, and offer insights into how it compares... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/strcmp-in-c-how-to-compare-strings-in-c/</link>
                <guid isPermaLink="false">66b90310380c84d101de5db3</guid>
                
                    <category>
                        <![CDATA[ c programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ functions ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Md. Fahim Bin Amin ]]>
                </dc:creator>
                <pubDate>Thu, 27 Apr 2023 20:32:00 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/04/max-duzij-qAjJk-un3BI-unsplash.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Comparing strings is a common task in most programming languages. In C, you can use the <code>strcmp</code> function to handle string comparisons. </p>
<p>In this article, I will show you practical examples of the <code>strcmp</code> function, and offer insights into how it compares strings, what its return values mean, and how to use it effectively. </p>
<p>You'll also see some best practices to help optimize your code.</p>
<h2 id="heading-what-are-strings-in-c">What are Strings in C?</h2>
<p>Before we discuss the <code>strcmp</code> function, it is important to understand the basics of strings in C. </p>
<p>A string is an array of characters terminated by a null character ('\0').<br>Strings are usually represented either using character pointers ( <code>char *</code> ) or character arrays ( <code>char []</code> ).</p>
<h2 id="heading-what-is-the-strcmp-function-of-c-and-how-does-it-work">What is t<strong>he <code>strcmp()</code> function of C, and how does it work?</strong></h2>
<p>The <code>strcmp()</code> function is part of the standard C library ( <code>string.h</code> ). Its primary purpose is to compare the characters of the two strings in sequence until it finds a mismatch or until the end of the strings is reached (that is, the null character '\0'). In our programming world, we call it <a target="_blank" href="https://en.wikipedia.org/wiki/Lexicographic_order">lexicographic order</a>-based searching.</p>
<p>The function prototype for <code>strcmp</code> is as follows:</p>
<pre><code class="lang-c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">strcmp</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> *s1, <span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> *s2)</span></span>;
</code></pre>
<p>Here's what the parameters above mean:</p>
<ul>
<li><strong>s1</strong> denotes the first string to be compared.</li>
<li><strong>s2</strong> denotes the second string to be compared.</li>
</ul>
<p><code>strcmp()</code> is like a game that compares two words. It helps us to identify whether a word comes before or after another word in the dictionary.</p>
<ul>
<li>If the first word (<strong>s1</strong>) comes before the second word (<strong>s2</strong>) in the dictionary, <code>strcmp()</code> gives a negative number.</li>
<li>If the first word (<strong>s1</strong>) comes after the second (<strong>s2</strong>) word in the dictionary, <code>strcmp()</code> gives a positive number.</li>
<li>If both words are the same, <code>strcmp()</code> gives the number 0.</li>
</ul>
<p><code>strcmp()</code> compares the corresponding characters from both strings based on their ASCII values, which are numeric codes that represent each character.</p>
<p>Therefore, if the ASCII value of the first differing character in the first string is less than the ASCII value of the corresponding character in the second string, <code>strcmp()</code> returns a negative number. This indicates that the first string comes before the second string in the dictionary. </p>
<p>If the ASCII value of the first differing character in the first string is greater than the ASCII value of the corresponding character in the second string, <code>strcmp()</code> returns a positive number. This indicates that the first string comes after the second string in the dictionary.</p>
<p>If the two strings are equal up to the end of the shorter string, <code>strcmp()</code> returns a negative, zero, or positive value depending on whether the longer string has a character with a smaller, equal, or greater ASCII value than the null character.</p>
<p>Actually, the C compiler implements this logic of comparing the ASCII values of characters in two strings and returning the result accordingly.</p>
<h2 id="heading-strcmp-function-example-1-basic-string-comparison"><code>strcmp()</code> Function Example #1 – Basic String Comparison</h2>
<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> str1[] = <span class="hljs-string">"apple"</span>;
    <span class="hljs-keyword">char</span> str2[] = <span class="hljs-string">"banana"</span>;

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

    <span class="hljs-keyword">if</span> (result == <span class="hljs-number">0</span>) {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"The strings are equal.\n"</span>);
    } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (result &lt; <span class="hljs-number">0</span>) {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"String 1 is less than string 2.\n"</span>);
    } <span class="hljs-keyword">else</span> {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"String 1 is greater than string 2.\n"</span>);
    }

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Output:</p>
<pre><code><span class="hljs-built_in">String</span> <span class="hljs-number">1</span> is less than string <span class="hljs-number">2.</span>
</code></pre><p>Let me now explain each of the lines from the code given above.</p>
<p>Here, I have taken two different strings as two different character arrays, as we do not have access to direct strings in the <strong>C</strong> programming language. </p>
<p>In the first character string ( <code>str1[]</code> ), I have stored the string <code>apple</code>. In the second character string ( <code>str2[]</code> ), I have stored the string <code>banana</code>. As the function <code>strcmp()</code> gives a boolean output (true/false, or 0/1), I have taken another int variable named <code>result</code> to store the boolean value ( <code>1</code> for <code>true</code> and <code>0</code> for <code>false</code>).</p>
<p>The <code>strcmp()</code> function compares the two strings, and finds out that the first string <code>apple</code> comes before the second string <code>banana</code>. Therefore, the function <code>strcmp()</code> returns a negative value indicating that the first string is less than the second string. </p>
<p>Based on the value in <code>result</code>, it prints <code>String 1 is less than string 2.</code>.</p>
<p>If you are confused about "the string comes before or after another string" then let me explain to you a bit more.</p>
<p>When we say that the first string comes before the second string, we mean that if the two strings were listed in a dictionary or a sorted list of words, the first string would appear before the second string.</p>
<p>In the case of the program above, the first string is <code>"apple"</code>, and the second string is <code>"banana"</code>. If we were to look up these two words in a dictionary, we would find that <code>"apple"</code> appears before <code>"banana"</code>, which means that the first string comes before the second string.</p>
<p>The <code>strcmp()</code> function compares the two strings character by character and determines which string comes first in the dictionary based on the ASCII values of the characters. </p>
<p>In this case, the first character of <code>"apple"</code> (which is <code>'a'</code>) has a lower ASCII value (ASCII value for a = 97) than the first character of <code>"banana"</code> (which is <code>'b'</code> and the ASCII value for b = 98), so <code>strcmp()</code> returns a negative value, indicating that the first string comes before the second string.</p>
<p>I hope this clears up any confusion regarding what the phrase "one string comes after/before another string" means.</p>
<h2 id="heading-strcmp-function-example-2-case-insensitive-string-comparison"><code>strcmp()</code> Function Example #2 – Case-insensitive String Comparison</h2>
<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> str1[] = <span class="hljs-string">"Apple"</span>;
    <span class="hljs-keyword">char</span> str2[] = <span class="hljs-string">"apple"</span>;

    <span class="hljs-comment">// Convert both strings to lowercase</span>
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; str1[i]; i++) {
        str1[i] = <span class="hljs-built_in">tolower</span>(str1[i]);
    }
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; str2[i]; i++) {
        str2[i] = <span class="hljs-built_in">tolower</span>(str2[i]);
    }

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

    <span class="hljs-keyword">if</span> (result == <span class="hljs-number">0</span>) {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"The strings are equal.\n"</span>);
    } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (result &lt; <span class="hljs-number">0</span>) {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"String 1 is less than string 2.\n"</span>);
    } <span class="hljs-keyword">else</span> {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"String 1 is greater than string 2.\n"</span>);
    }

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Output:</p>
<pre><code>The strings are equal.
</code></pre><p>Here I converted both strings into lowercase. I used a for loop to change all the characters in the string to lowercase using the function <code>tolower()</code>. The <code>for</code> loops convert both strings to lowercase by iterating over each character of the strings using an index variable <code>i</code>, and calling the <code>tolower()</code> function on each character to convert it to lowercase.</p>
<p>After converting both strings into lowercase, I called the function <code>strcmp()</code> to check whether both strings are equal or not, like earlier. I stored the output of the <code>strcmp()</code> function in a new variable named <code>result</code>, like earlier.</p>
<p>The <code>if</code> statement checks the value of <code>result</code> and prints out the corresponding message depending on whether the strings are equal, or which string is less or greater. Here, the two strings become equal after converting them to lowercase. Thus, the first <code>if</code> statement executes and provides the output.</p>
<p>Keep in mind that, after converting both strings to lowercase, the value of <code>str1</code> is <code>"apple"</code>, which is the same as the value of <code>str2</code> in the ASCII.</p>
<p>Therefore, when the <code>strcmp()</code> function is called, it returns <code>0</code>, which indicates that the strings are equal. This can be useful when we want to compare strings that may differ in case but should be considered equal.</p>
<h2 id="heading-strcmp-best-practices"><code>strcmp()</code> Best Practices</h2>
<p>Here are some best practices to follow when using the <code>strcmp()</code> function:</p>
<ul>
<li>Always include the <code>string.h</code> header when using <code>strcmp()</code>.</li>
<li>To compare case-insensitive strings, use a custom function like <code>strcasecmp()</code> or a standard library function like <code>stricmp()</code>.</li>
<li>Be cautious when comparing strings that may contain non-ASCII characters, as <code>strcmp()</code> uses the difference in ASCII values for comparisons, which might not work as expected for non-ASCII characters. In such cases, consider using a Unicode-aware comparison function or library. </li>
</ul>
<p>If you want to learn more about ASCII characters, check out <a target="_blank" href="https://www.freecodecamp.org/news/ascii-table-hex-to-ascii-value-character-code-chart-2/">this article</a>.</p>
<h2 id="heading-alternative-string-comparison-functions">Alternative String Comparison Functions</h2>
<p>Apart from <code>strcmp()</code>, there are other string comparison functions available in the C standard library:</p>
<ul>
<li><code>strncmp()</code>: Compares up to a specified number of characters of two strings. It is useful when you want to compare only a portion of the strings.</li>
<li><code>strcoll()</code>: This is quite interesting. Let me explain more about this.</li>
</ul>
<p>The <code>strcoll()</code> function is like a game that helps us compare two words or strings, just like <code>strcmp()</code>. But <code>strcoll()</code> is designed to handle strings that contain non-ASCII characters or require language-specific comparisons, such as words from different languages.</p>
<p><code>strcoll()</code> uses the rules for comparing words that are specific to the language or region, based on the current locale setting of the computer. In other words, it compares words according to the language-specific collation rules of the locale, which may be different from the standard ASCII ordering used by <code>strcmp()</code>.</p>
<p>For example, in the Spanish language, the word "cañón" (which means "canyon" in English) is sorted after "casa" (which means "house" in English), because the letter "ñ" is considered a separate letter in the Spanish alphabet, and comes after "n".</p>
<p>So, <code>strcoll()</code> can be useful for comparing words from different languages or regions that have specific collation rules, and we use this function for this kind of specific need.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this article, we've explored the <code>strcmp()</code> function in C, how it compares strings, and its return values. We've also looked at examples and best practices for using <code>strcmp</code> effectively. </p>
<p>With this knowledge, you can easily compare strings in C like a pro. Remember to consider alternative comparison functions like <code>strncmp()</code> and <code>strcoll()</code> when working with specific use cases, such as partial or locale-specific string comparisons.</p>
<p>If this article helps you in any way, then let me know via <a target="_blank" href="https://twitter.com/Fahim_FBA">Twitter</a> or <a target="_blank" href="https://www.linkedin.com/in/fahimfba/">LinkedIn</a>. You can also follow me on <a target="_blank" href="https://github.com/FahimFBA">GitHub</a> and check my <a target="_blank" href="https://www.youtube.com/@FahimAmin?sub_confirmation=1">YouTube channel</a>, and <a target="_blank" href="https://fahimbinamin.com/">my website</a>. You can also <a target="_blank" href="https://www.buymeacoffee.com/fahimbinamin">buy me a coffee</a> if you want to support me.</p>
<p>Cover: Photo by <a target="_blank" href="https://unsplash.com/@max_duz?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Max Duzij</a> on <a target="_blank" href="https://unsplash.com/s/photos/programming?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Use Functions in C - Explained With Examples ]]>
                </title>
                <description>
                    <![CDATA[ Functions are an essential component of the C programming language. They help you divide bigger problems into smaller, more manageable chunks of code, making it simpler to create and run programs.  We'll look at functions in C, their syntax, and how ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-use-functions-in-c/</link>
                <guid isPermaLink="false">66ba611f739c8c931a5408ad</guid>
                
                    <category>
                        <![CDATA[ c programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ functions ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ valentine Gatwiri ]]>
                </dc:creator>
                <pubDate>Thu, 06 Apr 2023 14:20:12 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/04/guillaume-bolduc-uBe2mknURG4-unsplash.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Functions are an essential component of the C programming language. They help you divide bigger problems into smaller, more manageable chunks of code, making it simpler to create and run programs. </p>
<p>We'll look at functions in C, their syntax, and how to use them successfully in this article.</p>
<h2 id="heading-what-is-a-function-in-c">What is a Function in C?</h2>
<p>A function is a block of code that executes a particular task in programing. It is a standalone piece of code that can be called from anywhere in the program. </p>
<p>A function can take in parameters, run computations, and output a value. A function in C can be created using this syntax:</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 class="hljs-built_in">list</span>)</span> </span>{
   <span class="hljs-comment">// function body</span>
}
</code></pre>
<p>The <code>return_type</code> specifies the type of value that the function will return. If the function does not return anything, the <code>return_type</code> will be <code>void</code>. </p>
<p>The <code>function_name</code> is the name of the function, and the <code>parameter list</code> specifies the parameters that the function will take in.</p>
<h2 id="heading-how-to-declare-a-function-in-c">How to Declare a Function in C</h2>
<p>Declaring a function in C informs the compiler about the presence of a function without giving implementation details. This enables the function to be called by other sections of the software before it is specified or implemented.</p>
<p>A function declaration usually contains the <code>function name</code>, <code>return type</code>, and the parameter types. The following is the syntax for defining 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_list)</span></span>;
</code></pre>
<p>Here, <code>return_type</code> is the data type of the value that the function returns. <code>function_name</code> is the name of the function, and <code>parameter_list</code> is the list of parameters that the function takes as input.</p>
<p>For example, suppose we have a function called <code>add</code> that takes two integers as input and returns their sum. We can declare the function as follows:</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>;
</code></pre>
<p>This tells the compiler that there is a function called <code>add</code> that takes two integers as input and returns an integer as output.</p>
<p>It's worth noting that function declarations do not include the function body, which includes the actual code that runs when the function is invoked. </p>
<p>The body of the function is defined independently of the function statement, usually in a separate block of code called the function definition.</p>
<p>Here's 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-comment">/* function statement */</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> a, <span class="hljs-keyword">int</span> b)</span></span>;

<span class="hljs-comment">/* function definition */</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> a, <span class="hljs-keyword">int</span> b)</span> </span>{
    <span class="hljs-keyword">return</span> a + b;
}

<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> result = add(<span class="hljs-number">2</span>, <span class="hljs-number">3</span>);
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"The result is %d\n"</span>, result);
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>In this example, the <code>add</code> function is declared with a function statement at the top of the file, which specifies its name, return type (<code>int</code>), and parameters (<code>a</code> and <code>b</code>, both <code>int</code>s).</p>
<p>The actual code for the <code>add</code> function is defined in the function definition. Here, the function simply adds its two parameters and returns the result.</p>
<p>The <code>main</code> function calls the <code>add</code> function with arguments <code>2</code> and <code>3</code>, and stores the result in the <code>result</code> variable. Finally, it prints the result using the <code>printf</code> function.</p>
<h3 id="heading-how-to-use-a-function-in-multiple-source-files">How to Use a Function in Multiple Source Files</h3>
<p>If you want to use a function in numerous source files, you must include a function declaration (also known as a function prototype) in the header file and the definition in one source file.</p>
<p>when you build, you first compile the source files to object files, and then you link the object files into the final executable.</p>
<p>Let's create a header file called <code>myfunctions.h</code>:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">ifndef</span> MYFUNCTIONS_H</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> MYFUNCTIONS_H</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> a, <span class="hljs-keyword">int</span> b)</span></span>;<span class="hljs-comment">// Function prototype, its declaration</span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span> <span class="hljs-comment">/* MYFUNCTIONS_H */</span></span>
</code></pre>
<p>In this header file, we declare a function <code>add</code> using a function statement.</p>
<p>Next, let's create a source file called <code>myfunctions.c</code>, which defines the <code>add</code> function:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"myfunctions.h"</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> a, <span class="hljs-keyword">int</span> b)</span> </span>{
    <span class="hljs-keyword">return</span> a + b;
}
</code></pre>
<p>In this file, we include the <code>myfunctions.h</code> header file using quotes, and we define the <code>add</code> function.</p>
<p>Finally, let's create a source file called <code>main.c</code>, which uses the <code>add</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">"myfunctions.h"</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> a = <span class="hljs-number">10</span>, b = <span class="hljs-number">5</span>;
    <span class="hljs-keyword">int</span> sum = add(a, b);

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Sum of %d and %d is %d\n"</span>, a, b, sum);

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>In this file, we include both the <code>stdio.h</code> header file and our <code>myfunctions.h</code> header file using angle brackets and quotes, respectively. We then call the <code>add</code> function, passing in values <code>a</code> and <code>b</code> and storing the result in <code>sum</code>. Finally, we print the result using <code>printf</code>.</p>
<p> The way you create it is heavily influenced by your environment. If you are using an IDE (such as Visual Studio), you must position all files in the proper locations in the project.</p>
<p>If you are creating from the command line e.g Linux. To compile this program, you would need to compile both <code>myfunctions.c</code> and <code>main.c</code> and link them together as shown below:</p>
<pre><code>gcc -c myfunctions.c
gcc -c main.c
gcc -o program main.o myfunctions.o
</code></pre><p>The <code>-c</code> option instructs the compiler to create an object file with the same name as the source file but with a <code>.o</code> suffix. The final instruction joins the two object files to create the final executable, which is named <code>program</code> (the -o option specifies the name of the output file).</p>
<h2 id="heading-what-happens-if-you-call-a-function-before-its-declaration-in-c">What Happens if You Call a Function Before Its Declaration in C?</h2>
<p>In this instance, the computer believes the usual return type is an integer. If the function gives a different data type, it throws an error. </p>
<p>If the return type is also an integer, it will function properly. But some cautions may be generated:</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>
main() {
   <span class="hljs-built_in">printf</span>(<span class="hljs-string">"The returned value: %d"</span>, function);
}
<span class="hljs-function"><span class="hljs-keyword">char</span> <span class="hljs-title">function</span><span class="hljs-params">()</span> </span>{
   <span class="hljs-keyword">return</span> <span class="hljs-string">'V'</span>;
}
</code></pre>
<p>In this code, the function <code>function()</code> is  called before it is declared. This returns an error:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/04/Screenshot-from-2023-04-05-14-03-36.png" alt="Image" width="600" height="400" loading="lazy">
<em>warnings and errors</em></p>
<h2 id="heading-how-to-define-a-function-in-c">How to Define a Function in C</h2>
<p>Assuming you want to create a code that accepts two integers and returns their sum, you can define a function that does that this way:</p>
<pre><code class="lang-c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">sum</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">int</span> result = num1 + num2;
   <span class="hljs-keyword">return</span> result;
}
</code></pre>
<p>In this example, the function <code>sum</code> takes in two integer parameters – <code>num1</code> and <code>num2</code>. The function calculates their sum and returns the result. The return type of the function is <code>int</code>.</p>
<h2 id="heading-where-should-a-function-be-defined">Where Should a Function Be Defined?</h2>
<p>In C, a function can be defined anywhere in the program, as long as it is defined before it is used. But it is a good practice to define functions at the beginning of the file or in a separate file to make the code more readable and organized.</p>
<p>Here's an example code showing how to define a function 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-comment">// function declaration (also known as function prototype)</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> a, <span class="hljs-keyword">int</span> b)</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> x = <span class="hljs-number">10</span>, y = <span class="hljs-number">20</span>, sum;
   sum = add(x, y);
   <span class="hljs-built_in">printf</span>(<span class="hljs-string">"The sum of %d and %d is %d\n"</span>, x, y, sum);
   <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}

<span class="hljs-comment">// function definition</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> a, <span class="hljs-keyword">int</span> b)</span> </span>{
   <span class="hljs-keyword">int</span> result;
   result = a + b;
   <span class="hljs-keyword">return</span> result;
}
</code></pre>
<p>In this example, the function <code>add()</code> is defined after its declaration (or prototype) within the same file.</p>
<p>Another approach is to define the function in a separate header file, which is then included in the main file using the <code>#include</code> directive. For example:</p>
<pre><code class="lang-c"><span class="hljs-comment">// header file: math.h</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">ifndef</span> MATH_H</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> MATH_H</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> a, <span class="hljs-keyword">int</span> b)</span></span>;

<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span>
</code></pre>
<pre><code class="lang-c"><span class="hljs-comment">// main file: main.c</span>
<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">"math.h"</span>  <span class="hljs-comment">// include the header file</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> x = <span class="hljs-number">10</span>, y = <span class="hljs-number">20</span>, sum;
   sum = add(x, y);
   <span class="hljs-built_in">printf</span>(<span class="hljs-string">"The sum of %d and %d is %d\n"</span>, x, y, sum);
   <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<pre><code class="lang-c"><span class="hljs-comment">// implementation file: math.c</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> a, <span class="hljs-keyword">int</span> b)</span> </span>{
   <span class="hljs-keyword">int</span> result;
   result = a + b;
   <span class="hljs-keyword">return</span> result;
}
</code></pre>
<p>In this approach, the function declaration (or prototype) is included in the header file <code>math.h</code>, which is then included in the main file <code>main.c</code> using the <code>#include</code> directive. The function implementation is defined in a separate file <code>math.c</code>.</p>
<p>This approach allows for better code organization and modularity, as the function implementation can be separated from the main program code.</p>
<h3 id="heading-how-to-call-a-function-in-c">How to Call a Function in C</h3>
<p>We can call a function from anywhere in the program once we've defined it. We use the function name followed by the argument list in parentheses to call a function. For example, we can use the following code to call the <code>sum</code> function that we defined earlier:</p>
<pre><code class="lang-c"><span class="hljs-keyword">int</span> a = <span class="hljs-number">5</span>;
<span class="hljs-keyword">int</span> b = <span class="hljs-number">10</span>;
<span class="hljs-keyword">int</span> c = sum(a, b);
</code></pre>
<p>In this code, we are calling the <code>sum</code> function with <code>a</code> and <code>b</code> as its parameters. The function returns the sum of <code>a</code> and <code>b</code>, which is then stored in the variable <code>c</code>.</p>
<h2 id="heading-how-to-pass-parameters-to-a-function">How to Pass Parameters to a Function</h2>
<p>There are two methods of passing parameters (also called arguments) to a function in C: by value and by reference. </p>
<p>When we pass a parameter by value, the method receives a copy of the parameter's value. Changes to the parameter within the code have no effect on the initial variable outside the function. </p>
<p>When we pass a parameter by reference, the method receives a link to the parameter's memory location. Any modifications to the parameter within the code will have an impact on the initial variable outside the function.</p>
<p>Consider the following examples of passing parameters by value and by reference. Assuming we want to create a function that accepts an integer and multiplies it by two, the function can be defined as follows:</p>
<pre><code class="lang-c"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">doubleValue</span><span class="hljs-params">(<span class="hljs-keyword">int</span> num)</span> </span>{
   num = num * <span class="hljs-number">2</span>;
}
</code></pre>
<p>In this example, the function <code>doubleValue</code> takes in an integer parameter <code>num</code> by value. It doubles the value of <code>num</code> and assigns it back to <code>num</code>. However, this change will not affect the original value of <code>num</code> outside the function.</p>
<p>Here's another example that shows how you can pass a single parameter by 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">void</span> <span class="hljs-title">square</span><span class="hljs-params">(<span class="hljs-keyword">int</span> num)</span> </span>{
    <span class="hljs-comment">// Function to calculate the square of a number.</span>
    <span class="hljs-keyword">int</span> result = num * num;
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d\n"</span>, result);
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    square(<span class="hljs-number">5</span>);  <span class="hljs-comment">// Output: 25</span>
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>In this example, we define a function called <code>square</code> that takes an integer parameter <code>num</code> by value. Inside the function, we calculate the square of <code>num</code> and print the result. We then call the function with the argument <code>5</code>.</p>
<p>Now, let's look at an example of passing a parameter by reference:</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">void</span> <span class="hljs-title">square</span><span class="hljs-params">(<span class="hljs-keyword">int</span>* num)</span> </span>{
    <span class="hljs-comment">// Function to calculate the square of a number.</span>
    *num = (*num) * (*num);
}

<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> x = <span class="hljs-number">5</span>;
    square(&amp;x);
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d\n"</span>, x);  <span class="hljs-comment">// Output: 25</span>
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>In this example, we define a function <code>square</code> that takes an integer pointer parameter <code>num</code> by reference. Inside the function, we reference the pointer and calculate the square of the value pointed to by <code>num</code>. </p>
<p>We then call the function with the address of the integer variable <code>x</code>. After calling the function, the value of <code>x</code> is modified to be the square of its original value, which we then print in the <code>main</code> function.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In conclusion, functions are an essential component of C programming. You can use them to divide large problems into smaller, more manageable pieces of code.</p>
<p>You can declare and define functions in C, and pass parameters either by value or by reference. It's a good practice to declare all functions before using them, and to define them at the beginning of the file or in a separate file for better code organization and modularity. </p>
<p>By using functions effectively, you can write cleaner, more readable code that is easier to debug and maintain.</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>
        
            <item>
                <title>
                    <![CDATA[ C Operator – Logic Operators in C Programming ]]>
                </title>
                <description>
                    <![CDATA[ In this article, you will learn about the three logical operators in C. I will first explain what operators are in programming and list the different types of operators available in C.  Then, you will learn the role logical operators have and how to ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/c-operator-logic-operators-in-c-programming/</link>
                <guid isPermaLink="false">66b1e3cb6500ee13f33bd570</guid>
                
                    <category>
                        <![CDATA[ c programming ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Dionysia Lemonaki ]]>
                </dc:creator>
                <pubDate>Wed, 08 Mar 2023 15:25:21 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/03/pexels-mikael-blomkvist-6476587.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In this article, you will learn about the three logical operators in C.</p>
<p>I will first explain what operators are in programming and list the different types of operators available in C. </p>
<p>Then, you will learn the role logical operators have and how to use them with the help of code examples along the way.</p>
<p>Let's get into it!</p>
<h2 id="heading-what-is-an-operator-in-computer-programming">What Is An Operator in Computer Programming?</h2>
<p>In computer programing, an operator is a character, symbol, keyword, or combination of those. It determines what action gets performed on one or more operands.</p>
<p>An operand is an individual data item that gets manipulated by the operator.</p>
<p>Each high-level programming language defines these built-in characters and uses them to tell the compiler to perform arithmetic, relational or logical operations that manipulate data items and then return a final result.</p>
<h3 id="heading-what-are-the-different-types-of-operators-in-c-programming">What Are The Different Types of Operators in C Programming?</h3>
<p>In C programming, operators fall into one of three categories:</p>
<ul>
<li><strong>Unary</strong> operators</li>
<li><strong>Binary</strong> operators</li>
<li><strong>Ternary</strong> operators</li>
</ul>
<p><strong>Unary</strong> operators operate on a single operand. Some of the unary operators in C are:</p>
<ul>
<li>Arithmetic operators such as the increment operator(<code>++</code>), which increments the value of the operand by <code>1</code>. And the decrement operator(<code>--</code>), which decrements the value of the operand by <code>1</code>.</li>
<li>Logical operators like the NOT(<code>!</code>) operator. This operator reverses the logical value of the operand – it changes <code>true</code> to <code>false</code> and <code>false</code> to <code>true</code>.</li>
<li>Bitwise operators like the NOT(<code>~</code>) operator, which changes each <code>0</code> bit to <code>1</code> and each <code>1</code> bit to <code>0</code>.</li>
</ul>
<p><strong>Binary</strong> operators operate on two operands. Some of the binary operators in C are:</p>
<ul>
<li>Arithmetic operators (<code>+, -, *, /, %</code>). These operators perform mathematical calculations on numerical data such as addition, subtraction, multiplication, division, and finding the remainder. </li>
<li>Equality/Relational operators (<code>==, !=, &gt;, &lt;, &gt;=, &lt;=</code>). These operators compare two values and determine if one operand is greater than, less than, equal to, or not equal to the other operand.</li>
<li>Logical/Conditional operators such as the AND(<code>&amp;&amp;</code>) and OR(<code>||</code>) operators.</li>
<li>Bitwise operators (<code>(&amp;, |, ^, &lt;&lt;, &gt;&gt;</code>), which treat data items as a sequence of bits (that is, <code>0</code>s and <code>1</code>s).</li>
<li>Assignment operators (<code>=, +=, -=, *=, /=, %=</code>), which assign a specific value to a variable.  </li>
</ul>
<p>The <strong>Ternary</strong> operator (<code>?:</code>) operates on three operands. The general syntax looks something similar to the following:</p>
<pre><code class="lang-c">(condition) ? expression1 : expression2;
</code></pre>
<p>The ternary operator is a conditional operator you can use as a shorthand for an <code>if..else</code> statement. It performs comparisons and returns a result.</p>
<h2 id="heading-what-is-the-role-of-logical-operators-in-c-programming">What Is the Role of Logical Operators in C Programming?</h2>
<p>You will see logical operators commonly used in conditional statements (such as <code>if..else</code> statements) since they aid in decision making – they determine what action should take place and what code should run next based on conditions you set.</p>
<p>You combine logical operators with one or multiple conditions to create a logical expression.</p>
<p>The logical operators evaluate the logical expression and return a result. </p>
<p>The result is always a Boolean value. A Boolean value determines whether the expression is <code>true</code> or <code>false</code>.</p>
<p>There are three logical operators in C programming: logical AND(<code>&amp;&amp;</code>), logical OR(<code>||</code>), and logical NOT (<code>!</code>).</p>
<p>Let's go into more detail on each one in the following sections.</p>
<h3 id="heading-how-to-use-the-and-ampamp-logical-operator-in-c-programming">How to Use the AND <code>(&amp;&amp;)</code> Logical Operator in C Programming</h3>
<p>The logical AND(<code>&amp;&amp;</code>) operator checks whether <strong>all</strong> operands are <code>true</code> – 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:
| First Operand | Second Operand | Result |
| --- | --- | --- |
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |</p>
<p>Something to note here is that, when the first operand is <code>false</code>, the second operand is not evaluated.</p>
<p>Let's look at an example:
The result of <code>(10 == 10) &amp;&amp; (20 == 20)</code> is <code>true</code> because <em>both</em> operands are <code>true</code> – <code>(10 == 10)</code> is <code>true</code> <em>and</em> <code>(20 == 20)</code> is <code>true</code>.</p>
<p>Let's look at another example:
The result of <code>(10 == 20) &amp;&amp; (20 == 20)</code> is <code>false</code> because one of the operands is <code>false</code> – in this case, the first operand is <code>false</code>, so the second operand doesn't get evaluated.</p>
<p>Now, let's see how you can use the <code>&amp;&amp;</code> operator in an <code>if</code> statement:</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">20</span>;
  <span class="hljs-keyword">int</span> b = <span class="hljs-number">30</span>;

  <span class="hljs-keyword">if</span> (a &gt; <span class="hljs-number">10</span> &amp;&amp; b &gt; <span class="hljs-number">10</span>)
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Both numbers are greater than 10\n"</span>);
}

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

<span class="hljs-comment">// Both numbers are greater than 10</span>
</code></pre>
<p>In the example above, the output is <code>Both numbers are greater than 10</code> because the condition <code>a &gt; 10 &amp;&amp; b &gt; 10</code> is satisfied.</p>
<p>Both <code>a &gt; 10</code> and <code>b &gt; 10</code> are <code>true</code>, so the result is <code>true</code>. </p>
<p>If either <code>a</code> or <code>b</code> did not satisfy the condition, then there would be no output in the console since I have not specified an <code>else</code> condition.</p>
<h3 id="heading-how-to-use-the-or-logical-operator-in-c-programming">How to Use the OR <code>(||)</code> Logical Operator in C Programming</h3>
<p>The logical OR(<code>||</code>) operator checks whether one of the operands is <code>true</code> – the result is <code>true</code> if <em>at least one</em> 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:
| First Operand | Second Operand | Result |
| --- | --- | --- |
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |</p>
<p>Note that with the OR (<code>||</code>) operator, if the first operand is <code>true</code>, then the second operator is not evaluated.</p>
<p>Let's look at an example:
The result of <code>(10 == 20) || (20 == 20)</code> is <code>true</code> because at least one of the operands is <code>true</code>, in this case, it is the second operand, even if the first operand is <code>false</code>. </p>
<p>Let's look at another example:
The result of <code>(20 == 20) || (10 == 20)</code> is <code>true</code> because one of the operands is <code>true</code> – in this case, since the first operand is <code>true</code>, the second one is not evaluated.</p>
<p>Now, let's see how you can use the OR (<code>||</code>) operator in an <code>if</code> statement:</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">20</span>;
  <span class="hljs-keyword">int</span> b = <span class="hljs-number">5</span>;

  <span class="hljs-keyword">if</span> (a &gt; <span class="hljs-number">10</span> || b &gt; <span class="hljs-number">10</span>)
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"At least one of the numbers is greater than 10"</span>);
}
</code></pre>
<p>In the example above, the output is <code>At least one of the numbers is greater than 10</code> because the condition <code>a &gt; 10 || b &gt; 10</code> is satisfied – at least one of the operands is <code>true</code>.</p>
<p>The first condition, <code>a &gt; 10</code>, is <code>true</code>, so the result is <code>true</code>.</p>
<p>If both <code>a</code> and <code>b</code> were <code>false</code>, there would be no output.</p>
<h3 id="heading-how-to-use-the-not-logical-operator-in-c-programming">How to Use the NOT <code>(!)</code> Logical Operator in C Programming</h3>
<p>The logical NOT(<code>!</code>) operator negates the operand – that is, it returns the opposite of 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>Here is the truth table for the NOT(<code>!</code>) operator:
| Operand | Result |
| --- | --- |
| true | false |
| false | true |</p>
<p>Let's look at an example:
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.</p>
<p>And let's look at another example:
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>
<p>Now, check the example below for how you can use the NOT(<code>!</code>) operator in an <code>if</code> statement:</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">20</span>;
  <span class="hljs-keyword">int</span> b = <span class="hljs-number">5</span>;

  <span class="hljs-keyword">if</span> ( a &gt; b)
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"a is greater than b\n"</span>);
}
</code></pre>
<p>The output is <code>a is greater than b</code> because the condition <code>a &gt; b</code> is <code>true</code>.</p>
<p>However, if you used the NOT(<code>!</code>) operator, the condition is no longer <code>true</code>, so there would be no output:</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">20</span>;
  <span class="hljs-keyword">int</span> b = <span class="hljs-number">5</span>;

  <span class="hljs-keyword">if</span> ( !(a &gt; b))
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"a is greater than b\n"</span>);
}
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>And there you have it! You now know how the three logical operators work in C programming.</p>
<p>To learn more about C, <a target="_blank" href="https://www.freecodecamp.org/news/the-c-beginners-handbook/">give this C beginner's handbook a read</a> to become familiar with the basics of the language.</p>
<p>Thanks for reading, and happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Use scanf( ) in C to Read and Store User Input ]]>
                </title>
                <description>
                    <![CDATA[ The scanf() function is a commonly used input function in the C programming language. It allows you to read input from the user or from a file and store that input in variables of different data types.  Input is an essential part of most programs, an... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/using-scanf-in-c/</link>
                <guid isPermaLink="false">66ba61370013ba5d5012bcc3</guid>
                
                    <category>
                        <![CDATA[ c programming ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ valentine Gatwiri ]]>
                </dc:creator>
                <pubDate>Mon, 06 Mar 2023 21:59:30 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/03/pexels-element-digital-1370294.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>The <code>scanf()</code> function is a commonly used input function in the C programming language. It allows you to read input from the user or from a file and store that input in variables of different data types. </p>
<p>Input is an essential part of most programs, and the <code>scanf()</code> function provides an easy way to read input in a variety of formats. But it's important to use <code>scanf()</code> carefully and to always validate user input to prevent security vulnerabilities and unexpected program behavior. </p>
<p>In this article, we'll take a closer look at the <code>scanf()</code> function and how to use it effectively in C programming.</p>
<h3 id="heading-what-you-will-learn"><strong>What you will learn</strong></h3>
<p>Here are some things that you will learn:</p>
<ol>
<li>What <code>scanf()</code> is and what it's used for</li>
<li>How to use <code>scanf()</code> to read input from the user or from a file</li>
<li>The syntax of the <code>scanf()</code> function and how to use conversion specifiers to read input</li>
<li>How to store input in variables using pointers</li>
<li>The importance of input validation and error checking to prevent unexpected program behavior and security vulnerabilities</li>
</ol>
<h2 id="heading-syntax-of-the-scanf-function">Syntax of the <code>scanf()</code> function</h2>
<p>The basic syntax of the <code>scanf()</code> function is as follows:</p>
<pre><code class="lang-c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">scanf</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> *format, ...)</span></span>;
</code></pre>
<p>The <code>scanf()</code> function returns the number of items successfully read, or <code>EOF</code> if an error occurs or the end of the input stream is reached. </p>
<p>The function takes two arguments:</p>
<ol>
<li><code>format</code>: A string that specifies the format of the input to be read. This string can contain conversion specifiers that tell <code>scanf()</code> what type of input to expect and how to read it. See the next section for more details on conversion specifiers.</li>
<li><code>...</code>: A variable-length argument list that contains the memory addresses of variables where the input values will be stored. These memory addresses must be passed as pointers.</li>
</ol>
<h2 id="heading-how-to-use-conversion-specifiers-to-read-input">How to Use Conversion Specifiers to Read Input</h2>
<p>The <code>scanf()</code> function takes a format string as its first argument, which specifies the format and data types of the input that will be read. </p>
<p>The format string can include conversion specifiers, which begin with the percent sign (<code>%</code>) and are followed by one or more characters that specify the type of data to be read. </p>
<p>The most common conversion specifiers are:</p>
<ul>
<li><code>%d</code>: reads an integer value</li>
<li><code>%f</code>: reads a floating-point value</li>
<li><code>%c</code>: reads a single character</li>
<li><code>%s</code>: reads a string of characters</li>
<li><code>%lf</code>: reads a double-precision floating-point value</li>
</ul>
<p>After the format string, the <code>scanf()</code> function takes a variable number of arguments, each of which is a pointer to the variable where the input value will be stored. The number and type of arguments must match the conversion specifiers in the format string.</p>
<p>For example, the following code reads an integer value and a floating-point value from the user, and stores them in the variables <code>num</code> and <code>fnum</code>, respectively:</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> num;
    <span class="hljs-keyword">float</span> fnum;
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Enter an integer and a floating-point number: "</span>);
    <span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d %f"</span>, &amp;num, &amp;fnum);
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"You entered %d and %f\n"</span>, num, fnum);
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Below is the expected output:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/03/Screenshot-from-2023-03-06-11-06-14.png" alt="Image" width="600" height="400" loading="lazy">
<em>output</em></p>
<p>In this example, the format string <code>"%d %f"</code> tells <code>scanf()</code> to read an integer value followed by a floating-point value, separated by a space. The <code>&amp;</code> operator is used to pass the address of the <code>num</code> and <code>fnum</code> variables to <code>scanf()</code>, so that the input values can be stored in those variables.</p>
<h2 id="heading-conversion-specifiers-vs-type-specifiers">Conversion Specifiers vs Type Specifiers</h2>
<p>In the C programming language, "conversion specifiers" and "type specifiers" are related concepts, but they have different meanings and purposes.</p>
<p>A "type specifier" is a keyword that specifies the data type of a variable or expression. For example, the <code>int</code>, <code>float</code>, and <code>char</code> keywords are type specifiers that indicate integer, floating-point, and character data types, respectively. We use type specifiers to declare variables and functions and to define the return type of a function.</p>
<p>On the other hand, a "conversion specifier" is a symbol we use in format strings to specify the format of input and output operations. Conversion specifiers start with the <code>%</code> character, followed by a single letter or sequence of characters that indicates the type of data to be read or written. For example, the <code>%d</code> conversion specifier reads integer values, while the <code>%f</code> specifier reads floating-point values.</p>
<p>In summary, type specifiers are used to specify the data type of variables and expressions, while conversion specifiers are used to specify the format of input and output operations. Both concepts are important in C programming and are used in different contexts.</p>
<h2 id="heading-how-to-store-input-in-variables-using-pointers">How to Store Input in Variables Using Pointers</h2>
<p>To store input in a variable using <code>scanf()</code>, you need to pass the memory address of the variable as an argument to the function using the <code>&amp;</code> (address of) operator. This is because <code>scanf()</code> expects pointers as arguments to store input values directly in memory locations.</p>
<p>Here's an example of using <code>scanf()</code> to read an integer value from the user and store it in a variable called <code>num</code>:</p>
<pre><code class="lang-c"><span class="hljs-keyword">int</span> num;
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"Enter an integer: "</span>);
<span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d"</span>, &amp;num);
</code></pre>
<p>In this example, the <code>%d</code> conversion specifier tells <code>scanf()</code> to expect an integer input value. The memory address of the <code>num</code> variable is passed to <code>scanf()</code> using the <code>&amp;</code> operator, which returns a pointer to the memory location of <code>num</code>.</p>
<p>If you need to read multiple input values, you can pass multiple pointers as arguments to <code>scanf()</code> in the order that they appear in the format string. For example, to read two integer values and store them in variables <code>num1</code> and <code>num2</code>, you could do:</p>
<pre><code class="lang-c"><span class="hljs-keyword">int</span> num1, num2;
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"Enter two integers: "</span>);
<span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d %d"</span>, &amp;num1, &amp;num2);
</code></pre>
<p>Note that it's important to make sure that the data types of the input values match the data types of the variables that you're storing them in. If the types don't match, the input value may be interpreted incorrectly, leading to unexpected program behavior. </p>
<p>Additionally, it's a good practice to validate input values and handle input errors, as discussed in the next section.</p>
<h2 id="heading-input-validation-and-error-checking">Input Validation and Error Checking</h2>
<p>Input validation and error checking are important concepts in programming, especially when dealing with user input or input from external sources. In C, you can use various techniques to validate input and handle input errors.</p>
<p>One common technique is to use the return value of <code>scanf()</code> to check if the input operation was successful or if an error occurred. The <code>scanf()</code> function returns the number of input values that were successfully read and stored, or <code>EOF</code> if an error occurred or the end of the input stream was reached. </p>
<p>By checking the return value, you can determine if the input operation was successful or if an error occurred.</p>
<p>For example, if you're using <code>scanf()</code> to read an integer value from the user and store it in a variable called <code>num</code>, you could use the following code to validate the input:</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;stdlib.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> num;
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Enter an integer: "</span>);
    <span class="hljs-keyword">if</span> (<span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d"</span>, &amp;num) != <span class="hljs-number">1</span>) {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Error: Invalid input\n"</span>);
        <span class="hljs-built_in">exit</span>(<span class="hljs-number">1</span>);
    }
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>In this example, the <code>scanf()</code> function is used to read an integer value and store it in the <code>num</code> variable. The return value of <code>scanf()</code> is compared to <code>1</code> to check if one input value was successfully read and stored. If the return value is not <code>1</code>, an error message is printed to the console and the program exits with an error code.</p>
<p>Below is the expected output:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/03/Screenshot-from-2023-03-06-11-02-36.png" alt="Image" width="600" height="400" loading="lazy">
<em>Output</em></p>
<p>You can use similar techniques to validate input of other types, such as floating-point numbers or strings. For example, to validate the input of a floating-point value, you could use the <code>%f</code> conversion specifier and check if the return value of <code>scanf()</code> is equal to <code>1</code>.</p>
<p>In addition to checking the return value of <code>scanf()</code>, you can also use other techniques to validate input and handle errors, such as using <code>fgets()</code> to read input as a string and then parsing the string to extract the desired values, or using regular expressions to validate input patterns.</p>
<p>It's important to carefully validate input and handle errors to prevent unexpected program behavior or security vulnerabilities.</p>
<h2 id="heading-scanf-and-the-standard-c-library"><code>scanf()</code> and the Standard C Library</h2>
<p>The <code>scanf()</code> function is included in the standard C library, which provides a collection of pre-defined functions that you can use in C programs. The <code>stdio.h</code> header file is also part of the standard C library and contains declarations for input and output functions like <code>scanf()</code>, <code>printf()</code>, and others.</p>
<p>To use the <code>scanf()</code> function in a C program, you need to include the <code>stdio.h</code> header file at the beginning of your program using the <code>#include</code> preprocessor directive. This allows you to access the functions and data types defined in the standard C library, including <code>scanf()</code>.</p>
<p>Here's an example of how to use <code>scanf()</code> in a 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> </span>{
    <span class="hljs-keyword">int</span> num;
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Enter an integer: "</span>);
    <span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d"</span>, &amp;num);
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"You entered: %d\n"</span>, num);
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>In this example, we first include the <code>stdio.h</code> header file using <code>#include</code>. We then define a variable <code>num</code> of type <code>int</code>. We use the <code>printf()</code> function to prompt the user to enter an integer, and the <code>scanf()</code> function reads the user's input and stores it in the <code>num</code> variable. Finally, we use another <code>printf()</code> statement to print the value of <code>num</code>.</p>
<p>Note that we use the <code>&amp;</code> operator before the variable name in the <code>scanf()</code> function to pass the memory address of the variable to the function. This allows the <code>scanf()</code> function to store the user's input directly in the variable.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>The <code>scanf()</code> function in C is a powerful tool for reading input from the user or from a file and storing it in variables. By specifying conversion specifiers in the format string, you can read input values of different types, such as integers, floating-point numbers, and strings.</p>
<p>When using <code>scanf()</code>, it's important to be aware of potential input errors and to validate input values to prevent unexpected program behavior or security vulnerabilities. </p>
<p>You can use the return value of <code>scanf()</code> to check if the input operation was successful. You can also use various techniques to validate input and handle errors, such as checking input ranges, using regular expressions, or converting input values to strings and parsing them.</p>
<p>Overall, <code>scanf()</code> is a versatile function that you can use in a variety of programming scenarios. By understanding how to use <code>scanf()</code> effectively and how to validate and handle input errors, you can build robust and reliable C programs that interact with users and external data sources in a safe and secure manner.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Write And Run C and C++ Code in Visual Studio Code ]]>
                </title>
                <description>
                    <![CDATA[ Visual Studio Code (or VS Code for short) is a very common and widely used text editor and IDE (Integrated Development Environment). You can make VS Code very powerful like an IDE using a lot of extensions. Before approaching the process of running y... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-write-and-run-c-cpp-code-on-visual-studio-code/</link>
                <guid isPermaLink="false">66b9030605ed142b6e64c273</guid>
                
                    <category>
                        <![CDATA[ C++ ]]>
                    </category>
                
                    <category>
                        <![CDATA[ c programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ compilers ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Visual Studio Code ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Visual Studio Code ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Md. Fahim Bin Amin ]]>
                </dc:creator>
                <pubDate>Fri, 20 Jan 2023 21:45:48 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/01/asd.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Visual Studio Code (or VS Code for short) is a very common and widely used text editor and IDE (Integrated Development Environment). You can make VS Code very powerful like an IDE using a lot of extensions.</p>
<p>Before approaching the process of running your first C or C++ code on Visual Studio Code, let me guide you through the process and get it all set up based on the operating system you are using on your computer.</p>
<h2 id="heading-c-and-c-compilers">C and C++ compilers</h2>
<p>For running C or C++ code, you just need to have a valid C/C++ compiler installed on your computer. If you are using a Linux operating system, then there is a high chance that it is already installed on your system. But we need to make sure that it is correctly installed.</p>
<p>For checking whether or not you have the compiler (GCC/G++/MinGW) installed on your system or not, you have to check the compiler version first. </p>
<p>Simply open your terminal and use <code>gcc --version</code> and <code>g++ --version</code>. If you get the version number, then the compiler is already installed on your system. </p>
<p>You can check the version using the same commands on any operating system, whether that is a Windows, Linux, or macOS-based operating system. </p>
<p>If you get feedback on your terminal that it does not know anything about GCC or G++, then you have to install the compiler correctly. </p>
<p>If you are using the most used Windows operating system, then I already have written an in-depth article showing you all the processes step-by-step on freeCodeCamp. Make sure to read the entire article first, as it also contains a complete video to provide you with complete support.</p>
<p><a target="_blank" href="https://www.freecodecamp.org/news/how-to-install-c-and-cpp-compiler-on-windows/">Embedded content</a></p>
<p>If you are using another operating system, and you don't have the compilers installed, then make sure to install them before proceeding.</p>
<h2 id="heading-how-to-install-vs-code-or-vs-code-insiders">How to Install VS Code or VS Code Insiders</h2>
<p>You have to download Visual Studio Code directly from the official website: <a target="_blank" href="https://code.visualstudio.com/">https://code.visualstudio.com/</a>.</p>
<p>If you want, you can also install VS Code Insiders, and the same process is applicable for that as well. </p>
<p>Visual Studio Code Insiders is actually the "Insiders" build of Visual Studio Code, which contains all the latest features that are shipped daily. You can think of VS Code as the stable release and the VS Code Insiders as the Insiders release of that.</p>
<p>If you want to experience the latest updates instantly, then you might also try Visual Studio Code Insiders (I use it myself). For downloading VS Code Insiders, you can visit the official website for VS Code Insiders here: <a target="_blank" href="https://code.visualstudio.com/insiders/">https://code.visualstudio.com/insiders/</a></p>
<p>Make sure to download the exact file for your operating system.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/image-163.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Download Page: VS Code</strong></em></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/image-164.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Download Page: VS Code Insiders</strong></em></p>
<p>The installation process is pretty basic. But I am going to show you all the steps sequentially. For now, I am going to show you the installation process using VS Code Insiders, but everything you will see here is going to be exactly the same for VS Code as well.</p>
<p>Make sure to click the box on the "I accept the agreement " box and click on <strong>Next</strong>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/image-165.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Accept the agreement and click Next</strong></em></p>
<p>Keep everything as it is. Do not change anything from here.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/image-168.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Click Next</strong></em></p>
<p> Click <strong>Next</strong>. Again, simply click <strong>Next</strong>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/image-170.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Click Next</strong></em></p>
<p>Make sure to add the checkmark (✔) on all of the boxes. Then click on <strong>Next</strong>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/image-171.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Check all of the boxes, and click Next</strong></em></p>
<p>Click on <strong>Install</strong>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/image-172.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Click Install</strong></em></p>
<p>It might take a little time to finish the installation.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/image-173.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Let it finish...</strong></em></p>
<p>Click on <strong>Finish</strong>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/image-175.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Click Finish</strong></em></p>
<p>Congrats - you've successfully installed VS Code/VS Code Insiders on your system. Now, cheers! 🥂</p>
<h2 id="heading-how-to-prepare-vs-codevs-code-insiders-for-c-and-c-code">How to Prepare VS Code/VS Code Insiders For C and C++ Code</h2>
<p>First, open VS Code or VS Code Insiders.</p>
<p>Go to the Extension tab. Search for "C" or "C++" and install the first one that is already verified by Microsoft itself.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/image-178.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Install C/C++ extension</strong></em></p>
<p>Also, install <strong>C/C++ Extension Pack</strong>. It should also be verified by Microsoft.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/image-179.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Install C/C++ Extension Pack</strong></em></p>
<p>Then you have to search for <strong>Code Runner</strong> and install the extension as well.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/image-180.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Install Code Runner Extension</strong></em></p>
<p>Now, we need to change some settings.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/image-177.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Change some settings</strong></em></p>
<p>Click the <strong>gear</strong> box (It is called the Manage section), and then click <strong>Settings</strong>. Alternatively, you can also use the shortcut keys <code>Ctrl</code> + <code>,</code>. You need to replace the <code>Ctrl</code> key with the Command key for Mac.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/image-182.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Type "Run code in terminal" and press Enter key</strong></em></p>
<p>In the search bar, type "Run code in terminal" and press the Enter key.</p>
<p>Scroll down a little bit until you find <code>Code-runner: Run In Terminal</code>. Make sure that the box is checked (✔).</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/image-184.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Make sure to check the box</strong></em></p>
<p>Now you need to restart your VS Code/VS Code Insiders. Simply close and reopen the program.</p>
<h2 id="heading-how-to-test-your-code">How to Test Your Code</h2>
<p>Simply open VS Code/VS Code Insiders, open any folder, and create any file with the extension <code>.c</code> for the C file and <code>.cpp</code> for the C++ file. </p>
<p>After writing your code, you can run the code directly using the play button you'll find in the upper right corner.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/image-185.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>This is how you can run any C/C++ program from VS Code/Insiders</strong></em></p>
<p>It will compile and then run the code directly. After running a code, the code runner button would be set default to run directly. So, your computer is 100% ready for compiling and running any C/C++ programming code.  😀</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Thanks for reading the entire article. If it helps you then you can also check out other articles of mine at <a target="_blank" href="https://www.freecodecamp.org/news/author/fahimbinamin/">freeCodeCamp</a>.</p>
<p>If you want to get in touch with me, then you can do so using <a target="_blank" href="https://twitter.com/Fahim_FBA">Twitter</a>, <a target="_blank" href="https://www.linkedin.com/in/fahimfba/">LinkedIn</a>, and <a target="_blank" href="https://github.com/FahimFBA">GitHub</a>. </p>
<p>You can also <a target="_blank" href="https://www.youtube.com/@FahimAmin?sub_confirmation=1">SUBSCRIBE to my YouTube channel</a> (Code With FahimFBA) if you want to learn various kinds of programming languages with a lot of practical examples regularly.</p>
<p>If you want to check out my highlights, then you can do so at my <a target="_blank" href="https://www.polywork.com/fahimbinamin">Polywork timeline</a>.</p>
<p>You can also <a target="_blank" href="https://fahimbinamin.com/">visit my website</a> to learn more about me and what I'm working on.</p>
<p>Thanks a bunch!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Find the Size of an Array in C with the sizeof Operator ]]>
                </title>
                <description>
                    <![CDATA[ When coding in the C programming language, there may be times when you need to know the size of an array. For example, when you want to loop through all the elements stored in the array to determine whether a specific value is present. In this articl... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-find-the-size-of-an-array-in-c-with-the-sizeof-operator/</link>
                <guid isPermaLink="false">66b1e41b8f7b9fe685bd6139</guid>
                
                    <category>
                        <![CDATA[ arrays ]]>
                    </category>
                
                    <category>
                        <![CDATA[ c programming ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Dionysia Lemonaki ]]>
                </dc:creator>
                <pubDate>Mon, 05 Dec 2022 14:53:05 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/11/pexels-eduardo-dutra-2115217-1.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>When coding in the C programming language, there may be times when you need to know the size of an array.</p>
<p>For example, when you want to loop through all the elements stored in the array to determine whether a specific value is present.</p>
<p>In this article, you will learn how to find the size of an array using the <code>sizeof()</code> operator.</p>
<p>Let's dive in!</p>
<h2 id="heading-what-is-an-array-in-the-c-programming-language">What is An Array in the C Programming Language?</h2>
<p>Arrays let you store multiple values under the same variable name.</p>
<p>An array in the C programming language is a collection of items of the same data type. This means you can create an array of only integer values or an array of <code>char</code>s and so on.</p>
<p>To create an array in C, you first need to specify the data type of the values the array will store.</p>
<p>Then, you give the array a name followed by a pair of square brackets, <code>[]</code>.</p>
<p>Inside the square brackets, you can specify the size of the array.</p>
<p>So, here is how you would create an array of type <code>int</code> called <code>faveNumbers</code> that will hold <code>5</code> integers:</p>
<pre><code class="lang-c"><span class="hljs-keyword">int</span> faveNumbers[<span class="hljs-number">5</span>];
</code></pre>
<p>To insert values inside the array during its declaration, use the assignment operator, <code>=</code>, and a pair of curly braces, <code>{}</code>.</p>
<p>Inside the curly braces, enter the items and separate each one with a comma:</p>
<pre><code class="lang-c"><span class="hljs-keyword">int</span> faveNumbers[<span class="hljs-number">5</span>] = {<span class="hljs-number">7</span>, <span class="hljs-number">33</span>, <span class="hljs-number">13</span>, <span class="hljs-number">9</span>, <span class="hljs-number">29</span>};
</code></pre>
<p>The code above creates an array with the name <code>faveNumbers</code> that holds <code>5</code> integers, <code>7, 33, 13, 9, 29</code>.</p>
<p>You could also write the code above as follows:</p>
<pre><code class="lang-c"><span class="hljs-keyword">int</span> faveNumbers[] = {<span class="hljs-number">7</span>, <span class="hljs-number">33</span>, <span class="hljs-number">13</span>, <span class="hljs-number">9</span>, <span class="hljs-number">29</span>};
</code></pre>
<p>In the example above, I didn't specify the size of the array. </p>
<p>However, the compiler can tell that the size is <code>5</code> since I initialized it with <code>5</code> elements.</p>
<p>Something to note here is that you cannot change the size and type of the array once you declare it since they have a fixed length.</p>
<h2 id="heading-how-to-find-the-size-of-an-array-in-the-c-programming-language">How to Find the Size of An Array in the C Programming Language</h2>
<p>C does not provide a built-in way to get the size of an array. </p>
<p>With that said, it does have the built-in <code>sizeof</code> operator, which you can use to determine the size.</p>
<p>The general syntax for using the <code>sizeof</code> operator is the following:</p>
<pre><code>datatype size = sizeof(array_name) / sizeof(array_name[index]);
</code></pre><p>Let's break it down:</p>
<ul>
<li><code>size</code> is the variable name that stores the size of the array, and <code>datatype</code> specifies the type of data value stored in <code>size</code>.</li>
<li><code>sizeof(array_name)</code> calculates the size of the array in bytes.</li>
<li><code>sizeof(array_name[index])</code> calculates the size of one element in the array.</li>
</ul>
<p>Now, let's see this operation in action and break it down into individual steps to see how it works.</p>
<p>Firstly, the <code>sizeof</code> operator returns the total amount of memory allocated to the array in bytes. </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-comment">// my array</span>
    <span class="hljs-keyword">int</span> faveNumbers[] = {<span class="hljs-number">7</span>, <span class="hljs-number">33</span>, <span class="hljs-number">13</span>, <span class="hljs-number">9</span>, <span class="hljs-number">29</span>};

    <span class="hljs-comment">// using sizeof to get the size of the array in bytes</span>
    <span class="hljs-keyword">size_t</span> size = <span class="hljs-keyword">sizeof</span>(faveNumbers);

    <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 20 bytes</span>
</code></pre>
<p>However, the code above doesn't calculate the size of the array directly. </p>
<p>You will need some extra programming logic, which will look something like this:</p>
<pre><code>array_length = (total size <span class="hljs-keyword">of</span> the array) / (size <span class="hljs-keyword">of</span> the first element <span class="hljs-keyword">in</span> the array)
</code></pre><p>To find the length of the array, you need to divide the total amount of memory by the size of one element - this method works because the array stores items of the same type. </p>
<p>So, you can divide the total number of bytes by the size of the first element in the array.</p>
<p>To access the first element in an array, specify the name and, in square brackets, include <code>0</code>. </p>
<p>In programming and Computer Science in general, indexing always starts at <code>0</code>, so the first element in an array will always have an index of <code>0</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> faveNumbers[] = {<span class="hljs-number">7</span>, <span class="hljs-number">33</span>, <span class="hljs-number">13</span>, <span class="hljs-number">9</span>, <span class="hljs-number">29</span>};

    <span class="hljs-keyword">size_t</span> size = <span class="hljs-keyword">sizeof</span>(faveNumbers) / <span class="hljs-keyword">sizeof</span>(faveNumbers[<span class="hljs-number">0</span>]);

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

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

<span class="hljs-comment">// The length of the array is 5</span>
</code></pre>
<p>Something to note here is the size of data types will vary from platform to platform.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Hopefully, this article helped you understand how to find the size of an array in the C programming language using the built-in <code>sizeof</code> operator.</p>
<p>Thank you for reading, and happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Learn C Programming Using the Classic Book by Kernighan and Ritchie ]]>
                </title>
                <description>
                    <![CDATA[ Dennis Ritchie created the C programming language. He also co-authored the authoritative reference book on C programming with Brian Kernighan. If you want to truly understand C programming, it is a good idea to go straight to the source. We just publ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/learn-c-programming-classic-book-dr-chuck/</link>
                <guid isPermaLink="false">66b2040df31aa965000e585c</guid>
                
                    <category>
                        <![CDATA[ c programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ youtube ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Beau Carnes ]]>
                </dc:creator>
                <pubDate>Thu, 18 Aug 2022 14:34:51 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/08/cprogramming.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Dennis Ritchie created the C programming language. He also co-authored the authoritative reference book on C programming with Brian Kernighan. If you want to truly understand C programming, it is a good idea to go straight to the source.</p>
<p>We just published a C programming course on the freeCodeCamp.org YouTube channel. In the course, Dr. Charles Severance will help you learn C programming by taking you through the classic 1978 C programming book written by Ritchie and Kernighan. Dr. Charles Severance (aka Dr. Chuck) is a professor at the University of Michigan and a very popular software instructor.</p>
<p>The course will help you understanding computer architecture and low-level programming.</p>
<p>The classic Kernighan and Ritchie book places the reader in the middle of the 1970's transition from a hardware-centered computer science to a focus on writing portable and efficient software. C was used to develop operating systems like Unix, Minix, and Linux and progamming languages like C++, Java, JavaScript, and Python.</p>
<p>In this course we will be reflecting on how C provided an important foundation for the creation of modern programming languages. This is not a good place to start if you do not already have some programming experience. A suggested pre-requisite is Dr. Chuck's <a target="_blank" href="https://www.youtube.com/watch?v=8DvywoWv6fI">Python for Everybody course</a>. </p>
<p>Dr. Chuck also created a website to go along with this course that has many supplementary resources. <a target="_blank" href="https://www.cc4e.com/">You can access the site here</a>. </p>
<p>Here are the chapters in the book which this course follows:</p>
<ul>
<li>Chapter 0: Introduction</li>
<li>Chapter 1: A Tutorial Introduction</li>
<li>Chapter 2: Types, Operators, and Expressions</li>
<li>Chapter 3: Control Flow</li>
<li>Chapter 4: Functions and Program Structure</li>
<li>Chapter 5: Pointers and Arrays</li>
<li>Chapter 6: Structures</li>
<li>Chapter 7: Input and Output</li>
<li>Chapter 8: The UNIX System Interface</li>
</ul>
<p>Watch the full course below or on <a target="_blank" href="https://youtu.be/j-_s8f5K30I">the freeCodeCamp.org YouTube channel</a> (10-hour watch).</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/j-_s8f5K30I" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
