<?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" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Medha Jain on Medium]]></title>
        <description><![CDATA[Stories by Medha Jain on Medium]]></description>
        <link>https://medium.com/@medhajain511?source=rss-74c15c82382f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6GKstbdT_gfNMG8wgWy4cA@2x.jpeg</url>
            <title>Stories by Medha Jain on Medium</title>
            <link>https://medium.com/@medhajain511?source=rss-74c15c82382f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 16:16:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@medhajain511/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Integer Overflow in Embedded Systems: The Hidden Pitfalls of char c=255]]></title>
            <link>https://medium.com/@medhajain511/integer-overflow-in-embedded-systems-the-hidden-pitfalls-of-char-c-255-eaad8b28b3a2?source=rss-74c15c82382f------2</link>
            <guid isPermaLink="false">https://medium.com/p/eaad8b28b3a2</guid>
            <dc:creator><![CDATA[Medha Jain]]></dc:creator>
            <pubDate>Fri, 07 Feb 2025 15:07:23 GMT</pubDate>
            <atom:updated>2025-02-07T15:09:33.542Z</atom:updated>
            <content:encoded><![CDATA[<p>As an embedded firmware engineer, I’ve seen countless bugs stem from misunderstanding how integers behave at the bit level. Today, let’s dive into a deceptively simple piece of code that has trapped many developers:</p><p>char c = 255;<br>c = c + 1; // What happens here?</p><p>This small code snippet uncovers <strong>integer overflow, implicit type promotions, and architecture-specific behaviors</strong> — crucial concepts for writing <strong>reliable embedded software</strong>.</p><p>1️. How char c = 255; is Stored in Memory<br>Before analyzing c + 1, let’s understand how 255 is stored based on whether char is signed or unsigned:</p><p>🔹 Case 1: Unsigned char (uint8_t)<br>Range: 0 to 255 (8-bit)<br>Storage: 255 is stored as 1111 1111 (0xFF)<br>255 → 1111 1111 (0xFF)<br>🔹 Case 2: Signed char (int8_t)<br>Range: -128 to 127 (Two’s complement)<br>Storage: 255 is <strong>out of range</strong>, so it wraps to -1<br>255 (stored as -1) → 1111 1111 (0xFF), but interpreted as -1.<br>📌 Key Takeaway: The same bit pattern (0xFF) has different meanings based on whether char is signed or unsigned.</p><p><strong>The Overflow Conundrum: What Happens to c + 1?</strong><br>Now, let’s analyze what happens when we increment c:</p><p>✅ Scenario 1: Signed char (int8_t)</p><p>char c = 255; // Interpreted as -1 in two’s complement<br>char result= c + 1; // -1 + 1 = 0<br>result was -1, so -1 + 1 = 0.<br>Final result: 0 (No wraparound, just standard signed addition).<br>✅ Scenario 2: Unsigned char (uint8_t)</p><p>uint8_t c = 255; // 0xFF<br>uint8_t result = (uint8_t)(c + 1); // Overflows to 0<br>255 + 1 = 256 → Since uint8_t only stores 8 bits, the 9th bit is discarded.</p><p><strong>Wrap-around behavior occurs because uint8_t is modulo 256</strong>).</p><p>10000 0000 (256)<br>↓ Drop the 9th bit<br>0000 0000 (Result: 0)<br>The value wraps around, resulting in 0.<br>💡 Why Does This Matter?<br>Different results for signed vs. unsigned char!<br>The same code may behave differently across compilers and architectures.<br>Misinterpretation of overflow leads to subtle and hard-to-debug issues.</p><h3><strong>Final Code:</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mIhyAMnnMsTBU0kgCmiTdw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KhmpjI0dPAxJTn_dBi2bgQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h1yTg7CS06sbxBho5MwQvw.png" /><figcaption>Output</figcaption></figure><h4><strong>Key points to remember:</strong></h4><p>🔹 <strong>Always use fixed-width types (uint8_t</strong>, <strong>int8_t)</strong> instead of generic char.<br>🔹 <strong>Be mindful of integer promotions</strong>—avoid relying on implicit compiler behavior.</p><p>🔹<strong>Understand how signed vs. unsigned types affect integer overflow.</strong><br><strong> Avoid unnecessary &amp; </strong>0xFF <strong>when uint8_t naturally wraps.</strong><br>🔹<strong>Be mindful of implicit integer promotions </strong>— use explicit casting to prevent unintended behavior.</p><p>📌 In embedded systems, every bit matters! Control integer overflow or it will control your system</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eaad8b28b3a2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Compilation stages in C]]></title>
            <link>https://medium.com/@medhajain511/compilation-stages-in-c-aa1823e741d4?source=rss-74c15c82382f------2</link>
            <guid isPermaLink="false">https://medium.com/p/aa1823e741d4</guid>
            <dc:creator><![CDATA[Medha Jain]]></dc:creator>
            <pubDate>Fri, 01 Nov 2024 08:05:36 GMT</pubDate>
            <atom:updated>2024-11-01T08:05:36.186Z</atom:updated>
            <content:encoded><![CDATA[<p>In C programming, the compilation process involves several stages that transform the source code(.c) into an executable file(.exe). There are 4 stages in compilation process:</p><ol><li>Pre-processing</li><li>Compilation</li><li>Assembling</li><li>Linking</li></ol><p><strong>1.Preprocessing</strong></p><p>Pre-processing is the first step in the compilation process in C performed using the<strong> <em>pre-processor tool(</em></strong>typically invoked using the -E flag with the gcc compiler to process source code before actual compilation. The -E flag tells gcc to stop after the preprocessing stage and output the preprocessed version of the source file. This output file, usually with a .i extension, includes all expanded macros, inserted headers, and conditionally included or excluded code.).All the statements starting with the # symbol in a C program are processed by the pre-processor, and it converts program file into an intermediate file with no # statements. The preprocessed tasks performed are:</p><ol><li><strong>Removal of Comments</strong>: All comments are stripped from the source code.</li><li><strong>Macros: </strong>are defined using #define and are constants or expressions that the preprocessor replaces before compilation. When a macro is called, the preprocessor replaces each macro reference with its defined value or expression, a process known as macro expansion. Let’s understand this with an example how macros are expanded and all:</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/517/1*dQsUOkpv0cRx91C_CsWX5w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/721/1*seqQC5wZjLMY7t6K3y5hyA.png" /></figure><p>During preprocessing, the macro SQUARE(a) will be expanded by replacing SQUARE(x) with ((x) * (x)), where x is a. After macro expansion, the code might look something like:</p><p>int main() {</p><p>int a = 5;</p><p>int result = ((a) * (a)); // + Expansion of macro SQUARE done by preprocessor internally</p><p>printf(“Result: %d\n”, result);//Result:25</p><p>return 0; }</p><p>3.<strong>File Inclusion</strong>:It includes the content of header files (#include).For example:#include &lt;stdio.h&gt; (The preprocessor replaces this line with the full content of the stdio.h file, providing access to standard I/O functions like printf and scanf).</p><p>4. Conditional compilation: During preprocessing, the preprocessor processes <strong>conditional compilation directives</strong> like #ifdef, #ifndef,#if, and #endif. These directives determine whether specific blocks of code should be included or excluded in the final preprocessed output, based on defined conditions.For example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/536/1*g9idKqru7vW9lyupWq74BQ.png" /></figure><p>If DEBUG is defined, the code prints “Debugging information” otherwise, it prints “Not Defined”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/790/1*NEjHdmqqg5YN9FbbFVZfhQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/211/1*bVQJwwiqFh9V33vzkdHtGA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/907/1*1ELD25mjiairZVSEzy3PHA.png" /></figure><p>This command (gcc -E P1.c -o P1.i ) preprocesses P1.c and writes the result to P1.i.</p><p><strong>2. Compilation:</strong>During the <strong>compiling phase</strong> in C, the compiler takes the preprocessed file (.i) and converts it into an <strong>assembly file</strong> (.s). This assembly file contains <strong>assembly-level instructions</strong> specific to the target machine architecture.During this phase, the compiler performs <strong>syntax analysis</strong> (or parsing), checking the entire program for syntax errors and generating relevant warnings via terminal window.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/262/1*ElYuJX0fOKRvyiwioyYKJw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*xV_mLidaxe6LZ9oCpOLT0A.png" /></figure><ul><li>Use gcc -Wall -S P1.c -o P1.s ,if you want to generate an assembly file (P1.s) directly from your C source file (P1.c). This is the more common approach as it combines preprocessing, compilation, and assembly in one step.</li><li>Use gcc -S P1.i -o P1.s if you already have a preprocessed file (P1.i). This approach is used when you want to control preprocessing separately, which might be helpful for debugging preprocessor issues.</li></ul><p>Typically, the first command is simpler and preferred unless you have specific reasons for separating the preprocessing step.It is during this phase that the compiler attempts to optimize your code, applying various techniques to enhance performance or hereby reducing resource usage. This stage is crucial for catching any mistakes that violate the C language rules and provides valuable feedback through terminal messages regarding syntax errors or potential issues.</p><p><strong>3. Assembly Phase: </strong>In the <strong>assembly phase</strong>, the .s file (assembly code) is passed to an assembler, which converts it into <strong>machine code</strong>. The assembler generates an <strong>object file</strong> (.o or .obj ), which is a binary representation of your source code. In simple words, we can say assembler converts the assembly program into opcodes where opcodes are machine code(in form of 1’s and 0&#39;s(binary format)). You can use the -c flag with gcc to stop after the assembly phase and generate an object file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/751/1*VTM74fS0487r7k87JQRasA.png" /><figcaption>This command compiles the assembly code into an object file without linking.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/146/1*xcHbe4UA16Zydc7_jpAMGA.png" /></figure><p>Note that the output of the assembly phase (.o file) is in a binary format, which is not directly viewable in the terminal as text. To view the contents of an object file, you can use tools like objdump or hexdump to inspect it in a readable form.These tools are yet to be explored but will let you know in my future blogs.</p><p><strong>4. Linking: </strong>The <strong>linking phase</strong> involves combining the object file with other object files and <strong>library files</strong> (.lib) to create the final <strong>executable</strong> file.</p><p>During linking, the linker resolves these symbolic references by replacing them with the actual memory addresses or offsets, enabling different parts of a program (like functions or variables) to be correctly linked. For example, when one function calls another, the symbolic reference (function name) is replaced with the actual address where that function’s code resides, allowing the executable to function correctly at runtime.(<strong>Symbolic reference</strong> refers to the use of names or symbols (like variable names, function names etc ) in a program to refer to specific memory addresses or data.)</p><p>The linking process generates an <strong>executable file </strong>with an extension of (.exe)in DOS or (.out) in UNIX-based operating systems to get output on screen/terminal.</p><p>Types of Linking:</p><ol><li><strong>Static linking</strong> is the process where all the necessary code, including external libraries, is combined into a single executable during the compilation phase. This means that the executable file contains all the required machine code for the program to run, making it independent of external libraries at runtime.Major disadvantage are larger executable size and memory usage as static linking can lead to redundant copies of libraries if multiple programs using the same library as each executable will contain its own copy.</li><li><strong>Dynamic linking</strong> keeps the executable smaller by loading libraries at runtime (done by operating system), making it more efficient for systems with many shared applications. Most modern systems use dynamic linking by default.</li></ol><h4>Loading Phase</h4><p>After linking, the <strong>loading phase</strong> occurs when the executable file is brought into memory for execution. The <strong>loader</strong> (a part of the operating system) loads the executable, allocates necessary memory, and sets up the environment, including the stack, heap, and mapped code sections. This phase prepares the program for running by setting the initial state of the processor and memory</p><p><strong>Conclusion:</strong></p><p>Compilation is not just converting human friendly code (source code) to generating an executable but a multi-step process involving a series of transformations and checks. By understanding each of these stages, you gain better flow of overall code, making it easier to diagnose issues.</p><p>Reference:</p><p><a href="https://www.scaler.com/topics/c/compilation-process-in-c/">https://www.scaler.com/topics/c/compilation-process-in-c/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aa1823e741d4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>