<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Programming on Mustafa Albazy</title><link>https://mustafa.page/tags/programming/</link><description>Recent content in Programming on Mustafa Albazy</description><image><title>Mustafa Albazy</title><url>https://mustafa.page/mustafa-profile-pic-compressed.jpg</url><link>https://mustafa.page/mustafa-profile-pic-compressed.jpg</link></image><generator>Hugo -- 0.150.1</generator><language>en</language><lastBuildDate>Sun, 16 Jul 2023 00:00:00 +0000</lastBuildDate><atom:link href="https://mustafa.page/tags/programming/index.xml" rel="self" type="application/rss+xml"/><item><title>C Compiler Errors and Warnings</title><link>https://mustafa.page/posts/2023-07-16-c-compiler-errors-and-warnings/</link><pubDate>Sun, 16 Jul 2023 00:00:00 +0000</pubDate><guid>https://mustafa.page/posts/2023-07-16-c-compiler-errors-and-warnings/</guid><description>&lt;h2 id="compiler-errors"&gt;Compiler Errors&lt;/h2&gt;
&lt;p&gt;C compiler errors split into two main categories.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Syntax Errors: something wrong with the syntax of the language itself.&lt;/li&gt;
&lt;li&gt;Semantic Errors: something wrong with the logic of the code, giving unexpected results.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id="syntax-errors"&gt;Syntax Errors&lt;/h3&gt;
&lt;p&gt;Syntax errors are quite common regardless the programmer experience, we all sometimes make &lt;em&gt;typos&lt;/em&gt;. For example instead of writing &lt;code&gt;int&lt;/code&gt; to declare an integer, one may write it as &lt;code&gt;imt&lt;/code&gt; and that will result in a syntax error during the compilation stage.&lt;/p&gt;</description><content:encoded><![CDATA[<h2 id="compiler-errors">Compiler Errors</h2>
<p>C compiler errors split into two main categories.</p>
<ol>
<li>Syntax Errors: something wrong with the syntax of the language itself.</li>
<li>Semantic Errors: something wrong with the logic of the code, giving unexpected results.</li>
</ol>
<h3 id="syntax-errors">Syntax Errors</h3>
<p>Syntax errors are quite common regardless the programmer experience, we all sometimes make <em>typos</em>. For example instead of writing <code>int</code> to declare an integer, one may write it as <code>imt</code> and that will result in a syntax error during the compilation stage.</p>
<p>Example code:</p>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#8b949e;font-weight:bold;font-style:italic">#include</span> <span style="color:#8b949e;font-weight:bold;font-style:italic">&lt;stdio.h&gt;</span><span style="color:#8b949e;font-weight:bold;font-style:italic">
</span></span></span><span style="display:flex;"><span><span style="color:#8b949e;font-weight:bold;font-style:italic"></span>
</span></span><span style="display:flex;"><span><span style="color:#ff7b72">int</span> <span style="color:#d2a8ff;font-weight:bold">main</span>() {
</span></span><span style="display:flex;"><span>    imt year <span style="color:#ff7b72;font-weight:bold">=</span> <span style="color:#a5d6ff">2023</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#d2a8ff;font-weight:bold">printf</span>(<span style="color:#a5d6ff">&#34;Hello, %d</span><span style="color:#79c0ff">\n</span><span style="color:#a5d6ff">&#34;</span>, year);
</span></span><span style="display:flex;"><span>    <span style="color:#ff7b72">return</span> <span style="color:#a5d6ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>Compiling this code will result into:</p>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>➜  /coding/clion/testccc/example.c:7:5: error: use of undeclared identifier <span style="color:#a5d6ff">&#39;imt&#39;</span>; did you mean <span style="color:#a5d6ff">&#39;int&#39;</span>?
</span></span><span style="display:flex;"><span>    imt <span style="color:#79c0ff">year</span> <span style="color:#ff7b72;font-weight:bold">=</span> 2023;
</span></span><span style="display:flex;"><span>    ^~~
</span></span><span style="display:flex;"><span>    int
</span></span><span style="display:flex;"><span><span style="color:#a5d6ff">1</span> error generated.
</span></span></code></pre></div><p>Carefully reading the error, we can figure out the issue. The error is telling us that on line 7 (on the source file) there is an <em>undeclared identifier</em> that need to be fixed. Which in our case it is the <code>imt</code> need to be <code>int</code>.</p>
<p>Sometimes it&rsquo;s easy and quick fix, other times you may don&rsquo;t get what it is exactly wrong with the syntax and you&rsquo;ll need to search and read some docs, either to refresh yourself our learn something new, and that&rsquo;s totally fine and healthy practice.</p>
<h3 id="semantic-errors">Semantic Errors</h3>
<p>On the other hand we have the semantic error, which is also known as a &ldquo;Logical Error&rdquo;, with these type of errors the program usually compile as normal without errors, but when running the program it does not produce the expected result.</p>
<p>The difficulty with semantic errors is that the compiler usually doesn&rsquo;t recognise them, as there are no mistakes with the code itself and the compiler doesn&rsquo;t really know what is your goal of writing this program and what is the end result you expect to see.</p>
<p>In other way, what you input is what you see on the output. If the programmer mistakenly divide a number instead of multiplying it, then it&rsquo;s the programmer mistake and the compiler does not recognise such type of logical mistakes, it will compile normally both ways.</p>
<p>Example code:</p>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#8b949e;font-weight:bold;font-style:italic">#include</span> <span style="color:#8b949e;font-weight:bold;font-style:italic">&lt;stdio.h&gt;</span><span style="color:#8b949e;font-weight:bold;font-style:italic">
</span></span></span><span style="display:flex;"><span><span style="color:#8b949e;font-weight:bold;font-style:italic"></span>
</span></span><span style="display:flex;"><span><span style="color:#ff7b72">int</span> <span style="color:#d2a8ff;font-weight:bold">main</span>() {
</span></span><span style="display:flex;"><span>    <span style="color:#ff7b72">float</span> width, height, area;
</span></span><span style="display:flex;"><span>    width <span style="color:#ff7b72;font-weight:bold">=</span> <span style="color:#a5d6ff">5.5</span>;
</span></span><span style="display:flex;"><span>    height <span style="color:#ff7b72;font-weight:bold">=</span> <span style="color:#a5d6ff">2.5</span>;
</span></span><span style="display:flex;"><span>    area <span style="color:#ff7b72;font-weight:bold">=</span> width <span style="color:#ff7b72;font-weight:bold">*</span> height;
</span></span><span style="display:flex;"><span>    
</span></span><span style="display:flex;"><span>    <span style="color:#d2a8ff;font-weight:bold">printf</span>(<span style="color:#a5d6ff">&#34;Area = %.2f square meter</span><span style="color:#79c0ff">\n</span><span style="color:#a5d6ff">&#34;</span>, area);
</span></span><span style="display:flex;"><span>    
</span></span><span style="display:flex;"><span>    <span style="color:#ff7b72">return</span> <span style="color:#a5d6ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>In this example we are calculating the area of a rectangle, we have the width and the height, and to find the area we apply formula: <em>area = wh</em>.</p>
<p>But if we change <code>area = width * height;</code> to <code>area = width + height;</code> then the compiler will compile both ways without errors, however the end result will by different.</p>
<p>Which is why we always need to test all the functions of a program and make sure they function as expected before submission. A compiled program doesn&rsquo;t mean it&rsquo;s free of bugs.</p>
<h2 id="compiler-warnings">Compiler Warnings</h2>
<p>A Compiler warning <em>(not error)</em> shows that something need attention within the code, but not to the level that prevents the program from getting compiled.</p>
<p>However, they should not be ignored, as they can lead to big problems down the road.</p>
<p>Example code:</p>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#8b949e;font-weight:bold;font-style:italic">#include</span> <span style="color:#8b949e;font-weight:bold;font-style:italic">&lt;stdio.h&gt;</span><span style="color:#8b949e;font-weight:bold;font-style:italic">
</span></span></span><span style="display:flex;"><span><span style="color:#8b949e;font-weight:bold;font-style:italic"></span>
</span></span><span style="display:flex;"><span><span style="color:#ff7b72">int</span> <span style="color:#d2a8ff;font-weight:bold">main</span>() {
</span></span><span style="display:flex;"><span>    imt year;
</span></span><span style="display:flex;"><span>    <span style="color:#d2a8ff;font-weight:bold">printf</span>(<span style="color:#a5d6ff">&#34;Hello, %d</span><span style="color:#79c0ff">\n</span><span style="color:#a5d6ff">&#34;</span>, year);
</span></span><span style="display:flex;"><span>    <span style="color:#ff7b72">return</span> <span style="color:#a5d6ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>In this case, the variable <code>year</code> is being declared but isn&rsquo;t initialised yet with any value.</p>
<p>So if we compile that, we will get the following warning:</p>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>➜  vsc_c gcc -Wall example.c -o example   
</span></span><span style="display:flex;"><span>example.c:6:27: warning: variable <span style="color:#a5d6ff">&#39;year&#39;</span> is uninitialized when used here <span style="color:#ff7b72;font-weight:bold">[</span>-Wuninitialized<span style="color:#ff7b72;font-weight:bold">]</span>
</span></span><span style="display:flex;"><span>    printf<span style="color:#ff7b72;font-weight:bold">(</span><span style="color:#a5d6ff">&#34;Hello, %d\n&#34;</span>, year<span style="color:#ff7b72;font-weight:bold">)</span>;
</span></span><span style="display:flex;"><span>                          ^~~~
</span></span><span style="display:flex;"><span>example.c:5:13: note: initialize the variable <span style="color:#a5d6ff">&#39;year&#39;</span> to silence this warning
</span></span><span style="display:flex;"><span>    int year;
</span></span><span style="display:flex;"><span>            ^
</span></span><span style="display:flex;"><span>             <span style="color:#ff7b72;font-weight:bold">=</span> <span style="color:#a5d6ff">0</span>
</span></span><span style="display:flex;"><span><span style="color:#a5d6ff">1</span> warning generated.
</span></span></code></pre></div><p>Which tell us exactly that the variable <code>year</code> isn&rsquo;t initialised. Mistakes like this can lead to big problems with functionality and performance/security wise, that&rsquo;s why they shouldn&rsquo;t be ignored even if the program does compile.</p>
<h2 id="linker-errors">Linker Errors</h2>
<p>Linker errors occur during the linking phase of the compilation process when multiple source files and/or external libraries are combined to create an executable program.</p>
<p>For example using functions from ABC library but not including that library, this would cause a linker error.</p>
<h2 id="runtime-errors">Runtime Errors</h2>
<p>Runtime errors happen during the execution of a program. These errors cause the program to behave unexpectedly or terminate/crash, could even crash/freeze the whole computer. Usually such bugs are either an issue dealing with accessing and reading memory, data handling&hellip;etc.</p>
<h2 id="references">References</h2>
<ol>
<li>Dan Gookin, C Programming for Dummies, 2nd edition, 2021.</li>
<li>Jens Gustedt, Modern C, 2020.</li>
</ol>
]]></content:encoded></item><item><title>Best Modern C Programming Language Books for Beginners</title><link>https://mustafa.page/posts/2023-07-10-modern-c-programming-lang-books/</link><pubDate>Mon, 10 Jul 2023 00:00:00 +0000</pubDate><guid>https://mustafa.page/posts/2023-07-10-modern-c-programming-lang-books/</guid><description>&lt;p&gt;If you&amp;rsquo;re starting learning (or refreshing yourself) C programming language then I recommend 2 excellent modern books.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;C for Dummies, 2nd Ed, Dan Gookin.&lt;/li&gt;
&lt;li&gt;Modern C, Jens Gustedt.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The C for Dummies is easier to follow if you&amp;rsquo;re beginner in programming in general. But the Modern C gives more explanation and some of the inner concepts of how and why.&lt;/p&gt;
&lt;p&gt;Both books are available as hardcopy and/or digital ebooks. If you buy a physical copy of &amp;lsquo;Modern C&amp;rsquo; then you can get the ebook version for free.&lt;/p&gt;</description><content:encoded><![CDATA[<p>If you&rsquo;re starting learning (or refreshing yourself) C programming language then I recommend 2 excellent modern books.</p>
<ul>
<li>C for Dummies, 2nd Ed, Dan Gookin.</li>
<li>Modern C, Jens Gustedt.</li>
</ul>
<p>The C for Dummies is easier to follow if you&rsquo;re beginner in programming in general. But the Modern C gives more explanation and some of the inner concepts of how and why.</p>
<p>Both books are available as hardcopy and/or digital ebooks. If you buy a physical copy of &lsquo;Modern C&rsquo; then you can get the ebook version for free.</p>
]]></content:encoded></item></channel></rss>