<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://neelpatel05.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://neelpatel05.github.io/" rel="alternate" type="text/html" /><updated>2026-11-24T08:19:31+00:00</updated><id>https://neelpatel05.github.io/feed.xml</id><title type="html">Learned Lessons</title><entry><title type="html">Know your data types!</title><link href="https://neelpatel05.github.io/2026/11/22/know-your-data-types.html" rel="alternate" type="text/html" title="Know your data types!" /><published>2026-11-22T00:00:00+00:00</published><updated>2026-11-22T00:00:00+00:00</updated><id>https://neelpatel05.github.io/2026/11/22/know-your-data-types</id><content type="html" xml:base="https://neelpatel05.github.io/2026/11/22/know-your-data-types.html"><![CDATA[<h1 id="know-your-data-types">Know your data types!</h1>

<p>In this blog, I’ll be diving deep into data types, using the C programming language predominantly to demonstrate them. We will also use a debugging utility called gdb, which is great for dissecting compiled programs and inspecting memory to analyze how this data is stored.</p>

<h2 id="what-are-data-types">What are data types?</h2>
<p>Almost every computer program we write involves manipulating some kind of data. At a low level, everything in computer memory is just <code class="language-plaintext highlighter-rouge">0</code>s and <code class="language-plaintext highlighter-rouge">1</code>s. However, the computer needs to know what kind of data those <code class="language-plaintext highlighter-rouge">0</code>s and <code class="language-plaintext highlighter-rouge">1</code>s represent in order to perform the right operations on them. This is where data types come into the picture.</p>

<p>Some common primitive data types are:</p>
<ol>
  <li><strong>Character</strong> (single letters or symbols)</li>
  <li><strong>Integers</strong> (whole numbers, natural numbers, etc.)</li>
  <li><strong>Decimals</strong> (fractions, aka <code class="language-plaintext highlighter-rouge">float</code> in C)</li>
</ol>

<p>Using these building blocks, we can create additional data types. For example:</p>
<ol>
  <li><strong>Strings</strong> (characters grouped together)</li>
  <li><strong>Arrays</strong> (lists of words, numbers, or characters)</li>
  <li><strong>Booleans</strong> (true/false values often represented as <code class="language-plaintext highlighter-rouge">1</code> or <code class="language-plaintext highlighter-rouge">0</code>)</li>
</ol>

<h2 id="bits-bytes-and-memory-addresses">Bits, bytes, and memory addresses</h2>

<p>Computers work in binary i.e <code class="language-plaintext highlighter-rouge">0</code> or <code class="language-plaintext highlighter-rouge">1</code>. Because binary has 2 states, computer memory is organized in powers of 2 (2¹, 2², 2³, 2⁴, 2⁵, etc.). For example, a “Yes” can be represented by <code class="language-plaintext highlighter-rouge">1</code> and a “No” by <code class="language-plaintext highlighter-rouge">0</code>. How much memory do you need to store a single “Yes” or “No”? Just <strong>1 bit</strong>. A bit is the smallest unit of information a computer can store.</p>

<p>However, computers don’t access single bits on their own in memory. Instead, they group them into <strong>bytes</strong>. What is a byte? Simply put, 8 bits grouped together make 1 byte. Data in memory is organized in bytes because the computer assigns a unique address to each byte in RAM.</p>

<p>To keep it simple, take the number <code class="language-plaintext highlighter-rouge">1</code>. Its 1-byte binary representation in a computer is <code class="language-plaintext highlighter-rouge">00000001</code>. Similarly, the number <code class="language-plaintext highlighter-rouge">2</code> is <code class="language-plaintext highlighter-rouge">00000010</code>. These bytes (<code class="language-plaintext highlighter-rouge">00000001</code> and <code class="language-plaintext highlighter-rouge">00000010</code>) are stored at unique addresses (<code class="language-plaintext highlighter-rouge">x</code> and <code class="language-plaintext highlighter-rouge">y</code>) in RAM.</p>

