<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>C on Siddharth Mishra</title><link>http://brightprogrammer.in/tags/c/</link><description>Recent content in C on Siddharth Mishra</description><generator>Hugo</generator><language>en</language><lastBuildDate>Sun, 14 Jun 2026 21:53:10 -0700</lastBuildDate><atom:link href="http://brightprogrammer.in/tags/c/index.xml" rel="self" type="application/rss+xml"/><item><title>A C Of Polymorphism Without CPP</title><link>http://brightprogrammer.in/posts/a-c-of-polymorphism-with-cpp/</link><pubDate>Wed, 25 Mar 2026 00:00:00 +0000</pubDate><guid>http://brightprogrammer.in/posts/a-c-of-polymorphism-with-cpp/</guid><description>&lt;h1 id="introduction">Introduction&lt;/h1>
&lt;p>Polymorphism sounds like a C++ word. In C, it still exists, just without the sugar. You create a
base type, keep a table of function pointers, and pass that base pointer around.&lt;/p>
&lt;h1 id="a-small-example">A Small Example&lt;/h1>
&lt;p>Here’s an example. This is the base class.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-c" data-lang="c">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#007020;font-weight:bold">typedef&lt;/span> &lt;span style="color:#007020;font-weight:bold">struct&lt;/span> Animal Animal;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#007020;font-weight:bold">struct&lt;/span> Animal {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#902000">char&lt;/span>&lt;span style="color:#666">*&lt;/span> name;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#902000">void&lt;/span> (&lt;span style="color:#666">*&lt;/span>speak)(Animal&lt;span style="color:#666">*&lt;/span> self);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>};
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Now the child types.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-c" data-lang="c">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#007020;font-weight:bold">typedef&lt;/span> &lt;span style="color:#007020;font-weight:bold">struct&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Animal base;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>} Dog;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#007020;font-weight:bold">typedef&lt;/span> &lt;span style="color:#007020;font-weight:bold">struct&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Animal base;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>} Cat;
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The polymorphic functions live in the child types. There is no default &lt;code>animal_speak()&lt;/code>, so this
behaves like a pure virtual method.&lt;/p></description></item><item><title>OnlyGenerics In C</title><link>http://brightprogrammer.in/posts/onlygenerics-in-c/</link><pubDate>Wed, 25 Mar 2026 00:00:00 +0000</pubDate><guid>http://brightprogrammer.in/posts/onlygenerics-in-c/</guid><description>&lt;h1 id="introduction">Introduction&lt;/h1>
&lt;p>C doesn’t have templates, but it does have the preprocessor. If you’re careful, you can get
something that feels like &lt;code>std::vector&amp;lt;T&amp;gt;&lt;/code> without switching languages. It’s not perfect and it’s
not type theory, but it is surprisingly usable.&lt;/p>
&lt;p>This post is about that: cheap macro tricks that give you “templated” types in plain C.&lt;/p>
&lt;h1 id="the-core-trick">The Core Trick&lt;/h1>
&lt;p>You first define a generic backing struct. Then you create a macro that expands to a type‑specific
struct. That macro is your “template”.&lt;/p></description></item><item><title>A Build System For C in C</title><link>http://brightprogrammer.in/posts/a-build-system-in-c-for-c/</link><pubDate>Wed, 29 Jan 2025 00:00:00 +0000</pubDate><guid>http://brightprogrammer.in/posts/a-build-system-in-c-for-c/</guid><description>&lt;p>I recently started a new project where I had to decide (again) how to build this project. This decision is faced
by any developer who use a language that does not have a standard way of building things. In my case, my project
is to be written in C, my go-to language, and as we all know, it does not have any standard other than the standard
spec. I usually go with CMake, as my go-to build system, because I&amp;rsquo;ve seen many projects use it. It&amp;rsquo;s almost like
the &amp;ldquo;industry&amp;rdquo; standard. It&amp;rsquo;s very old, and regularly updated, to keep itself up to date with available libraries,
frameworks, compiler features etc&amp;hellip; and it might be good for very big projects, but I decided that I&amp;rsquo;ll try something
simpler this time.&lt;/p></description></item><item><title>AutoGrad - Algorithmic Differentiation in C</title><link>http://brightprogrammer.in/posts/autograd-algorithmic-differentiation/</link><pubDate>Sat, 28 Dec 2024 00:00:00 +0000</pubDate><guid>http://brightprogrammer.in/posts/autograd-algorithmic-differentiation/</guid><description>&lt;h2 id="background">Background&lt;/h2>
&lt;p>Let&amp;rsquo;s write an auto differentiation algorithm. We will take mathematical expressions
in prefix notation, build an expression tree like the one below for&lt;/p>
&lt;p>\[
\begin{align}
1338 * a + \frac{y^5}{x^{1335}}
\end{align}
\]&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-c" data-lang="c">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#60a0b0;font-style:italic">// expression in prefix notation
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#60a0b0;font-style:italic">&lt;/span>Expr e &lt;span style="color:#666">=&lt;/span> &lt;span style="color:#06287e">ADD_EXPR&lt;/span>(
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#06287e">MUL_EXPR&lt;/span>(&lt;span style="color:#06287e">INT_CONSTANT_EXPR&lt;/span>(&lt;span style="color:#40a070">1338&lt;/span>), &lt;span style="color:#06287e">VAR_EXPR&lt;/span>(&lt;span style="color:#4070a0">&amp;#34;a&amp;#34;&lt;/span>, &lt;span style="color:#40a070">1&lt;/span>)),
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#06287e">DIV_EXPR&lt;/span>(&lt;span style="color:#06287e">VAR_EXPR&lt;/span>(&lt;span style="color:#4070a0">&amp;#34;y&amp;#34;&lt;/span>, &lt;span style="color:#40a070">5&lt;/span>), &lt;span style="color:#06287e">VAR_EXPR&lt;/span>(&lt;span style="color:#4070a0">&amp;#34;x&amp;#34;&lt;/span>, &lt;span style="color:#40a070">1337&lt;/span>))
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>);
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;pre tabindex="0">&lt;code class="language-mermaid" data-lang="mermaid">graph TD
 node0[&amp;#34;add&amp;#34;]
 node1[&amp;#34;mul&amp;#34;]
 node2[&amp;#34;1338&amp;#34;]
 node3[&amp;#34;$a^{1}$&amp;#34;]
 node4[&amp;#34;div&amp;#34;]
 node5[&amp;#34;$y^{5}$&amp;#34;]
 node6[&amp;#34;$x^{1337}$&amp;#34;]

 node0 --&amp;gt; node1
 node0 --&amp;gt; node4
 node1 --&amp;gt; node2
 node1 --&amp;gt; node3
 node4 --&amp;gt; node5
 node4 --&amp;gt; node6
&lt;/code>&lt;/pre>&lt;p>and then write an algorithm to traverse the expression tree, taking derivatives of each sub-expression
recursively and then generating a new expression tree that represents the derivative of the original
expression tree, like the one below by just doing &lt;code>ExprGrad(&amp;amp;e)&lt;/code>&lt;/p></description></item><item><title>Zero To Prod In C - Creating Server &amp; Hosting</title><link>http://brightprogrammer.in/posts/creating-and-hosting-your-http-server-in-c/</link><pubDate>Wed, 25 Dec 2024 00:00:00 +0000</pubDate><guid>http://brightprogrammer.in/posts/creating-and-hosting-your-http-server-in-c/</guid><description>&lt;h2 id="background">Background&lt;/h2>
&lt;p>I recently started looking into self-hosting, and after spending two almost sleepless nights, I finally have a solution
to self host any service and link it to an endpoint with your domain name. Usually, you&amp;rsquo;d need a static IP address that
you must purchase from your ISP, and it&amp;rsquo;s quite costly (depending on bandwith and IP range). This solution does not need
any static IP address, or port forwarding or any magic from your side. You just need a machine where you can just do things.
In my case I have a Raspberry Pi, on which I&amp;rsquo;ve installed Ubuntu Server, but this can be any machine that you can keep
up for extended periods of time, be it your laptop, or a real home lab setup, everything goes.&lt;/p></description></item><item><title>How I Do Error Handling in C</title><link>http://brightprogrammer.in/posts/error-handling-in-c/</link><pubDate>Fri, 13 Dec 2024 00:00:00 +0000</pubDate><guid>http://brightprogrammer.in/posts/error-handling-in-c/</guid><description>&lt;p>This is not actually supposed to be a post because I think everyone has their own way of doing
things. This post I&amp;rsquo;m writing is just to overcome the FOMO of not writing enough blog posts, but
owning a domain and constantly keeping up my Pi4 to host this static website. Also because good
software engineers recommend making a habit of writing a blog post regularly.&lt;/p>
&lt;h1 id="the-problem">The Problem&lt;/h1>
&lt;p>You&amp;rsquo;re writing lots of functions in your program and it&amp;rsquo;s possible for those functions to fail
and you want to know when they fail. Say you&amp;rsquo;re creating a &lt;code>Vector&lt;/code> implementation in C and
you want to know whether the &lt;code>VectorPushBack&lt;/code> method you wrote will fail or succeed in inserting
element.&lt;/p></description></item><item><title>Creating Our Own 'puts'</title><link>http://brightprogrammer.in/posts/creating-our-own-puts/</link><pubDate>Sun, 09 Jan 2022 00:00:00 +0000</pubDate><guid>http://brightprogrammer.in/posts/creating-our-own-puts/</guid><description>&lt;p>Well, the title can be misleading. We will be eventually implementing our own &amp;lsquo;puts&amp;rsquo; function but for now it&amp;rsquo;s not named puts. The function will be analogous to puts. This function will actually be to help us debug things. After this chapter we will be adding more and more features to our OS and we need a method to see if it worked or not. This method can be anything! For eg : changing the screen colour to red (for error) or blue (for success). We print an error message if something bad happens.&lt;/p></description></item><item><title>Project Initialisation</title><link>http://brightprogrammer.in/posts/project-initialisation/</link><pubDate>Sat, 08 Jan 2022 00:00:00 +0000</pubDate><guid>http://brightprogrammer.in/posts/project-initialisation/</guid><description>&lt;p>We&amp;rsquo;ll use &lt;a href="https://github.com/limine-bootloader/limine">limine&lt;/a> bootloader and use stivale2 as our boot protocol. There are multiple boot protocols and stivale2 provides many advanced features. Limine supports stivale2 by default. It also gives us a way to instantly print anything on screen but we won&amp;rsquo;t be using that.
To read more about stivale2 boot specification, read &lt;a href="https://github.com/stivale/stivale/blob/master/STIVALE2.md">here&lt;/a>&lt;/p>
&lt;p>Create project with following directory structure&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>.
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>├── CMakeLists.txt
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>├── kernel
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>│   ├── kernel.c
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>│   ├── kernel.h
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>│   └── stivale2.h
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>└── limine
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> ├── bochsrc
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> ├── build-aux
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>.
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>.
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>.
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#40a070">19&lt;/span> directories, &lt;span style="color:#40a070">183&lt;/span> files
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;code>kernel&lt;/code> will contain our kernel code. Soon we&amp;rsquo;ll be creating many other folders. Use &lt;code>wget&lt;/code> to get &lt;a href="https://raw.githubusercontent.com/stivale/stivale/master/stivale2.h">stivale2&lt;/a> header file. Initialise a git repository and add &lt;a href="https://github.com/limine-bootloader/limine">limine&lt;/a> boot loader as a &lt;code>submodule&lt;/code>.&lt;/p></description></item></channel></rss>