<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aryan Kumar Shah</title>
    <description>The latest articles on DEV Community by Aryan Kumar Shah (@aryancodes_dev).</description>
    <link>https://dev.to/aryancodes_dev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4203957%2Fd6be3baa-790a-40c2-9b7b-c6e600be1644.jpg</url>
      <title>DEV Community: Aryan Kumar Shah</title>
      <link>https://dev.to/aryancodes_dev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aryancodes_dev"/>
    <language>en</language>
    <item>
      <title>5 C Programming Output Traps That Fooled Me in My First Semester</title>
      <dc:creator>Aryan Kumar Shah</dc:creator>
      <pubDate>Sat, 24 Oct 2026 06:00:44 +0000</pubDate>
      <link>https://dev.to/aryancodes_dev/5-c-programming-output-traps-that-fooled-me-in-my-first-semester-4go9</link>
      <guid>https://dev.to/aryancodes_dev/5-c-programming-output-traps-that-fooled-me-in-my-first-semester-4go9</guid>
      <description>

&lt;p&gt;Hi, I'm &lt;strong&gt;Aryan Kumar Shah&lt;/strong&gt;, a first-year BCA student at MAKAUT, West Bengal. This semester is my first real encounter with C programming, and the biggest lesson so far is this: &lt;strong&gt;the output is not always what your brain predicts.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here are five tiny programs that fooled me. Every output below is real — I compiled and ran each one instead of guessing.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Integer division quietly throws away the fraction
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;My guess: &lt;code&gt;2.5&lt;/code&gt;&lt;br&gt;
Actual output: &lt;code&gt;2&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In C, when both operands are integers, the result is an integer. The fractional part is discarded, not rounded. The same calculation in Python gives &lt;code&gt;2.5&lt;/code&gt; with &lt;code&gt;/&lt;/code&gt; — and Python needs &lt;code&gt;//&lt;/code&gt; to behave like C here. If you want the decimal answer in C, at least one side must be a float: &lt;code&gt;5.0 / 2&lt;/code&gt; gives &lt;code&gt;2.5&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Characters are numbers in disguise
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d %c"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'A'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'A'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output: &lt;code&gt;66 B&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;'A'&lt;/code&gt; is stored as its ASCII value, 65. Add 1 and you get 66 — which prints as the number &lt;code&gt;66&lt;/code&gt; with &lt;code&gt;%d&lt;/code&gt;, and as the character &lt;code&gt;B&lt;/code&gt; with &lt;code&gt;%c&lt;/code&gt;. Same value, two faces. This one idea explains a whole family of exam questions.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Multiplication answers before addition
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;My first guess: &lt;code&gt;20&lt;/code&gt; (2 + 3 = 5, times 4)&lt;br&gt;
Actual output: &lt;code&gt;14&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Operator precedence: &lt;code&gt;*&lt;/code&gt; runs before &lt;code&gt;+&lt;/code&gt;, so it's 2 + 12 = 14. If you want the other answer, you have to ask for it with brackets: &lt;code&gt;(2 + 3) * 4&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Post-increment: use first, increase later
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"a=%d b=%d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output: &lt;code&gt;a=6 b=5&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;a++&lt;/code&gt; hands over the &lt;em&gt;old&lt;/em&gt; value first, then increments. So &lt;code&gt;b&lt;/code&gt; receives 5, and only afterwards does &lt;code&gt;a&lt;/code&gt; become 6. Its twin &lt;code&gt;++a&lt;/code&gt; does it in the opposite order — that single difference is a favourite exam trap.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. 0.1 + 0.2 is not exactly 0.3
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%.20f"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output: &lt;code&gt;0.30000000000000004441&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Computers store decimals in binary, and values like 0.1 have no exact binary form — so tiny errors hide below the usual two decimal places. This is why comparing floats with &lt;code&gt;==&lt;/code&gt; is risky, and why money is handled in integers (paise/cents) in serious software.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'm taking from this
&lt;/h2&gt;

&lt;p&gt;Predicting output before running code — a "dry run" — is the skill underneath all five traps. I now write my guess first, then compile and check. Being wrong on paper is cheap; being wrong in the exam is not.&lt;/p&gt;

&lt;p&gt;If you're also in your first semester and one of these fooled you too, tell me which one in the comments — mine was #4.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;I'm Aryan Kumar Shah, a BCA student at MAKAUT learning Python, C, Java and JavaScript, and building small developer tools as I go.&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Portfolio: &lt;a href="https://aryancodes-dev.github.io/" rel="noopener noreferrer"&gt;https://aryancodes-dev.github.io/&lt;/a&gt; · GitHub: &lt;a href="https://github.com/aryancodes-dev" rel="noopener noreferrer"&gt;https://github.com/aryancodes-dev&lt;/a&gt; · YouTube: &lt;a class="mentioned-user" href="https://dev.to/aryancodes_dev"&gt;@aryancodes_dev&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>c</category>
      <category>beginners</category>
      <category>programming</category>
      <category>bca</category>
    </item>
  </channel>
</rss>