<h2 id="viewing-primitive-data-types-in-memory">Viewing primitive data types in memory</h2>

<p>On a 64-bit computer, basic C data types typically have the following sizes in memory:</p>

<table>
  <thead>
    <tr>
      <th style="text-align: left">Data Type</th>
      <th style="text-align: left">Size</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td style="text-align: left"><code class="language-plaintext highlighter-rouge">char</code></td>
      <td style="text-align: left">1 byte</td>
    </tr>
    <tr>
      <td style="text-align: left"><code class="language-plaintext highlighter-rouge">int</code></td>
      <td style="text-align: left">4 bytes</td>
    </tr>
    <tr>
      <td style="text-align: left"><code class="language-plaintext highlighter-rouge">float</code></td>
      <td style="text-align: left">4 bytes</td>
    </tr>
  </tbody>
</table>

<p>Let’s view the sizes of these basic data types via a C program. The following program declares three variables <code class="language-plaintext highlighter-rouge">x</code>, <code class="language-plaintext highlighter-rouge">y</code>, and <code class="language-plaintext highlighter-rouge">z</code>, whose data types are <code class="language-plaintext highlighter-rouge">char</code>, <code class="language-plaintext highlighter-rouge">int</code>, and <code class="language-plaintext highlighter-rouge">float</code>, respectively. The <code class="language-plaintext highlighter-rouge">sizeof</code> operator in C retrieves the amount of memory in bytes allocated for a variable or data type.</p>

<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#include</span> <span class="cpf">&lt;stdio.h&gt;</span><span class="cp">
</span>
<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span><span class="o">**</span> <span class="n">argv</span><span class="p">)</span>
<span class="p">{</span>
	<span class="kt">char</span> <span class="n">x</span> <span class="o">=</span> <span class="sc">'a'</span><span class="p">;</span>
	<span class="kt">int</span> <span class="n">y</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
	<span class="kt">float</span> <span class="n">z</span> <span class="o">=</span> <span class="mi">2</span><span class="p">.</span><span class="mi">5</span><span class="p">;</span>

	<span class="n">printf</span><span class="p">(</span><span class="s">"Size of char x: %zu</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">x</span><span class="p">));</span>
	<span class="n">printf</span><span class="p">(</span><span class="s">"Size of integer y: %zu</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">y</span><span class="p">));</span>
	<span class="n">printf</span><span class="p">(</span><span class="s">"Size of float z: %zu</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">z</span><span class="p">));</span>

	<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>

<p><strong>Output:</strong></p>

<div class="language-text highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Size of char x: 1
Size of integer y: 4
Size of float z: 4
</code></pre></div></div>

<p>The above output showcases that <code class="language-plaintext highlighter-rouge">char</code>, <code class="language-plaintext highlighter-rouge">int</code>, and <code class="language-plaintext highlighter-rouge">float</code> are 1, 4, and 4 bytes, respectively.</p>

<p>Let’s dive deep to prove this output by dissecting the program using gdb and inspecting the memory for each of the three primitive data types. Let’s begin the analysis by getting the memory addresses of the variables:</p>

<pre><code class="language-gdb">(gdb) p &amp;x
\$1 = 0x7ffffffc0437 "a\001"
(gdb) p &amp;y
\$2 = (int *) 0x7ffffffc0438
(gdb) p &amp;z
\$3 = (float *) 0x7ffffffc043c
</code></pre>

<h3 id="character">Character</h3>

<p>Let’s examine 9 bytes of memory starting from the address of <code class="language-plaintext highlighter-rouge">x</code> (note that on little-endian architectures like x86-64, multi-byte numbers store their least significant byte first):</p>

<pre><code class="language-gdb">(gdb) x/9xb &amp;x
0x7ffffffc0437:	0x61	0x01	0x00	0x00	0x00	0x00	0x00	0x20
0x7ffffffc043f:	0x40
</code></pre>

