<?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 - 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 - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Thu, 28 May 2026 16:47:32 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/c-3/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ Boost Your Programming Skills by Reading Git's Code ]]>
                </title>
                <description>
                    <![CDATA[ These days there are plenty of trendy ways to improve your programming skills and knowledge, including: Taking a free or paid online programming course Reading a programming book Picking a personal project and hacking away to learn as you code Fo... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/boost-programming-skills-read-git-code/</link>
                <guid isPermaLink="false">66d45f33f855545810e9345c</guid>
                
                    <category>
                        <![CDATA[ C ]]>
                    </category>
                
                    <category>
                        <![CDATA[ FOSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Git ]]>
                    </category>
                
                    <category>
                        <![CDATA[ open source ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Jacob Stopak ]]>
                </dc:creator>
                <pubDate>Wed, 10 Mar 2021 01:40:53 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/03/Boost-Your-Programming-Skills-by-Reading-Git-s-Code.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>These days there are plenty of trendy ways to improve your programming skills and knowledge, including:</p>
<ul>
<li><p>Taking a free or paid online programming course</p>
</li>
<li><p>Reading a programming book</p>
</li>
<li><p>Picking a personal project and hacking away to learn as you code</p>
</li>
<li><p>Following along with an online tutorial project</p>
</li>
<li><p>Keeping up to date with relevant programming blogs</p>
</li>
</ul>
<p>Each of these methods will appeal to different people, and each one has elements that will definitely make you a better programmer. If you are an intermediate or advanced coder, it is almost certain that you've tried each of these methods at least once.</p>
<p>However, there is another method that the vast majority of developers overlook, which is a shame in my opinion because it has so much to offer. This method is to <strong>learn by reading, analyzing, and understanding existing, high-quality codebases!</strong></p>
<p>We are lucky to live in a time where good code is often accessible for free via high-quality, free-and-open-source (FOSS) projects. And it takes less than a minute to clone down copies of these codebases to our local machines from sites like GitHub or BitBucket.</p>
<p>Furthermore, modern version control systems like Git allow us to view the code at any point in its development history. Clearly there is a wealth of information right in front of our noses!</p>
<p>In this article, we will discuss the original version of Git's code in order to highlight how reading existing code can help boost your coding skills.</p>
<p>We will cover why it's worth learning about Git's code, how to access Git's code, and review some related C programming concepts.</p>
<p>We will provide an overview of Git's original codebase structure and learn how Git's core functionalities are implemented in code.</p>
<p>Finally, we will recommend some next steps for curious developers to continue learning from Git's code and other projects.</p>
<h2 id="heading-why-learn-about-gits-code">Why Learn About Git's Code?</h2>
<p>Git's codebase is an incredible resource for intermediate developers to further their programming knowledge and skills. Here are 7 reasons why it's worth digging into Git's code:</p>
<ol>
<li><p>Git is probably the most popular software dev tool in use today. In short, if you're a developer, you probably use Git. Learning how Git's code works will give you a deeper understanding of an essential tool you work with every day.</p>
</li>
<li><p>Git is interesting! Git is a versatile tool that solves many interesting problems to allow developers to collaborate on code. As a curious human, I thoroughly enjoyed learning more about it.</p>
</li>
<li><p>Git's code is written in the <strong>C</strong> programming language, which offers a great opportunity for developers to branch out into an important language they may not have used much before.</p>
</li>
<li><p>Git makes use of many important programming concepts, including <em>content-addressable databases, file compression/inflation, hash functions, caching,</em> and a <em>simple</em> <em>data model</em>. Git's code illustrates how these concepts can be implemented in a real project.</p>
</li>
<li><p>Git's code and design are <em>elegant.</em> It is a great example of a functional, minimalist codebase that accomplishes its goal in a clear, effective way.</p>
</li>
<li><p>Git's initial commit is small in size – it is made up of only 10 files, containing less than 1,000 total lines of code. This is very small compared to most other projects and is very manageable to understand in a reasonable amount of time.</p>
</li>
<li><p>The code in Git's initial commit can be compiled and executed successfully. This means you can play with and test the original version of Git's code to see how it works.</p>
</li>
</ol>
<p>Now, let's take a look at how to access the original version of Git's code.</p>
<h2 id="heading-how-to-check-out-gits-initial-commit">How to check out Git's initial commit?</h2>
<p>The official copy of Git's codebase is hosted in <a target="_blank" href="https://github.com/git/git">this public GitHub repository</a>. However, I created a fork of Git's codebase and added extensive inline comments to the source code, to help developers easily read through it line by line.</p>
<p>Since I worked off of the very first commit in Git's history, I named this project <strong>Baby Git</strong>. The Baby Git codebase is located in <a target="_blank" href="https://bitbucket.org/jacobstopak/baby-git">this public BitBucket repository</a>.</p>
<p>I recommend cloning the Baby Git codebase to your local machine by running the following command in your terminal:</p>
<pre><code class="lang-sh">git <span class="hljs-built_in">clone</span> https://bitbucket.org/jacobstopak/baby-git.git
</code></pre>
<p>If you want to stick with Git's original codebase (without the extensive comments I added), use this command instead:</p>
<pre><code class="lang-sh">git <span class="hljs-built_in">clone</span> https://github.com/git/git.git
</code></pre>
<p>Browse into the new <code>git</code> directory by running the command <code>cd git</code>. Feel free to poke around the folders and files in here.</p>
<p>You'll quickly notice that in the current version of Git – the version currently checked out in your working directory – that there are <strong>a lot</strong> of files containing a lot of very long and complicated-looking code.</p>
<p>Clearly this current version of Git is too big for a single developer to realistically get acquainted with in a reasonable amount of time.</p>
<p>Let's simplify things by checking out Git's initial commit, using the command:</p>
<pre><code class="lang-sh">git <span class="hljs-built_in">log</span> --reverse
</code></pre>
<p>This shows a list of Git's commit log in reverse order, starting from Git's initial commit. Note that the first commit ID in the list is <code>e83c5163316f89bfbde7d9ab23ca2e25604af290</code>.</p>
<p>Check out the contents of this commit into the working directory by running the command:</p>
<pre><code class="lang-sh">git checkout e83c5163316f89bfbde7d9ab23ca2e25604af290
</code></pre>
<p>This <a target="_blank" href="https://initialcommit.com/blog/what-is-git-head">puts Git into a <em>detached head state</em></a> and places Git's original code files into the working directory.</p>
<p>Now run the <code>ls</code> command to list these files, and note that there are only 10 that actually contain code! (The 11th is just a README). Understanding the code in these files is totally manageable for an intermediate developer!</p>
<p><strong>Note:</strong> If you're using my Baby Git repository, you'll want to run the command <code>git checkout master</code> to abandon the detached head and move back to the tip of the master branch. This will enable you to see all the inline comments describing how Git's code works line by line!</p>
<h2 id="heading-important-c-concepts-that-will-help-you-understand-gits-code">Important C Concepts that Will Help You Understand Git's Code</h2>
<p>Before diving straight into Git's code, it helps to get a refresher on a few C programming concepts that appear throughout the codebase.</p>
<h3 id="heading-c-header-files">C Header Files</h3>
<p>A C header file is a code file ending in the <code>.h</code> extension. Header files are used to store variables, functions, and other C objects that a developer wants to include in multiple <code>.c</code> source code files using the <code>#include "headerFile.h"</code> directive.</p>
<p>If you're familiar with importing files in Python or Java, this is a comparable procedure.</p>
<h3 id="heading-c-function-prototypes-or-function-signatures">C Function Prototypes (or Function Signatures)</h3>
<p>A function prototype or signature tells the C compiler information about a function definition – the function's name, number of argument, types of arguments, and return type – without providing a function body. They help the C compiler identify function properties in situations where the function body appears after the function is called.</p>
<p>Here is an example of a function prototype:</p>
<pre><code class="lang-c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">multiplyNumbers</span><span class="hljs-params">(<span class="hljs-keyword">int</span> a, <span class="hljs-keyword">int</span> b)</span></span>;
</code></pre>
<h3 id="heading-c-macros">C Macros</h3>
<p>A macro in C is essentially a rudimentary variable that is processed before code compilation in a C program. Macros are created using the <code>#define</code> directive, such as:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> TESTMACRO asdf</span>
</code></pre>
<p>This creates a macro called <code>TESTMACRO</code> with a value of <code>asdf</code>. Wherever the placeholder <code>TESTMACRO</code> is used in the code, it will be replaced by the preprocessor (before code compilation) with the value <code>asdf</code>.</p>
<p>Macros are commonly used in a few ways:</p>
<ul>
<li><p>As a true/false switch by checking whether a macro is defined</p>
</li>
<li><p>To store a simple integer or string value to be replaced in the code in multiple locations</p>
</li>
<li><p>To store a simple (usually one-line) code snippet to be replaced in the code in multiple locations</p>
</li>
</ul>
<p>Macros are convenient tools since they enable developers to update a single line of code which influences the behavior of the code in multiple locations.</p>
<h3 id="heading-c-structs">C Structs</h3>
<p>A struct in C is a grouped set of properties that are related to a single object.</p>
<p>You are probably familiar with Classes in languages such as Java and Python. A struct is a predecessor to a class – it can be thought of as a primitive class with no methods.</p>
<pre><code class="lang-c"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">person</span> {</span>

    <span class="hljs-keyword">int</span> person_id;
    <span class="hljs-keyword">char</span> *first_name;
    <span class="hljs-keyword">char</span> *last_name;

};
</code></pre>
<p>This struct represents a person, by grouping together an ID field, along with the person's first and last names. A variable can be instantiated and initialized from this struct as follows:</p>
<pre><code class="lang-c"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">person</span> <span class="hljs-title">jacob</span> = {</span> <span class="hljs-number">1</span>, <span class="hljs-string">"Jacob"</span>, <span class="hljs-string">"Stopak"</span> };
</code></pre>
<p>Struct properties can be retrieved using the dot operator:</p>
<pre><code class="lang-c">jacob.person_id
jacob.first_name
jacob.last_name
</code></pre>
<h3 id="heading-c-pointers">C Pointers</h3>
<p>A pointer is a memory address of a variable – it is the memory address at which the value of that variable is stored.</p>
<p>A pointer to an existing variable can be obtained by using the <code>&amp;</code> symbol, and stored in a pointer variable declared with the <code>*</code> symbol:</p>
<pre><code class="lang-c"><span class="hljs-keyword">int</span> age = <span class="hljs-number">21</span>;

<span class="hljs-keyword">int</span>* age_pointer = &amp;age;
</code></pre>
<p>This snippet defines the variable <code>age</code> and assigns it a value of 21. Then it defines a <em>pointer to an integer</em> called <code>age_pointer</code>, and uses the <code>&amp;</code> to obtain the memory address that the value of the age variable is stored at.</p>
<p>Pointers can be <em>dereferenced</em> (i.e. obtain the value stored at the memory address), using the <code>*</code> as well.</p>
<pre><code class="lang-c"><span class="hljs-keyword">int</span> new_age = *age_pointer + <span class="hljs-number">10</span>;
</code></pre>
<p>Continuing from our previous example, we use the <code>*age_pointer</code> syntax to fetch the value stored in the pointer (21), and add 10 to it. So the <code>new_age</code> variable would contain a value of 31.</p>
<p>Now that our short segue into C programming is completed, let's get back to Git's code.</p>
<h2 id="heading-overview-of-gits-codebase-structure">Overview of Git's Codebase Structure</h2>
<p>There are ten relevant code files that make up Git's initial commit. We'll start by briefly discussing these two:</p>
<ul>
<li><p>Makefile</p>
</li>
<li><p>cache.h</p>
</li>
</ul>
<p>We'll discuss <code>Makefile</code> and <code>cache.h</code> first because they are a bit special.</p>
<p><code>Makefile</code> is a build file that contains a set of commands used to build the other source code files into executables.</p>
<p>When you run the command <code>make all</code> from the command line, the Makefile will compile the source code files and spit out the relevant executables for Git's commands. If you're interested, I wrote an <a target="_blank" href="https://initialcommit.com/blog/Learn-Git-Makefile">in-depth guide on Git's makefile</a>.</p>
<p><strong>Note:</strong> If you actually want to compile Git's code locally, which I recommend you do, you'll need to use my Baby Git version of the code mentioned above. The reason is that I made some tweaks to allow Git's original code to compile on modern operating systems.</p>
<p>Next up is the <code>cache.h</code> file, which is Baby Git's only header file. As mentioned above, the header file defines many of the function signatures, structs, macros, and other settings that are used in the <code>.c</code> source code files. If you're curious, I wrote an <a target="_blank" href="https://initialcommit.com/blog/Learn-Git-Header-Files">in-depth guide on Git's header file</a>.</p>
<p>The remaining eight code files are all <code>.c</code> source code files:</p>
<ul>
<li><p><code>init-db.c</code></p>
</li>
<li><p><code>update-cache.c</code></p>
</li>
<li><p><code>read-cache.c</code></p>
</li>
<li><p><code>write-tree.c</code></p>
</li>
<li><p><code>commit-tree.c</code></p>
</li>
<li><p><code>read-tree.c</code></p>
</li>
<li><p><code>cat-file.c</code></p>
</li>
<li><p><code>show-diff.c</code></p>
</li>
</ul>
<p>Each file (except <code>read-cache.c</code>) is named after the Git command that it contains the code for – some probably look familiar to you. For example, the <code>init-db.c</code> file contains the code for the <code>init-db</code> command used to initialize a new Git repository. As you probably guessed, this was the precursor to the <code>git init</code> command.</p>
<p>In fact, each of these <code>.c</code> files (except <code>read-cache.c</code>) contains the code for one of the original eight Git commands. The build process compiles each of these files and creates an executable file (with matching name) for each one. Once these executables are added to the filesystem path, they can be executed similarly to any modern Git command.</p>
<p>So after compiling the code using the <code>make all</code> command, the following executables are produced:</p>
<ul>
<li><p><code>init-db</code>: Initializes a new Git repository. Equivalent to <code>git init</code>.</p>
</li>
<li><p><code>update-cache</code>: Add a file to the staging index. Equivalent to <code>git add</code>.</p>
</li>
<li><p><code>write-tree</code>: Creates a tree object in the Git repository from the current index contents.</p>
</li>
<li><p><code>commit-tree</code>: Creates a new commit object in the Git repository. Equivalent to <code>git commit</code>.</p>
</li>
<li><p><code>read-tree</code>: Print out the contents of a tree from the Git repository.</p>
</li>
<li><p><code>cat-file</code>: Retrieve the the contents of an object from the Git repository, and store it in a temporary file in the current directory. Equivalent to <code>git show</code>.</p>
</li>
<li><p><code>show-diff</code>: Show the differences between files staged in the index and the current versions of those files as they exist in the filesystem. Equivalent to <code>git diff</code>.</p>
</li>
</ul>
<p>These commands are executed individually in sequence, similar to how modern Git commands are executed as a part of standard development workflows.</p>
<p>The one file we haven't discussed yet is <code>read-cache.c</code>. This file contains a set of helper functions that the other <code>.c</code> source code files use to retrieve information from the Git repository.</p>
<p>Now that we've touched on each of the important files in Git's initial commit, let's discuss some of the core programming concepts that allow Git to function.</p>
<h2 id="heading-implementation-of-gits-core-concepts">Implementation of Git's Core Concepts</h2>
<p>In this section, we'll discuss the following programming concepts that Git uses to work its magic, as well as how they were implemented in Git's original code:</p>
<ul>
<li><p>File compression</p>
</li>
<li><p>Hash function</p>
</li>
<li><p>Objects</p>
</li>
<li><p>Current directory cache (staging area)</p>
</li>
<li><p>Content addressable database (object database)</p>
</li>
</ul>
<h3 id="heading-file-compression">File Compression</h3>
<p>File compression, also know as deflation, is used for storage and performance efficiency in Git. This reduces the size of the files that Git stores on disk and increases the speed of data retrieval when Git needs to transfer these files across a network.</p>
<p>This is important since Git's local and network operations need to be as fast as possible. As a part of the data retrieval process, Git decompresses, or inflates, the files to obtain their content.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/03/image-37.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Source: https://initialcommit.com/blog/Learn-Git-Guidebook-For-Developers-Chapter-2</em></p>
<p>File deflation and inflation were implemented in Git's original code using the popular <code>zlib.h</code> C library. This library contains functions, structures, and properties for compressing and decompressing content. Specifically, <code>Zlib</code> defines a <code>z_stream</code> struct that is used to hold the content that is to be deflated or inflated.</p>
<p>The following <code>zlib</code> functions are used to <em>initialize</em> a stream for deflation or inflation, respectively:</p>
<pre><code class="lang-c"><span class="hljs-comment">/*
 * Initializes the internal `z_stream` state
 * for compression at `level`, which indicates
 * scale of speed versuss compression on a 
 * scale from 0-9. Sourced from &lt;zlib.h&gt;.
 */</span>
deflateInit(z_stream, level);

<span class="hljs-comment">/*
 * Initializes the internal `z_stream` state for
 * decompression. Sourced from &lt;zlib.h&gt;.
 */</span>
inflateInit(z_stream);
</code></pre>
<p>The following <code>zlib</code> functions are used to perform the actual deflation and inflation operations:</p>
<pre><code class="lang-c"><span class="hljs-comment">/*
 * Compresses as much data as possible and stops
 * when the input buffer becomes empty or the
 * output buffer becomes full. Sourced from &lt;zlib.h&gt;.
 */</span>
deflate(z_stream, flush);

<span class="hljs-comment">/*
 * Decompresses as much data as possible and stops
 * when the input buffer becomes empty or the
 * output buffer becomes full. Sourced from &lt;zlib.h&gt;.
 */</span>
inflate(z_stream, flush);
</code></pre>
<p>The actual deflation/inflation process is a bit more complex than this and involves setting several parameters of the compression stream, but we won't go into more detail here.</p>
<p>Next, we'll discuss the concept of hash functions and how they are implemented in Git's original code.</p>
<h3 id="heading-hash-functions">Hash Functions</h3>
<p>A hash function is a function that can easily transform an input into a unique output, but makes it very difficult or impossible to reverse that operation. In other words, it is a <strong>one-way function</strong>. It is not possible with today's technology to use the output of a hash function to deduce the input that was used to generate that output.</p>
<p>Git uses a hash function – specifically the SHA-1 hash function – to generate unique identifiers for the files we tell Git to track.</p>
<p>As developers, we make changes to the code files in the codebase we are working on using a text editor, and at some point we tell Git to track those changes. At this point, Git uses those file changes as inputs for the hash function.</p>
<p>The output to the hash function is called a <strong>hash*</strong>.* The hash is a hexadecimal value 40 characters in length, such as <code>47a013e660d408619d894b20806b1d5086aab03b</code>.</p>
<p><img src="https://initialcommit.com/img/initialcommit/figure5.png" alt="Git hash function" width="894" height="462" loading="lazy"></p>
<p><em>Source: https://initialcommit.com/blog/Learn-Git-Guidebook-For-Developers-Chapter-2</em></p>
<p>Git uses these hashes for various purposes that we will see in the following sections.</p>
<h3 id="heading-objects">Objects</h3>
<p>Git uses a simple data model – a structured set of related objects – to implement its functionality. These objects are the nuggets of information that enable Git to track changes to the files of a codebase. The three types of objects that Git uses are:</p>
<ul>
<li><p>Blob</p>
</li>
<li><p>Tree</p>
</li>
<li><p>Commit</p>
</li>
</ul>
<p>Let's discuss each one in turn.</p>
<h4 id="heading-blob">Blob</h4>
<p>A blob is short for a <strong>B</strong>inary <strong>L</strong>arge <strong>OB</strong>ject. When Git is told to track a file using the <code>update-cache &lt;filename.ext&gt;</code> command, (the predecessor to <code>git add</code>), Git creates a new blob using the compressed contents of that file.</p>
<p>Git takes the content of the file, compresses it using the <code>zlib</code> functions we described above, and uses this compressed content as input to the SHA-1 hash function. This creates a 40 character hash that Git uses to identify the blob in question.</p>
<p>Finally, Git saves the blob as a binary file in a special folder called the <strong>object database</strong> (more on this in a minute). The name of the blob file is the generated hash, and the contents of the blob file are the compressed file contents that were added using <code>update-cache</code>.</p>
<h4 id="heading-tree">Tree</h4>
<p>Tree objects are used to link together multiple blobs that are added to Git at once. They are also used to correlate blobs with file names (and other file metadata like permissions), since blobs don't provide any information besides the hash and compressed binary file content.</p>
<p>For example, if two changed files are added using the <code>update-cache</code> command, a tree will be created containing the hashes of those files, along with the file name that each of those blobs corresponds to.</p>
<p>What Git does next is very interesting, so pay attention. Git uses <strong>the content of the tree itself</strong> as input to the SHA-1 hash function, which generates a 40 character hash. This hash is used to identify the tree object, and Git saves this in the same special folder that blobs are saved in – the object database we'll touch on shortly.</p>
<h4 id="heading-commit">Commit</h4>
<p>You're probably more familiar with commit objects than with blobs and trees. A commit represents a set of file changes saved by Git, along with descriptive information about the change such as a commit message, the author's name, and the timestamp of the commit.</p>
<p>In Git's original code, a commit object is the result of running the <code>commit-tree &lt;tree-hash&gt;</code> command. The resulting commit object includes the specified tree object (which remember, represents a collection of file changes via one or more blobs mapped to their file names), and the descriptive information mentioned in the previous paragraph.</p>
<p>Like blobs and trees, Git identifies the commit by hashing its content using the SHA-1 hash function, and saving it in the object database. Importantly, each commit object also contains the hash of its parent commit. In this way, the commits form a chain that Git can use to represent the history of a project.</p>
<h3 id="heading-current-directory-cache-staging-area">Current Directory Cache (Staging Area)</h3>
<p>You probably know Git's staging area as the place your changed files go after using the <code>git add</code> command, patiently waiting to be committed using <code>git commit</code>. But what exactly is the staging area?</p>
<p>In Git's original version, the staging area is called the <strong>current directory cache</strong>. The current directory cache is just a binary file stored in the repository at the path <code>.dircache/index</code>.</p>
<p>As mentioned in the previous section, after changed files are added to Git using the <code>update-cache</code> (<code>git add</code>) command, Git calculates the blob and tree objects associated with those changes. The generated tree object associated with the staged files is added to the <code>index</code> file.</p>
<p>It is called a <strong>cache</strong> because it is just a temporary storage location for the staged changes to reside before being committed. When the user makes a commit by running the <code>commit-tree</code> command, the tree from the current directory cache can be supplied. It also includes other commit information like the commit message for Git to create a new commit object with.</p>
<p>At that point the <code>index</code> file is simply removed to make room for new changes to be staged.</p>
<h3 id="heading-content-addressable-database-object-database">Content Addressable Database (Object Database)</h3>
<p>The object database is Git's primary storage location. This is where all the objects we discussed above – blobs, trees, and commits – are stored. In Git's original version, the object database is simply a directory at the path <code>.dircache/objects/</code>.</p>
<p>When Git creates objects through operations such as <code>update-cache</code>, <code>write-tree</code>, and <code>commit-tree</code>, (the predecessors of <code>git add</code> and <code>git commit</code>), these objects are compressed, hashed, and stored in the object database.</p>
<p>The name of each object is the hash of its content, hence why the object database is also called a <strong>content addressable database*</strong>.* Each piece of content (blob, tree, or commit) is stored and retrieved based on an identifier generated from the content itself.</p>
<p>The modern version of Git works very much the same way. The difference is that storage formats have been optimized to use more efficient methods (especially related to data transfer over networks), but the basic principles are the same.</p>
<h2 id="heading-summary">Summary</h2>
<p>In this article, we discussed the original version of Git's code in order to highlight how reading existing code can help boost your coding skills.</p>
<p>We covered the reasons Git is a great project to learn from in this way, how to access Git's code, and reviewed some related C programming concepts. Finally, we provided an overview of Git's original codebase structure and dove into some concepts that Git's code relies on.</p>
<h2 id="heading-next-steps">Next Steps</h2>
<p>If you're interested in learning more about Git's code, <a target="_blank" href="https://initialcommit.com/store/baby-git-guidebook-for-developers">we wrote a guidebook you can check out here</a>. This book dives into Git's original C code in detail and directly explains how the code works.</p>
<p>I encourage any and all developers to explore the open-source community to try and find quality projects that interest you. Those projects will have codebases that you can clone down in a matter of minutes.</p>
<p>Take some time to poke around the code, and you might learn something you never expected to find.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ File Handling in C — How to Open, Close, and Write to Files ]]>
                </title>
                <description>
                    <![CDATA[ If you’ve written the C helloworld program before, you already know basic file I/O in C: /* A simple hello world in C. */ #include <stdlib.h> // Import IO functions. #include <stdio.h> int main() {     // This printf is where all the file IO magic ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/file-handling-in-c-how-to-open-close-and-write-to-files/</link>
                <guid isPermaLink="false">66c34a54c8f6b2d81069b359</guid>
                
                    <category>
                        <![CDATA[ C ]]>
                    </category>
                
                    <category>
                        <![CDATA[ toothbrush ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Sat, 01 Feb 2020 00:00:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9d31740569d1a4ca3667.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>If you’ve written the C <code>helloworld</code> program before, you already know basic file I/O in C:</p>
<pre><code class="lang-c"><span class="hljs-comment">/* A simple hello world in C. */</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-comment">// Import IO functions.</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-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-comment">// This printf is where all the file IO magic happens!</span>
    <span class="hljs-comment">// How exciting!</span>
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Hello, world!\n"</span>);
    <span class="hljs-keyword">return</span> EXIT_SUCCESS;
}
</code></pre>
<p>File handling is one of the most important parts of programming. In C, we use a structure pointer of a file type to declare a file:</p>
<pre><code class="lang-c">FILE *fp;
</code></pre>
<p>C provides a number of build-in function to perform basic file operations:</p>
<ul>
<li><code>fopen()</code> - create a new file or open a existing file</li>
<li><code>fclose()</code> - close a file</li>
<li><code>getc()</code> - reads a character from a file</li>
<li><code>putc()</code> - writes a character to a file</li>
<li><code>fscanf()</code> - reads a set of data from a file</li>
<li><code>fprintf()</code> - writes a set of data to a file</li>
<li><code>getw()</code> - reads a integer from a file</li>
<li><code>putw()</code> - writes a integer to a file</li>
<li><code>fseek()</code> - set the position to desire point</li>
<li><code>ftell()</code> - gives current position in the file</li>
<li><code>rewind()</code> - set the position to the beginning point</li>
</ul>
<h3 id="heading-opening-a-file"><strong>Opening a file</strong></h3>
<p>The <code>fopen()</code> function is used to create a file or open an existing file:</p>
<pre><code class="lang-c">fp = fopen(<span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> filename,<span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> mode);
</code></pre>
<p>There are many modes for opening a file: </p>
<ul>
<li><code>r</code> - open a file in read mode</li>
<li><code>w</code> - opens or create a text file in write mode</li>
<li><code>a</code> - opens a file in append mode</li>
<li><code>r+</code> - opens a file in both read and write mode</li>
<li><code>a+</code> - opens a file in both read and write mode</li>
<li><code>w+</code> - opens a file in both read and write mode</li>
</ul>
<p>Here’s an example of reading data from a file and writing to it:</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;conio.h&gt;</span></span>
main()
{
FILE *fp;
<span class="hljs-keyword">char</span> ch;
fp = fopen(<span class="hljs-string">"hello.txt"</span>, <span class="hljs-string">"w"</span>);
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"Enter data"</span>);
<span class="hljs-keyword">while</span>( (ch = getchar()) != EOF) {
  putc(ch,fp);
}
fclose(fp);
fp = fopen(<span class="hljs-string">"hello.txt"</span>, <span class="hljs-string">"r"</span>);

<span class="hljs-keyword">while</span>( (ch = getc(fp)! = EOF)
  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%c"</span>,ch);

fclose(fp);
}
</code></pre>
<p>Now you might be thinking, "This just prints text to the screen. How is this file IO?” </p>
<p>The answer isn’t obvious at first, and needs some understanding about the UNIX system. In a UNIX system, everything is treated as a file, meaning you can read from and write to it. </p>
<p>This means that your printer can be abstracted as a file since all you do with a printer is write with it. It is also useful to think of these files as streams, since as you’ll see later, you can redirect them with the shell.</p>
<p>So how does this relate to <code>helloworld</code> and file IO?</p>
<p>When you call <code>printf</code>, you are really just writing to a special file called <code>stdout</code>, short for <strong>standard output</strong>. <code>stdout</code> represents the standard output as decided by your shell, which is usually the terminal. This explains why it printed to your screen.</p>
<p>There are two other streams (i.e. files) that are available to you with effort, <code>stdin</code> and <code>stderr</code>. <code>stdin</code> represents the <strong>standard input</strong>, which your shell usually attaches to the keyboard. <code>stderr</code> represents the <strong>standard error</strong> output, which your shell usually attaches to the terminal.</p>
<h3 id="heading-rudimentary-file-io-or-how-i-learned-to-lay-pipes"><strong>Rudimentary File IO, or How I Learned to Lay Pipes</strong></h3>
<p>Enough theory, let’s get down to business by writing some code! The easiest way to write to a file is to redirect the output stream using the output redirect tool, <code>&gt;</code>. </p>
<p>If you want to append, you can use <code>&gt;&gt;</code>:</p>
<pre><code class="lang-bash"><span class="hljs-comment"># This will output to the screen...</span>
./helloworld
<span class="hljs-comment"># ...but this will write to a file!</span>
./helloworld &gt; hello.txt
</code></pre>
<p>The contents of <code>hello.txt</code> will, not surprisingly, be</p>
<pre><code class="lang-text">Hello, world!
</code></pre>
<p>Say we have another program called <code>greet</code>, similar to <code>helloworld</code>, that greets you with a given <code>name</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-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-comment">// Initialize an array to hold the name.</span>
    <span class="hljs-keyword">char</span> name[<span class="hljs-number">20</span>];
    <span class="hljs-comment">// Read a string and save it to name.</span>
    <span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%s"</span>, name);
    <span class="hljs-comment">// Print the greeting.</span>
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Hello, %s!"</span>, name);
    <span class="hljs-keyword">return</span> EXIT_SUCCESS;
}
</code></pre>
<p>Instead of reading from the keyboard, we can redirect <code>stdin</code> to read from a file using the <code>&lt;</code> tool:</p>
<pre><code class="lang-bash"><span class="hljs-comment"># Write a file containing a name.</span>
<span class="hljs-built_in">echo</span> Kamala &gt; name.txt
<span class="hljs-comment"># This will read the name from the file and print out the greeting to the screen.</span>
./greet &lt; name.txt
<span class="hljs-comment"># ==&gt; Hello, Kamala!</span>
<span class="hljs-comment"># If you wanted to also write the greeting to a file, you could do so using "&gt;".</span>
</code></pre>
<p>Note: these redirection operators are in <code>bash</code> and similar shells.</p>
<h3 id="heading-the-real-deal"><strong>The Real Deal</strong></h3>
<p>The above methods only worked for the most basic of cases. If you wanted to do bigger and better things, you will probably want to work with files from within C instead of through the shell. </p>
<p>To accomplish this, you will use a function called <code>fopen</code>. This function takes two string parameters, the first being the file name and the second being the mode. </p>
<p>The mode are basically permissions, so <code>r</code> for read, <code>w</code> for write, <code>a</code> for append. You can also combine them, so <code>rw</code> would mean you could read and write to the file. There are more modes, but these are the most commonly used.</p>
<p>After you have a <code>FILE</code> pointer, you can use basically the same IO commands you would’ve used, except that you have to prefix them with <code>f</code> and the first argument will be the file pointer. For example, <code>printf</code>’s file version is <code>fprintf</code>.</p>
<p>Here’s a program called <code>greetings</code> that reads a from a file containing a list of names and write the greetings to another file:</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-comment">// Create file pointers.</span>
    FILE *names = fopen(<span class="hljs-string">"names.txt"</span>, <span class="hljs-string">"r"</span>);
    FILE *greet = fopen(<span class="hljs-string">"greet.txt"</span>, <span class="hljs-string">"w"</span>);

    <span class="hljs-comment">// Check that everything is OK.</span>
    <span class="hljs-keyword">if</span> (!names || !greet) {
        <span class="hljs-built_in">fprintf</span>(<span class="hljs-built_in">stderr</span>, <span class="hljs-string">"File opening failed!\n"</span>);
        <span class="hljs-keyword">return</span> EXIT_FAILURE;
    }

    <span class="hljs-comment">// Greetings time!</span>
    <span class="hljs-keyword">char</span> name[<span class="hljs-number">20</span>];
    <span class="hljs-comment">// Basically keep on reading untill there's nothing left.</span>
    <span class="hljs-keyword">while</span> (<span class="hljs-built_in">fscanf</span>(names, <span class="hljs-string">"%s\n"</span>, name) &gt; <span class="hljs-number">0</span>) {
        <span class="hljs-built_in">fprintf</span>(greet, <span class="hljs-string">"Hello, %s!\n"</span>, name);
    }

    <span class="hljs-comment">// When reached the end, print a message to the terminal to inform the user.</span>
    <span class="hljs-keyword">if</span> (feof(names)) {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Greetings are done!\n"</span>);
    }

    <span class="hljs-keyword">return</span> EXIT_SUCCESS;
}
</code></pre>
<p>Suppose <code>names.txt</code> contains the following:</p>
<pre><code class="lang-text">Kamala
Logan
Carol
</code></pre>
<p>Then after running <code>greetings</code> the file <code>greet.txt</code> will contain:</p>
<pre><code class="lang-text">Hello, Kamala!
Hello, Logan!
Hello, Carol!
</code></pre>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Format Specifiers in C ]]>
                </title>
                <description>
                    <![CDATA[ Format specifiers define the type of data to be printed on standard output. You need to use format specifiers whether you're printing formatted output with printf() or  accepting input with scanf(). Some of the % specifiers that you can use in ANSI C... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/format-specifiers-in-c/</link>
                <guid isPermaLink="false">66c34ab20f58901a6209177f</guid>
                
                    <category>
                        <![CDATA[ C ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ programming languages ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Wed, 22 Jan 2020 21:32:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9d9c740569d1a4ca38a4.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Format specifiers define the type of data to be printed on standard output. You need to use format specifiers whether you're printing formatted output with <code>printf()</code> or  accepting input with <code>scanf()</code>.</p>
<p>Some of the % specifiers that you can use in ANSI C are as follows:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Specifier</td><td>Used For</td></tr>
</thead>
<tbody>
<tr>
<td>%c</td><td>a single character</td></tr>
<tr>
<td>%s</td><td>a string</td></tr>
<tr>
<td>%hi</td><td>short (signed)</td></tr>
<tr>
<td>%hu</td><td>short (unsigned)</td></tr>
<tr>
<td>%Lf</td><td>long double</td></tr>
<tr>
<td>%n</td><td>prints nothing</td></tr>
<tr>
<td>%d</td><td>a decimal integer (assumes base 10)</td></tr>
<tr>
<td>%i</td><td>a decimal integer (detects the base automatically)</td></tr>
<tr>
<td>%o</td><td>an octal (base 8) integer</td></tr>
<tr>
<td>%x</td><td>a hexadecimal (base 16) integer</td></tr>
<tr>
<td>%p</td><td>an address (or pointer)</td></tr>
<tr>
<td>%f</td><td>a floating point number for floats</td></tr>
<tr>
<td>%u</td><td>int unsigned decimal</td></tr>
<tr>
<td>%e</td><td>a floating point number in scientific notation</td></tr>
<tr>
<td>%E</td><td>a floating point number in scientific notation</td></tr>
<tr>
<td>%%</td><td>the % symbol</td></tr>
</tbody>
</table>
</div><h2 id="heading-examples">Examples:</h2>
<h3 id="heading-c-single-character-format-specifier"><code>%c</code> single character format specifier:</h3>
<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> first_ch = <span class="hljs-string">'f'</span>; 
  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%c\n"</span>, first_ch); 
  <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; 
}
</code></pre>
<p><strong>Output:</strong></p>
<pre><code>f
</code></pre><h3 id="heading-s-string-format-specifier"><code>%s</code> string format specifier:</h3>
<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> str[] = <span class="hljs-string">"freeCodeCamp"</span>; 
  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%s\n"</span>, str); 
  <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; 
}
</code></pre>
<p><strong>Output:</strong></p>
<pre><code>freeCodeCamp
</code></pre><h3 id="heading-character-input-with-the-c-format-specifier">Character input with the <code>%c</code> format specifier:</h3>
<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> user_ch; 
  <span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%c"</span>, &amp;user_ch); <span class="hljs-comment">// user inputs Y</span>
  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%c\n"</span>, user_ch); 
  <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; 
}
</code></pre>
<p><strong>Output:</strong></p>
<pre><code>Y
</code></pre><h3 id="heading-string-input-with-the-s-format-specifier">String input with the <code>%s</code> format specifier:</h3>
<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> user_str[<span class="hljs-number">20</span>]; 
  <span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%s"</span>, user_str); <span class="hljs-comment">// user inputs fCC</span>
  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%s\n"</span>, user_str); 
  <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; 
}
</code></pre>
<p><strong>Output:</strong></p>
<pre><code>fCC
</code></pre><h3 id="heading-d-and-i-decimal-integer-format-specifiers"><code>%d</code> and <code>%i</code> decimal integer format specifiers:</h3>
<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> found = <span class="hljs-number">2015</span>, curr = <span class="hljs-number">2020</span>; 
  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d\n"</span>, found); 
  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%i\n"</span>, curr); 
  <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; 
}
</code></pre>
<p><strong>Output:</strong></p>
<pre><code><span class="hljs-number">2015</span>
<span class="hljs-number">2020</span>
</code></pre><h3 id="heading-f-and-e-floating-point-number-format-specifiers"><code>%f</code> and <code>%e</code> floating point number format specifiers:</h3>
<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">float</span> num = <span class="hljs-number">19.99</span>; 
  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%f\n"</span>, num); 
  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%e\n"</span>, num); 
  <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; 
}
</code></pre>
<p><strong>Output:</strong></p>
<pre><code><span class="hljs-number">19.990000</span>
<span class="hljs-number">1.999000e+01</span>
</code></pre><h3 id="heading-o-octal-integer-format-specifier"><code>%o</code> octal integer format specifier:</h3>
<pre><code>#include &lt;stdio.h&gt; 

int main() { 
  int num = <span class="hljs-number">31</span>; 
  printf(<span class="hljs-string">"%o\n"</span>, num); 
  <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; 
}
</code></pre><p><strong>Output:</strong></p>
<pre><code><span class="hljs-number">37</span>
</code></pre><h3 id="heading-x-hexadecimal-integer-format-specifier"><code>%x</code> hexadecimal integer format specifier:</h3>
<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> c = <span class="hljs-number">28</span>; 
  <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%x\n"</span>, c); 
  <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; 
}
</code></pre>
<p><strong>Output:</strong></p>
<pre><code><span class="hljs-number">1</span>c
</code></pre> ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
