<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>emre şahin's digital garden 🍃 - C</title>
    <link>https://emresahin.net/tags/c/</link>
    <description>Posts in the C tag</description>
    <language>en</language>
    <managingEditor>contact@emresahin.net (Emre Şahin)</managingEditor>
    <lastBuildDate>Tue, 15 Sep 2026 19:46:32 +0000</lastBuildDate>
    <atom:link href="https://emresahin.net/tags/c/rss.xml" rel="self" type="application/rss+xml"/>
    <item>
      <title>TIL 12: Modern C Features</title>
      <published>2025-01-04T12:58:23+00:00</published>
      <updated>2025-01-04T12:58:23+00:00</updated>
      <author>Emre Şahin</author>
      <pubDate>Sat, 04 Jan 2025 12:58:23 +0000</pubDate>
      <link>https://emresahin.net/til-12/</link>
      <guid isPermaLink="true">https://emresahin.net/til-12/</guid>
      <description>🐢 There are some new features in C since I learned it in the 90s. One is variable-length arrays (VLAs). You can now set the length of an array at runtime. This makes some uses of pointers moot, but Linus Torvalds is reported to have said that the Linux kernel does not contain any VLAs. 🐇 It may b...</description>
      <category>Software Development</category>
      <category>C</category>
      <category>arrays</category>
      <category>variable length arrays</category>
      <category>complex numbers</category>
      <category>initialization</category>
      <category>dialog</category>
      <category>TIL</category>
      <content:encoded><![CDATA[<p>🐢 There are some new features in C since I learned it in the 90s. One is variable-length arrays (VLAs). You can now set the length of an array at runtime. This makes some uses of pointers moot, but Linus Torvalds is reported to have said that the Linux kernel does not contain any VLAs.</p>
<p>🐇 It may be useful, though. It looks like syntactic sugar for a <code>const</code> pointer plus <code>malloc</code>, but implicitness is usually not ideal. It also appears to use the stack.</p>
<p>🐢 I haven’t looked into the details, but it may be useful in some cases. I don’t think it can replace pointer usage if it uses the stack, though. Stack sizes are usually small, and having arrays that can fill them up is not a good approach. Another feature is complex number support. It seems from the lecture that this is also a bit of a half-baked feature.</p>
<p>🐇 What is the difference between this and using a struct with two float fields?</p>
<p>🐢 There are operators (+, -, *, or ==) that support these. Since there is no operator overloading in C, having a separate complex number type may be useful.</p>
<p>🐇 The example looks like <code>double complex cx = 1.0 + 3.0*I</code>; and yes, this may be useful if you’re frequently using complex numbers in your code. But I think it’s unnecessary in most cases. There shouldn’t be such a frequent need for complex numbers, right?</p>
<p>🐢 A good complex number library will probably provide more than the built-in type, such as vectors for these numbers. A dedicated library will still be needed in most cases, I believe.</p>
<p>🐇 Another feature added to C is that struct members can now be initialized by name or index. You can initialize an array like <code>int a[6] = { [3] = 29, [2] = 14 };</code>, and it will initialize only those specific members.</p>
<p>🐢 Ah, this is much more useful than complex numbers for the general case.</p>
<p>🐇 There is also a span syntax: <code>int a[10] = {1, 2, [2 ... 4] = 3, [5] = 30};</code>.</p>
<p>🐢 That is really neat.</p>
<p>🐇 It’s also possible to omit the initial length, so the initializer values determine the size of the array. For example, <code>int a[] = {1, 3, [30] = 55, [9090] = 1010};</code> will result in an array of length 9091.</p>
<p>🐢 How is this used with structs?</p>
<p>🐇 Instead of <code>[]</code>, in struct initializers, we use the dot notation, like <code>struct Point p1 = { .x=0, .y=10 };</code>.</p>
<p>🐢 This is really useful and would keep much of the initialization code simpler.</p>
<p>🐇 There is also a way to initialize arrays of structs, like:</p>
<pre><code class="language-c">struct points pts[5] = { [0].x = 10, [0].y = 20, [3].x = 100 };
</code></pre>
<p>🐢 This feature is a nice addition to C; I really like it.</p>]]></content:encoded>
    </item>
    <item>
      <title>Adding version information to executables in CMake projects</title>
      <published>2018-02-16T11:04:16+00:00</published>
      <updated>2018-02-16T11:04:16+00:00</updated>
      <author>Emre Şahin</author>
      <pubDate>Fri, 16 Feb 2018 11:04:16 +0000</pubDate>
      <link>https://emresahin.net/versioning-through-cmake-14095-76258/</link>
      <guid isPermaLink="true">https://emresahin.net/versioning-through-cmake-14095-76258/</guid>
      <description>In programming, versioning your code files is of immense importance. Most files need to be constantly updated, renamed, and merged. You also need backups, as everyone learns after losing work due to various computer problems. Another problem we face is establishing a connection between an executa...</description>
      <category>Development</category>
      <category>C/C++</category>
      <category>CMake</category>
      <category>CMake</category>
      <category>C</category>
      <category>Versioning</category>
      <category>Git</category>
      <category>Build Systems</category>
      <category>Automation</category>
      <content:encoded><![CDATA[<p>In programming, versioning your code files is of immense importance. Most
files need to be constantly updated, renamed, and merged. You also need
backups, as everyone learns after losing work due to various computer problems.</p>
<p>Another problem we face is establishing a connection between an
executable file or library and its source code. We normally don’t add executable
files to version control, as they are produced from code files. A common
solution to this is writing version information to an “About” page or
something similar.</p>
<p>When I was using Subversion some 15 years ago, I would create hooks to change the
code files for the necessary versioning info, but this is not a recommended
approach in Git because of its distributed nature. I have never tried it, but it
would likely create more problems than it solves. The recommended way is to use the
build system’s facilities to retrieve the versioning information and add it to
the necessary places.</p>
<p>While developing the C library for dervaze, I wanted to add descriptive
versioning information, as the library will also contain wordlists, and more words
will be added over time.</p>
<p>In CMake, it’s possible to set versioning information and supply it through
compiler options. It’s also possible to replace strings formatted as
<code>@CHANGE_THIS@</code> in source files. To supply version information to the executable,
you can use these facilities:</p>
<pre><code class="language-cmake">
set (DERVAZE_VERSION_MAJOR 1)
set (DERVAZE_VERSION_MINOR 0)
string(TIMESTAMP DERVAZE_TIMESTAMP "%y%m%d%H%M%S")
# current branch
execute_process(
  COMMAND git rev-parse --abbrev-ref HEAD
  WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  OUTPUT_VARIABLE DERVAZE_GIT_BRANCH
  OUTPUT_STRIP_TRAILING_WHITESPACE
)

# abbreviated commit hash
execute_process(
  COMMAND git log -1 --format=%h
  WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  OUTPUT_VARIABLE DERVAZE_GIT_COMMIT_HASH
  OUTPUT_STRIP_TRAILING_WHITESPACE
)

</code></pre>
<p>This information can be supplied to the C files by creating a header file that
will be used as a template.</p>
<pre><code class="language-c">
#define DERVAZE_VERSION_MAJOR       "@DERVAZE_VERSION_MAJOR@"
#define DERVAZE_VERSION_MINOR       "@DERVAZE_VERSION_MINOR@"
#define DERVAZE_TIMESTAMP           "@DERVAZE_TIMESTAMP@"
#define DERVAZE_LIB_GIT_BRANCH      "@DERVAZE_GIT_BRANCH@"
#define DERVAZE_LIB_GIT_COMMIT_HASH "@DERVAZE_GIT_COMMIT_HASH@"
</code></pre>
<p>Suppose this file is named <code>version.h.in</code>; the following command creates the
actual <code>version.h</code> for each build.</p>
<pre><code class="language-cmake"># configure a header file to pass some of the CMake settings
# to the source code
configure_file (
  "${PROJECT_SOURCE_DIR}/version.h.in"
  "${PROJECT_SOURCE_DIR}/version.h"
  )

</code></pre>
<p>It’s also possible to write this file only during the build by using
<code>${PROJECT_BINARY_DIR}/version.h</code> as the second argument, but in my experience,
keeping such a file in the source directory is sometimes needed by build tools.
When you keep it in the source directory, it’s better to ignore the generated
<code>version.h</code> by adding it to <code>.gitignore</code>.</p>]]></content:encoded>
    </item>
    <item>
      <title>The Sorry State of NDK Testing in Android</title>
      <published>2018-02-08T09:39:03+00:00</published>
      <updated>2018-02-08T09:39:03+00:00</updated>
      <author>Emre Şahin</author>
      <pubDate>Thu, 08 Feb 2018 09:39:03 +0000</pubDate>
      <link>https://emresahin.net/ndk-testing-android-14087-70802/</link>
      <guid isPermaLink="true">https://emresahin.net/ndk-testing-android-14087-70802/</guid>
      <description>I’m writing a C library for use in Android, iOS, and Python applications. Although the C library has its own unit tests, I wanted to write a few more to ensure that data transfer between the C and Android layers is correct. In Android, one needs to put unit test files in the app/src/test/*module-...</description>
      <category>Software</category>
      <category>Android</category>
      <category>Development</category>
      <category>ndk</category>
      <category>android</category>
      <category>testing</category>
      <category>c</category>
      <category>jni</category>
      <content:encoded><![CDATA[<p>I’m writing a C library for use in Android, iOS, and Python applications. Although
the C library has its own unit tests, I wanted to write a few more to ensure
that data transfer between the C and Android layers is correct.</p>
<p>In Android, one needs to put unit test files in the <code>app/src/test/*module-name*</code>
directory. I spent a few hours yesterday writing tests to check that the
conversion between <em>visenc</em> and <em>Unicode</em> is correct in Android. After those
hours, however, I learned that NDK support for tests is limited, and the error I
received means that <a href="https://stackoverflow.com/questions/34802667/unit-test-java-class-that-loads-native-library">I need to write instrumentation
tests</a>
instead, which use an emulator or a real device.</p>
<p>Instrumentation tests are <em>user tests</em> that check whether the user-facing
aspects of the app run correctly. Unit tests, instead, check whether data flow,
calculations, etc., are working as expected. The former is slow; it needs to be
run in an emulator and should mimic user behavior. Just checking
whether <em>e</em> converted correctly to ا should not require an emulator.</p>]]></content:encoded>
    </item>
    <item>
      <title>Progress on Ottoman Translation - Week 6, 2018</title>
      <published>2018-02-05T07:23:43+00:00</published>
      <updated>2018-02-05T07:23:43+00:00</updated>
      <author>Emre Şahin</author>
      <pubDate>Mon, 05 Feb 2018 07:23:43 +0000</pubDate>
      <link>https://emresahin.net/dervaze-progress-2018-6-14084-62774/</link>
      <guid isPermaLink="true">https://emresahin.net/dervaze-progress-2018-6-14084-62774/</guid>
      <description>Some of the upcoming posts will serve as a TODO list for the coming months, outlining my plans for Dervaze and its mobile versions. As a solo developer, I’ll share my experiences with this problem here to help those interested. The technology for Ottoman OCR was mostly ready before my family obli...</description>
      <category>NLP</category>
      <category>History</category>
      <category>Ottoman Turkish</category>
      <category>OCR</category>
      <category>C</category>
      <category>Dervaze</category>
      <category>Linguistics</category>
      <content:encoded><![CDATA[<p>Some of the upcoming posts will serve as a TODO list for the coming months, outlining my plans for <a href="http://dervaze.com">Dervaze</a> and its mobile versions. As a solo developer, I’ll share my experiences with this problem here to help those interested.</p>
<p>The technology for Ottoman OCR was mostly ready before my family obligations interrupted the project. I need to re-evaluate what is currently available, but a more pressing problem for me is the <em>speed</em> of translation; currently, it is so slow that it’s barely usable.</p>
<p>I have written the dictionary as a C library without any database dependencies and integrated it into the Android version. I’m currently updating the search functionality in the mobile app to use this library instead of a web service. It will be orders of magnitude faster than the current version because it’s offline and uses a <a href="https://en.wikipedia.org/wiki/Trie">trie</a> to store the words—making it both small and fast.</p>
<p>In my experience, having a single data structure with dedicated functions for transforming and indexing is much simpler than managing multiple data structures. In my case, the core structure is:</p>
<pre><code class="language-c">typedef struct _dervaze_lexical_item {
  int index;
  bstr latin_search_key;
  bstr latin;
  bstr visenc_search_key;
  bstr visenc_dotless_search_key;
  bstr visenc;
  bstr annotation;
  bstr meaning;
  bstr abjad;
  lexical_role role;
  int last_vowel;
  int props;
} dervaze_lexical_item;
</code></pre>
<p><code>visenc</code> is our abbreviation for <em>visual encoding</em>, which is used to represent Arabic/Ottoman/Farsi words using basic ASCII letters. It is documented on its <a href="https://emresahin.net/visual-encoding-for-ottoman/">own page</a>.</p>
<p><code>index</code> is a unique identifier assigned to each word. Search keys for Latin, Visenc, and <em>Dotless Visenc</em> (e.g., searching for ﺥ using letters like ح, چ, or ج) are used to locate these lexical items via the tries mentioned earlier.</p>
<p>I’m intentionally avoiding UTF-8 or other Unicode encodings because terminal output is often not well-suited for displaying Arabic text correctly.</p>
<p>When I began writing this software, one of my goals was to enable searching for words by their traditional numeric values (Abjad). These are often used in classical Ottoman poetry to encode a date within a verse. For example, the letter Alif corresponds to 1, Ba (ب) to 2, and so on. To search for words by these numerals—for instance, typing 246 to find words with that total value—we store the value here as well.</p>
<p>The <code>lexical_role</code> is used during translation; currently, we use two roles to distinguish between noun and verb suffixes in Turkish.</p>
<p><code>last_vowel</code>, as the name implies, represents the last vowel of the word. Since vowel harmony in Turkish is not always reflected in Ottoman spelling, we check the last vowel when converting an Ottoman word to its Latin Turkish equivalent to ensure the correct suffix is added (e.g., <em>gemiler</em> instead of <em>gemilar</em>).</p>
<p><code>props</code> is a bit field used to represent various properties of the lexical item, such as:</p>
<pre><code class="language-c">#define HAS_FINAL_VOWEL 0x01
#define HAS_SINGLE_VOWEL 0x02
#define IS_LAST_VOWEL_HARD 0x04
#define IS_FINAL_CONSONANT_HARD 0x08
#define HAS_CONSONANT_SOFTENING 0x10
</code></pre>
<p>These properties are particularly important when converting Ottoman text to modern Turkish.</p>
<p>The current version of the translation engine is written in Python, but I plan to rewrite it in C this week. As this is the third rewrite, I don’t expect it to pose a major algorithmic challenge, although debugging the C version may be more demanding.</p>]]></content:encoded>
    </item>
  </channel>
</rss>