<p>As shown above, the value stored at <code class="language-plaintext highlighter-rouge">0x7ffffffc0437</code> is <code class="language-plaintext highlighter-rouge">0x61</code> (ASCII <code class="language-plaintext highlighter-rouge">'a'</code>). If we inspect the memory at <code class="language-plaintext highlighter-rouge">0x7ffffffc0437 + 0x1</code> (<code class="language-plaintext highlighter-rouge">0x7ffffffc0438</code>), we find the value <code class="language-plaintext highlighter-rouge">1</code> (<code class="language-plaintext highlighter-rouge">0x01 0x00 0x00 0x00</code>), which belongs to variable <code class="language-plaintext highlighter-rouge">y</code>:</p>

<pre><code class="language-gdb">(gdb) x/4xb 0x7ffffffc0437+1
0x7ffffffc0438:	0x01	0x00	0x00	0x00
</code></pre>

<p>Because <code class="language-plaintext highlighter-rouge">y</code> begins immediately 1 byte after <code class="language-plaintext highlighter-rouge">x</code>, the difference between their addresses is 1 byte—proving that the <code class="language-plaintext highlighter-rouge">char</code> variable <code class="language-plaintext highlighter-rouge">x</code> occupies <strong>1 byte</strong>.</p>

<h3 id="integer">Integer</h3>

<p>Next, let’s visualize the size of the <code class="language-plaintext highlighter-rouge">int</code> data type by examining the 8 bytes starting at the address of <code class="language-plaintext highlighter-rouge">y</code>:</p>

<pre><code class="language-gdb">(gdb) x/8xb &amp;y
0x7ffffffc0438:	0x01	0x00	0x00	0x00	0x00	0x00	0x20	0x40
</code></pre>

<p>If we examine the 4 bytes starting at the address of <code class="language-plaintext highlighter-rouge">z</code>, we get the IEEE 754 representation of <code class="language-plaintext highlighter-rouge">2.5</code>:</p>

<pre><code class="language-gdb">(gdb) x/4xb &amp;z
0x7ffffffc043c:	0x00	0x00	0x20	0x40
</code></pre>

<p>Subtracting the address of <code class="language-plaintext highlighter-rouge">y</code> from the address of <code class="language-plaintext highlighter-rouge">z</code> gives:</p>

<pre><code class="language-gdb">(gdb) p/x 0x7ffffffc043c - 0x7ffffffc0438
\$4 = 0x4
</code></pre>

<p>Since <code class="language-plaintext highlighter-rouge">z</code> starts immediately after <code class="language-plaintext highlighter-rouge">y</code> with no alignment padding between them, this 4-byte offset proves that <code class="language-plaintext highlighter-rouge">int</code> occupies <strong>4 bytes</strong> in memory.</p>

<h3 id="float">Float</h3>

<p>To prove the size of the <code class="language-plaintext highlighter-rouge">float</code> data type, we will use pointer arithmetic. If we add <code class="language-plaintext highlighter-rouge">1</code> to a memory address of a variable, the memory address advances by the exact byte size of data type of that variable. Let’s re-run the program and examine the address of <code class="language-plaintext highlighter-rouge">z</code>:</p>

<pre><code class="language-gdb">(gdb) p &amp;z
\$1 = (float *) 0x7ffffffc03cc
(gdb) p &amp;z+1
\$2 = (float *) 0x7ffffffc03d0
(gdb) p/x 0x7ffffffc03d0-0x7ffffffc03cc
\$3 = 0x4
</code></pre>

<p>The address of <code class="language-plaintext highlighter-rouge">z</code> is <code class="language-plaintext highlighter-rouge">0x7ffffffc03cc</code>, and when we added <code class="language-plaintext highlighter-rouge">1</code> to <code class="language-plaintext highlighter-rouge">&amp;z</code>, the address advanced to <code class="language-plaintext highlighter-rouge">0x7ffffffc03d0</code>. Subtracting those addresses gives <code class="language-plaintext highlighter-rouge">0x4</code>, which proves the size of the <code class="language-plaintext highlighter-rouge">float</code> data type is <strong>4 bytes</strong> and shows how adding <code class="language-plaintext highlighter-rouge">1</code> to a pointer increases the address by the size of that specific data type.</p>

<h2 id="viewing-composite-data-types-in-memory">Viewing composite data types in memory</h2>

<h3 id="arrays">Arrays</h3>

<p>Arrays are a composite data type built from primitive data types like char, int, and float. At a low level, each element of an array is stored in memory the exact same way a standalone primitive variable is. The only difference is that all elements are grouped together contiguously starting from a single base memory address.</p>

<p>For example, in the following C program, <code class="language-plaintext highlighter-rouge">a</code> is declared as an int array and <code class="language-plaintext highlighter-rouge">b</code> is declared as a char array. Every element inside a occupies the standard size of an int (4 bytes), and every element inside b occupies the standard size of a char (1 byte):</p>

<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#include</span><span class="cpf">&lt;stdio.h&gt;</span><span class="cp">
</span>
<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span><span class="o">**</span> <span class="n">argv</span><span class="p">)</span>
<span class="p">{</span>
	<span class="kt">int</span> <span class="n">a</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">};</span>
	<span class="kt">char</span> <span class="n">b</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span><span class="sc">'a'</span><span class="p">,</span><span class="sc">'b'</span><span class="p">,</span><span class="sc">'c'</span><span class="p">,</span><span class="sc">'d'</span><span class="p">,</span><span class="sc">'e'</span><span class="p">,</span><span class="sc">'f'</span><span class="p">};</span>

	<span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>

<p>To understand how array elements are laid out in memory, let’s inspect both variable <code class="language-plaintext highlighter-rouge">a</code> and <code class="language-plaintext highlighter-rouge">b</code> using gdb. First, we check the starting memory addresses and data types of each array.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>(gdb) p &amp;a
$1 = (int (*)[5]) 0xffffb3c00530
(gdb) p &amp;b
$2 = (char (*)[6]) 0xffffb3c00552
</code></pre></div></div>

<p>As shown in the output above, the starting address of a is <code class="language-plaintext highlighter-rouge">0xffffb3c00530</code> and b is <code class="language-plaintext highlighter-rouge">0xffffb3c00552</code>. Because <code class="language-plaintext highlighter-rouge">a</code> is an array of 5 integers, we examine 5 words (1 word = 4 bytes) starting from <code class="language-plaintext highlighter-rouge">0xffffb3c00530</code>. In total, <code class="language-plaintext highlighter-rouge">a</code> occupies 20 bytes in RAM with each individual element is stored just like a standalone int, simply grouped contiguously starting from that base address.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>(gdb) x/5xw 0xffffb3c00530
0xffffb3c00530:	0x00000001	0x00000002	0x00000003	0x00000004
0xffffb3c00540:	0x00000005
</code></pre></div></div>

<p>To inspect each element individually, we dereference <code class="language-plaintext highlighter-rouge">&amp;a</code> (<code class="language-plaintext highlighter-rouge">*&amp;a</code>). In C, dereferencing a pointer to an array yields the array itself, which immediately decays into a pointer to its first element (int *). This changes the pointer arithmetic stride from the size of the entire array (20 bytes) to the size of a single int (4 bytes) so each <code class="language-plaintext highlighter-rouge">+ 1</code> advances the address by 4 bytes as shown below.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>(gdb) x/1xw *&amp;a
0xffffb3c00530:	0x00000001
(gdb) x/1xw *&amp;a+1
0xffffb3c00534:	0x00000002
(gdb) x/1xw *&amp;a+2
0xffffb3c00538:	0x00000003
(gdb) x/1xw *&amp;a+3
0xffffb3c0053c:	0x00000004
(gdb) x/1xw *&amp;a+4
0xffffb3c00540:	0x00000005
(gdb) x/1xw *&amp;a+5
0xffffb3c00544:	0x00000000
</code></pre></div></div>

<p>Repeating the same process on variable <code class="language-plaintext highlighter-rouge">b</code>. But switching the format specifier from <code class="language-plaintext highlighter-rouge">w</code> (4-byte word) to <code class="language-plaintext highlighter-rouge">b</code> (1-byte unit).  Because <code class="language-plaintext highlighter-rouge">*&amp;b</code> decays to a <code class="language-plaintext highlighter-rouge">char *</code>, adding <code class="language-plaintext highlighter-rouge">+ 1</code> advances the address by just 1 byte at a time. This shows how the underlying data type controls the step size.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>(gdb) x/1xb *&amp;b
0xffffb3c00552:	0x61
(gdb) x/1xb *&amp;b+1
0xffffb3c00553:	0x62
(gdb) x/1xb *&amp;b+2
0xffffb3c00554:	0x63
(gdb) x/1xb *&amp;b+3
0xffffb3c00555:	0x64
(gdb) x/1xb *&amp;b+4
0xffffb3c00556:	0x65
(gdb) x/1xb *&amp;b+5
0xffffb3c00557:	0x66
</code></pre></div></div>

<h3 id="strings">Strings</h3>

<p>In C, there is no separate primitive data type called string. Instead, a string is simply a char array terminated by a special null character (‘\0’, or 0x00 in hex). This null byte tells C library functions (like printf and strlen) where the string ends in memory.</p>

<p>Let’s see this behavior in the following example, where we declare a with “hello!” inside double quotes. At first glance, “hello!” looks like shorthand for initializing a character array with curly braces ({‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘!’}), just written with quotes instead.</p>

<p>However, there is an important nuance:</p>
<ul>
  <li>When you initialize with curly braces (<code class="language-plaintext highlighter-rouge">char a[] = {'h', 'e', 'l', 'l', 'o', '!'};</code>), the compiler allocates only those 6 bytes and does not add a null byte at the end.</li>
  <li>When you initialize with double quotes (<code class="language-plaintext highlighter-rouge">char a[] = "hello!";</code>), the compiler automatically appends a null byte (‘\0’) at the end, allocating 7 bytes in total.</li>
</ul>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include&lt;stdio.h&gt;
#include&lt;string.h&gt;

int main(int argc, char** argv)
{
	char a[] = "hello!";
	return 1;
}
</code></pre></div></div>

<p>Let’s inspect a in gdb to see all 7 bytes in memory:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>(gdb) p &amp;a
$1 = (char (*)[7]) 0xffffa1c00551
</code></pre></div></div>

<p>Notice the type gdb reports <code class="language-plaintext highlighter-rouge">char (*)[7]</code>. Even though “hello!” has only 6 visible characters, the array size is 7 bytes. Dumping all 7 bytes in hexadecimal with x/7xb reveals the trailing 0x00 at the end:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>(gdb) x/7xb *&amp;a
0xffffa1c00551:	0x68	0x65	0x6c	0x6c	0x6f	0x21	0x00
</code></pre></div></div>

<p>Finally, we can step through each byte one at a time using x/1cb (examine 1 byte formatted as a character) to see both the ASCII value and character stored at every offset.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>(gdb) x/1cb *&amp;a
0xffffa1c00551:	104 'h'
(gdb) x/1cb *&amp;a+1
0xffffa1c00552:	101 'e'
(gdb) x/1cb *&amp;a+2
0xffffa1c00553:	108 'l'
(gdb) x/1cb *&amp;a+3
0xffffa1c00554:	108 'l'
(gdb) x/1cb *&amp;a+4
0xffffa1c00555:	111 'o'
(gdb) x/1cb *&amp;a+5
0xffffa1c00556:	33 '!'
(gdb) x/1cb *&amp;a+6
0xffffa1c00557:	0 '\000'
</code></pre></div></div>

<p>At <code class="language-plaintext highlighter-rouge">0xffffa1c00557</code> (*&amp;a + 6), gdb displays 0 ‘\000’</p>]]></content><author><name></name></author><summary type="html"><![CDATA[Know your data types!]]></summary></entry></feed>