<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://promathx.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://promathx.github.io/" rel="alternate" type="text/html" /><updated>2026-03-11T14:00:08+00:00</updated><id>https://promathx.github.io/feed.xml</id><title type="html">Controlling complexity is the essence of computer programming.</title><subtitle>everything around cs</subtitle><entry><title type="html">Cpu</title><link href="https://promathx.github.io/cpu/" rel="alternate" type="text/html" title="Cpu" /><published>2026-03-11T00:00:00+00:00</published><updated>2026-03-11T00:00:00+00:00</updated><id>https://promathx.github.io/cpu</id><content type="html" xml:base="https://promathx.github.io/cpu/"><![CDATA[<h1 id="cpu">CPU</h1>
<hr />
<p>layout: post
title: “ CPU “
date: 2026-03-011
—</p>

<p>Central processing unit (CPU, often just <em>processor</em>) is the main, central part of a <a href="computer.md">computer</a>, one that carries out computation by following instructions of the main <a href="program.md">program</a>, colloquially likened to the computer’s “brain”. CPU stands at the center of computer design because other parts (such as the main <a href="ram.md">memory</a>, <a href="hdd.md">hard disk</a> and <a href="io.md">input/output</a> devices like keyboard and monitor) are present to serve the CPU, their master. CPU is normally composed of <a href="alu.md">ALU</a> (arithmetic logic unit, the circuit performing calculations), CU (<a href="control_unit.md">control unit</a>, the circuit that directs the CPU’s operation), a relatively small amount of memory (e.g. its registers, temporary buffers and <a href="cache.md">cache</a>, the main <a href="ram.md">RAM</a> memory is NOT part of a CPU!) and possibly also other components. A specific model of CPU is characterized by its <a href="isa.md">instruction set</a> (ISA, e.g. <a href="x86.md">x86</a> or <a href="arm.md">Arm</a>, which we mostly divide into <a href="cisc.md">CISC</a> and <a href="risc.md">RISC</a>) which subsequently determines the <a href="machine_code.md">machine code</a> it will understand, then by its <a href="transistor.md">transistor</a> count (nowadays billions), operation <a href="frequency.md">frequency</a> or <strong>clock rate</strong> (defining how many instructions per second it executes, nowadays typically billions; the frequency can also be increased with <a href="overclocking.md">overclocking</a>), number of cores (determining how many programs it can run in parallel) and also other parameters and “features” such as amount of <a href="cache.md">cache</a> memory, possible operation modes etcetc. Very commonly we also associate a CPU with a <strong>number of <a href="bit.md">bits</a></strong> (called <em><a href="word.md">word</a> size</em> or something similar) that’s often connected to the data <a href="bus.md">bus</a> width and the CPU’s native integer size, i.e. for example a 16 bit CPU will likely consist of 16 bit integer registers, it will see the memory as a sequence of 16 bit words, its memory addresses may be limited to 16 bits etc. (note that the CPU can still handle even wider words by emulating them with the native words, but this will suffer performance penalties) – nowadays most mainstream CPUs are 64 bit (to allow ungodly amounts of RAM), but 32 or even 16 and 8 bits is usually enough for <a href="lrs.md">good programs</a>. CPU in form of a single small integrated circuit is called <em>microprocessor</em>. CPU is not to be <a href="often_confused.md">confused</a> with <a href="mcu.md">MCU</a>, a small single board computer which is composed of a CPU and other parts.</p>

<p>CPU is meant to perform <strong>general purpose computation</strong>, i.e. it can execute anything reasonably fast but won’t reach near optimum speed at certain specialized tasks (e.g. processing HD video or <a href="3d_rendering.md">rendering 3D graphics</a>), which is why other specialized processing units such as <a href="gpu.md">GPUs</a> (graphics processing unit) and sound cards exist. Because CPU is a general <a href="algorithm.md">algorithm</a> executing unit, it is made for running <strong>linear</strong> programs, i.e. a series of instructions that go one after another; even though today CPUs more often than not sport multiple cores and with it the capability of running several linear programs in parallel, their level of parallelism is still low, not nearly in the same league as a GPU for example. CPUs are nonetheless <a href="good_enough.md">good enough</a> for most tasks and nowadays reach astronomical speeds anyway, so a <a href="suckless.md">suckless</a>/<a href="lrs.md">LRS</a> program will likely choose to only rely on CPU, knowing it’s safe to assume presence of this most essential part of a computer, and by that our program becomes more <a href="portability.md">portable</a> and <a href="future_proof.md">future proof</a>.</p>

<p>Designs of CPUs differ, some may aim to maximize performance while others prefer lower power consumption or low transistor count – remember, a more complex CPU is more expensive because it requires more <a href="transistor.md">transistors</a>! Of course it will also be harder to design, <a href="debugging.md">debug</a> etc., so it may be better to <a href="kiss.md">keep it simple</a> when designing a CPU. For this reason many CPUs, e.g. those in <a href="embedded.md">embedded</a> <a href="mcu.md">microcontrollers</a>, intentionally lack cache, <a href="microcode.md">microcode</a>, multiple cores or even a complex instruction pipeline. Space technology for instance highly prefers reliability before performance.</p>

<p><strong>WATCH OUT</strong>: <a href="modern.md">modern</a> <a href="mainstream.md">mainstream</a> CPUs (practically the ones in desktops and spyphones) are <a href="shit.md">shit</a>, they are hugely <a href="consumerism.md">consumerist</a>, <a href="bloat.md">bloated</a> (they literally include shit like <a href="gpu.md">GPU</a>s and whole <a href="os.md">operating systems</a>, e.g. Intel’s <a href="me.md">ME</a> runs <a href="minix.md">Minix</a>) and have built-in antifeatures such as <a href="backdoor.md">backdoor</a>s (post 2010 basically all Intel and AMD CPUs, see Intel <a href="me.md">Management Engine</a> and AMD <a href="psp.md">PSP</a>) that can’t be disabled and that allow remote infiltration of your computer by the CPU manufacturer (on hardware level, no matter what operating system you run). You are much better off using a simple CPU if you can (<a href="old.md">older</a>, <a href="embedded.md">embedded</a> etc.).</p>

<h2 id="details">Details</h2>

<p>TODO: diagrams, modes, transistor count history …</p>

<p>Let’s take a look at how our average CPU operates. Indeed the techno world is diverse and so we mustn’t assume that anything is set in <a href="rock.md">stone</a>, CPUs vary in many ways. We may also dumb down some concepts a bit, real world CPUs are remarkably overengineered and complicated as hell.</p>

<p>Firstly then the most pressing question: <strong>what is it that a CPU really does?</strong> In essence it just reads instructions from the memory (depending on specific computer architecture this may be <a href="ram.md">RAM</a> or <a href="rom.md">ROM</a>) and does whatever they dictate – these instructions are super simple, often commands like “add two numbers”, “write a number to memory” and so on. The instructions themselves are nothing more than <a href="binary.md">binary</a> data in memory and their format depends on each CPU, or more precisely its <strong><a href="isa.md">instruction set</a></strong> (basically a very low level language it understands) – each CPU, or rather a CPU family, may generally have a different instruction set, so a program in one instruction set can’t be executed by a CPU that doesn’t understand this instruction set. The whole binary program for the CPU is called <strong><a href="machine_code.md">machine code</a></strong> and machine code corresponds to <strong><a href="assembly.md">assembly</a> language</strong> (basically a textual representation of the machine code, for better readability by humans) of the CPU (or better said its instruction set). So a CPU can be seen as a hardware <a href="interpreter.md">interpreter</a> of specific machine code, machine code depends on the instruction set and programmer can create machine code by writing a program in assembly language (which is different for each instruction set) and then using an assembler to translate the program to machine code. Nowadays mostly two instruction sets are used: <a href="x86.md">x86</a> and <a href="arm.md">Arm</a>, but there are also other ones, AND it’s still not so simple because each instruction set gets some kind of updates and/or has some extensions that may or may not be supported by a specific CPU, so it’s a bit messy. For example <a href="ia_32.md">IA-32</a> and <a href="x86_64.md">x86_64</a> are two different versions of the x86 ISA, one 32 bit and one 64 bit.</p>

<p>The CPU has an internal state (we can picture it as a <a href="finite_state_machine.md">state machine</a>), i.e. it has a few internal variables, called <strong><a href="register.md">registers</a></strong>; these are NOT variables in RAM but rather in the CPU itself, there is only a few of them (let’s say 32 for example) but they are stunningly fast, much faster than any other memory. What exactly these registers are, what they are called, how many <a href="bit.md">bits</a> they can hold and what their purpose is depends again on the instruction set architecture. However there are usually a few special registers, notably the <strong>program counter</strong> which holds the address of the currently executed instruction. After executing an instruction program counter is incremented so that in the nest step the next instruction will be executed, AND we can also modify program counter (sometimes directly, sometimes by specialized instructions) to jump between instruction to implement branching, loops, function calls etc.</p>

<p>So at the beginning (when powered on) the CPU is set to some initial state, most notably it sets its program counter to some initial value (depending on each CPU, it may be e.g. 0) so that it points to the first instruction of the program. Then it performs so called <strong>fetch, decode, execute</strong> cycle, i.e. it reads the instruction, decodes what it means and does what it says. In simpler CPUs this functionality is hard wired, however more complex CPUs (especially <a href="cisc.md">CISC</a>) are programmed in so called <a href="microcode.md">microcode</a>, a code yet at the lower level than machine code, machine code execution is programmed in microcode – microcode is something like “<a href="firmware.md">firmware</a> for the CPU” (or a “CPU <a href="shader.md">shader</a>”?), it basically allows later updates and reprogramming of how the CPU internally works. However this is pretty <a href="bloat.md">overcomplicated</a> and you shouldn’t make crappy CPUs like this.</p>

<p>A CPU works in <strong>clock cycles</strong>, i.e. it is a sequential circuit which has so called <em>clock</em> input; on this input voltage periodically switches between high and low (1 and 0) and each change makes the CPU perform another operation cycle. How fast the clock changes is determined by the clock <strong>frequency</strong> (nowadays usually around 3 GHz) – the faster the frequency, the faster the CPU will compute, but the more it will also heat up (so we can’t just set it up arbitrarily high, but we can <a href="overclocking.md">overclock</a> it a bit if we are cooling it down). WATCH OUT: <strong>one clock cycle doesn’t necessarily equal one executed instruction</strong>, i.e. frequency of 1 Hz doesn’t have to mean the CPU will execute 1 instruction per second because executing an instruction may take several cycles (how many depends on each instruction and also other factors). The number saying how many cycles an instruction takes is called CPI (cycles per instruction) – CPUs try to aim for CPI 1, i.e. they try to execute 1 instruction per cycle, but they can’t always do it.</p>

<p>One way to approach CPI 1 is by optimizing the <em>fetch, decode, execute</em> cycle in hardware so that it’s as BLAZINGLY fast as possible. This is typically done by utilizing an instruction <strong><a href="pipeline.md">pipeline</a></strong> – a pipeline has several stages working in parallel so that as soon as one instruction is entering e.g. the <em>decode</em> stage, another one is already coming to the <em>fetch</em> stage (and the previous instruction is in <em>execute</em> stage), i.e. we don’t have to wait for an instruction to be fully processed before starting to process the next one. This is practically the same principle as that of manufacturing lines in factories; if you have a long car manufacturing pipeline, you can make a factory produce let’s say one car each hour, though it is impossible to make a single car from scratch in one hour (or imagine e.g. a university producing new PhDs each year despite no one being able to actually earn PhD in a year). This is also why branching (jumps between instructions) are considered bad for program performance – a jump to different instruction makes the CPU have to throw away its currently preprocessed instruction because that will not be executed (though CPUs again try to deal with this with so called <em>branch prediction</em>, but it can’t work 100%). Some CPUs even have multiple pipelines, allowing for execution of multiple instructions at the same time – however this can only be done sometimes (the latter instruction must be independent of the former, also the other pipelines may be simpler and able to only handle simple instructions).</p>

<p>In order for a CPU to be useful it has to be able to perform some <strong><a href="io.md">input/output</a></strong>, i.e. it has to be able to retrieve data from the outside and present what it has computed. Notable ways of performing I/O are:</p>

<ul>
  <li>Through <strong>memory</strong>: here some parts of memory serve to pass data to the CPU and to retrieve computed results back. For example a keyboard may be mapped to memory so that when certain keys are pressed, the memory bits are set to 1 – this way a CPU can simply read from memory and know if a key is pressed. Similarly a display may be mapped to memory so that when a CPU writes a value to this address, a pixel appears on the display. Note that his doesn’t always have to PHYSICALLY pass through memory, there may be a special circuit that translate e.g. memory access in some address range to signals to hardware etc., but the CPU is using the same instructions it would use for interacting with memory.</li>
  <li>Through <strong><a href="gpio.md">GPIO</a> pins</strong>: CPUs typically have pins that are reserved for general purpose input/output, i.e. we can electronically communicate through them with whatever device we physically connect to those pins. A CPU can set and read voltage to/from those pins e.g. with some special instructions. This may be convenient if we just want to e.g. light up some LED without having to somehow hook it to the main memory.</li>
  <li><strong><a href="interrupt.md">Interrupts</a></strong>: a CPU can be informed about an external event with an interrupt (see further on).</li>
</ul>

<p>CPUs often also have a <strong><a href="cache.md">cache</a></strong> memory that speeds up communication with the main memory (RAM, ROM, …), though simpler CPUs may live even without cache of course. Mainstream CPUs even have several levels of cache, called L1, L2 etc. Caches are basically transparent for the programmer, they don’t have to deal with them, it’s just something that makes memory access faster, however a programmer knowing how a cache works can write code so as to be friendlier to the cache and utilize it better.</p>

<p>Mainstream consoomer CPUs nowadays have multiple <strong><a href="core.md">cores</a></strong> so that each core can essentially run a separate computation in parallel. The separate cores can be seen kind of like duplicate copies of the single core CPU with some connections between them (details again depend on each model), for example cores may share the cache memory, they will be able to communicate with each other etc. Of course this doesn’t just magically make the whole CPU faster, it can now only run multiple computations at once, but someone has to make programs so as to make use of this – typical use cases are e.g. <a href="multitasking.md">multitasking</a> operating systems which can run different programs (or rather processes) on each core (note that multitasking can be done even with a single core by rapidly switching between the processes, but that’s slower), or <a href="multithreading.md">multithreading</a> programming languages which may run each thread on a separate core.</p>

<p><strong><a href="interrupt.md">Interrupts</a></strong> are an important concept for the CPU and for low level programming, they play a role e.g. in saving power – high level programmers often don’t know what interrupts are, to those interrupts can be likened to “event <a href="callback.md">callbacks</a>”. An interrupt occurs on some sort of event, for example upon a key press, when timer ticks, when error occurred etc. (An interrupt can also be raised by the CPU itself, this is how operating system <a href="syscall.md">syscalls</a> are often implemented). What kinds of interrupts there are depends on each CPU architecture (consult your datasheet) and one can usually configure which interrupts to enable and which “callbacks” to use for them – this is often done through so called <strong><a href="vector.md">vector</a> table</strong>, a special area in memory that records addresses (“vectors”) of routines (functions/subprograms) to be called on specified interrupts. When interrupt happens, the current program execution is paused and the CPU automatically jumps to the subroutine for handling the interrupt – after returning from the subroutine the main program execution continues. Interrupts are contrasted with <strong><a href="polling.md">polling</a></strong>, i.e. manually checking some state and handling things as part of the main program, e.g. executing an infinite loop in which we repeatedly check keyboard state until some key is pressed. However polling is inefficient, it wastes power by constantly performing computation just by waiting – interrupts on the other hand are a hard wired functionality that just performs a task when it happens without any overhead of polling. Furthermore interrupts can make programming easier (you save many condition checks and memory reads) and mainly <strong>interrupts allow CPU to go into sleep mode</strong> and so save a lot of power. When a CPU doesn’t have any computation to do, it can stop itself and go into waiting state, not executing any instructions – however interrupts still work and when something happens, the CPU jumps back in to work. This is typically what the <code class="language-plaintext highlighter-rouge">sleep</code>/<code class="language-plaintext highlighter-rouge">wait</code> function in your programming language does – it puts the CPU to sleep and sets a timer interrupt to wake up after given amount of time. As a programmer you should know that you should call this sleep/wait function in your main program loop to relieve the CPU – if you don’t, you will notice the <strong>CPU utilization</strong> (amount of time it is performing computations) will go to 100%, it will heat up, your computer starts spinning the fans and be noisy because you don’t let it rest.</p>

<p>Frequently there are several <strong>modes</strong> of operation in a CPU which is typically meant for <a href="os.md">operating systems</a> – there will usually be some kind of privileged mode in which the CPU can do whatever it wants (this is the mode for the OS kernel) and a restricted mode in which there are “restrictions”, e.g. on which areas of memory can be accessed or which instructions can be used (this will be used for user program). Thanks to this a user program won’t be able to crash the operating system, it will at worst crash itself. Most notably x86 CPUs have the <em>real mode</em> (addresses correspond to real, physical addresses) and <em>protected mode</em> (memory is <a href="virtual_memory.md">virtualized</a>, protected, addresses don’t generally correspond to physical addresses).</p>

<p>A CPU may also have integrated some <strong><a href="coprocessor.md">coprocessors</a></strong>, though sometimes coprocessors are really a separate chip. Coprocessors that may be inside the CPU include e.g. the FPU (<a href="float.md">floating point</a> unit) or encryption coprocessor. Again, this will make the CPU a lot more expensive.</p>

<p>TODOOOOOOO: ALU, virtual memory, IP cores, architectures (register, …), …</p>

<h2 id="notable-cpus">Notable CPUs</h2>

<p>UNDER CONSTRUCTION</p>

<p>Here are listed some notable CPUs (or sometimes CPU families or cores).</p>

<p>{ I’m not so great with HW, suggest me improvements for this section please, thanks &lt;3 ~drummyfish }</p>

<p>{ WTF, allthetropes has quite a big list of famous CPUs, isn’t it a site about movies? https://allthetropes.org/wiki/Central_Processing_Unit. ~drummyfish }</p>

<p>TODO: add more, mark CPUs with ME, add features like MMX, FPU, …</p>

<table>
  <thead>
    <tr>
      <th>CPU</th>
      <th>year</th>
      <th>bits (/a)</th>
      <th>ISA</th>
      <th>~tr. c.</th>
      <th>tr. size</th>
      <th>freq.</th>
      <th>pins</th>
      <th>cores</th>
      <th>other</th>
      <th>notes</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Intel 4004</td>
      <td>1971</td>
      <td>4 / 12</td>
      <td>own</td>
      <td>2.3 K</td>
      <td>10 um</td>
      <td>75O K</td>
      <td>16</td>
      <td>1</td>
      <td> </td>
      <td>1st commercial microproc.</td>
    </tr>
    <tr>
      <td>Intel 8008</td>
      <td>1972</td>
      <td>8 / 14</td>
      <td>own</td>
      <td>3.5 K</td>
      <td>10 um</td>
      <td>800 K</td>
      <td>18</td>
      <td>1</td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td>Intel 8080</td>
      <td>1974</td>
      <td>8 / 16</td>
      <td>own</td>
      <td>6 K</td>
      <td>6 um</td>
      <td>3 M</td>
      <td>40</td>
      <td>1</td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td>AMD Am9080</td>
      <td>1975</td>
      <td>8 / 16</td>
      <td>own</td>
      <td>6 K</td>
      <td>6 um</td>
      <td>4 M</td>
      <td>40</td>
      <td>1</td>
      <td> </td>
      <td>reverse-eng. clone of i8080</td>
    </tr>
    <tr>
      <td>MOS Technology 6502</td>
      <td>1975</td>
      <td>8 / 16</td>
      <td>own</td>
      <td>3.5 K</td>
      <td>8 um</td>
      <td>3 M</td>
      <td>40</td>
      <td>1</td>
      <td> </td>
      <td>popular, cheap, Atari 2600, C64, …</td>
    </tr>
    <tr>
      <td>Zilog Z80</td>
      <td>1976</td>
      <td>8 / 16</td>
      <td>own</td>
      <td>8.5 K</td>
      <td>4 um</td>
      <td>10 M</td>
      <td>40</td>
      <td>1</td>
      <td> </td>
      <td>popular</td>
    </tr>
    <tr>
      <td>Intel 8086</td>
      <td>1978</td>
      <td>16 / 20</td>
      <td>x86 (x86-16)</td>
      <td>29 K</td>
      <td>3 um</td>
      <td>10 M</td>
      <td>40</td>
      <td>1</td>
      <td> </td>
      <td>started x86 ISA</td>
    </tr>
    <tr>
      <td>Motorola 68000</td>
      <td>1979</td>
      <td>32 / 24</td>
      <td>own (CISC)</td>
      <td>68 K</td>
      <td> </td>
      <td> </td>
      <td>64</td>
      <td>1</td>
      <td> </td>
      <td>popular, e.g. Amiga, Mega Drive, …</td>
    </tr>
    <tr>
      <td>Intel (80)286</td>
      <td>1982</td>
      <td>16 / 24</td>
      <td>x86 (x86-16)</td>
      <td>130 K</td>
      <td>1.5 um</td>
      <td>25 M</td>
      <td>68</td>
      <td>1</td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td>Intel (80)386</td>
      <td>1985</td>
      <td>32</td>
      <td>x86 (IA-32)</td>
      <td>275 K</td>
      <td>1 um</td>
      <td>40 M</td>
      <td>132</td>
      <td>1</td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td>Intel (80)486</td>
      <td>1989</td>
      <td>32</td>
      <td>x86 (IA-32)</td>
      <td>1.6 M</td>
      <td>600 nm</td>
      <td>100 M</td>
      <td>196</td>
      <td>1</td>
      <td>16 K cache, FPU</td>
      <td>1st intel with cache and FPU</td>
    </tr>
    <tr>
      <td>AMD Am386</td>
      <td>1991</td>
      <td>32</td>
      <td>x86 (IA-32)</td>
      <td>275 K</td>
      <td>800 nm</td>
      <td>40 M</td>
      <td>132</td>
      <td>1</td>
      <td> </td>
      <td>clone of i386, lawsuit</td>
    </tr>
    <tr>
      <td>Intel Pentium P5</td>
      <td>1993</td>
      <td>32</td>
      <td>x86 (IA-32)</td>
      <td>3 M</td>
      <td>800 nm</td>
      <td>60 M</td>
      <td>273</td>
      <td>1</td>
      <td>16 K cache</td>
      <td>starts Pentium line with many to follow</td>
    </tr>
    <tr>
      <td>AMD K5</td>
      <td>1996</td>
      <td>32</td>
      <td>x86 (IA-32)</td>
      <td>4.3 M</td>
      <td>500 nm</td>
      <td>133 M</td>
      <td>296</td>
      <td>1</td>
      <td>24 K cache</td>
      <td>1st in-house AMD CPU, compet. of Pentium</td>
    </tr>
    <tr>
      <td>Intel Pentium II</td>
      <td>1997</td>
      <td>32</td>
      <td>x86 (IA-32)</td>
      <td>7 M</td>
      <td>180 nm</td>
      <td>450 M</td>
      <td>240</td>
      <td>1</td>
      <td>512 K L2 cache, MMX</td>
      <td> </td>
    </tr>
    <tr>
      <td>ARM7TDMI</td>
      <td>1994</td>
      <td>32</td>
      <td>ARM</td>
      <td> </td>
      <td> </td>
      <td>100 M</td>
      <td> </td>
      <td>1</td>
      <td> </td>
      <td>ARM core, e.g. GBA, PS2, Nokia 6110 …</td>
    </tr>
    <tr>
      <td>AMD Athlon 1000 Thunderbird</td>
      <td>2000</td>
      <td>32</td>
      <td>x86 (IA-32)</td>
      <td>37 M</td>
      <td>180 nm</td>
      <td>1 G</td>
      <td>453</td>
      <td>1</td>
      <td>~300 K cache</td>
      <td>1st 1GHz+ CPU</td>
    </tr>
    <tr>
      <td>RAD750</td>
      <td>2001</td>
      <td>32</td>
      <td>PowerPC</td>
      <td>10 M</td>
      <td>150 nm</td>
      <td>200 M</td>
      <td>360</td>
      <td>1</td>
      <td>64 K cache</td>
      <td>radiation hard., space (Curiosity, …)</td>
    </tr>
    <tr>
      <td>AMD Opteron</td>
      <td>2003</td>
      <td>64</td>
      <td>x86 (x86-64)</td>
      <td>105 M</td>
      <td>130 nm</td>
      <td>1.6 G</td>
      <td>940</td>
      <td>1</td>
      <td>~1 M cache</td>
      <td>1st 64 bit x86 CPU</td>
    </tr>
    <tr>
      <td>Intel Pentium D 820</td>
      <td>2005</td>
      <td>64</td>
      <td>x86 (x86-64)</td>
      <td>230 M</td>
      <td>90 nm</td>
      <td>2.8 G</td>
      <td>775</td>
      <td>2</td>
      <td>~2 M cache</td>
      <td>1st desktop multi core CPU</td>
    </tr>
    <tr>
      <td>Intel Core i5-2500K</td>
      <td>2011</td>
      <td>64</td>
      <td>x86 (x86-64)</td>
      <td>1 B</td>
      <td>32 nm</td>
      <td>3.3 G</td>
      <td> </td>
      <td>4</td>
      <td>~6 M cache, ME</td>
      <td> </td>
    </tr>
    <tr>
      <td>PicoRV32</td>
      <td>2015?</td>
      <td>32</td>
      <td>RISC-V (RV32IMC)</td>
      <td> </td>
      <td> </td>
      <td>~700 M</td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td>simple, free hardware RISV-V core</td>
    </tr>
    <tr>
      <td>Apple A9</td>
      <td>2015</td>
      <td>64</td>
      <td>ARM (ARMv8)</td>
      <td>2 B</td>
      <td>14 nm</td>
      <td>1.8 G</td>
      <td> </td>
      <td>2</td>
      <td>~7 M cache</td>
      <td>iPhones</td>
    </tr>
    <tr>
      <td>AMD Ryzen Threadrip. PRO 5995WX</td>
      <td>2022</td>
      <td>64</td>
      <td>x86 (x86-64)</td>
      <td>33 B</td>
      <td>7 nm</td>
      <td>4.5 G</td>
      <td>4094</td>
      <td>64</td>
      <td>~300 M cache, PSP</td>
      <td>high end bloat</td>
    </tr>
    <tr>
      <td><a href="talos_es.md">Talos ES</a></td>
      <td>2023</td>
      <td>8</td>
      <td>own (RISC)</td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td>simple but usable DIY free hardware CPU</td>
    </tr>
  </tbody>
</table>

<h2 id="see-also">See Also</h2>

<ul>
  <li><a href="gpu.md">GPU</a></li>
  <li><a href="ppu.md">PPU</a></li>
  <li><a href="fpu.md">FPU</a></li>
  <li><a href="mcu.md">MCU</a></li>
  <li><a href="alu.md">ALU</a></li>
  <li><a href="wpu.md">WPU</a> (weird processing unit)</li>
  <li><a href="computer.md">computer</a></li>
</ul>]]></content><author><name></name></author><summary type="html"><![CDATA[CPU layout: post title: “ CPU “ date: 2026-03-011 —]]></summary></entry><entry><title type="html">Essential security for Linux servers</title><link href="https://promathx.github.io/essential-security-for-linux-servers/" rel="alternate" type="text/html" title="Essential security for Linux servers" /><published>2025-08-20T00:00:00+00:00</published><updated>2025-08-20T00:00:00+00:00</updated><id>https://promathx.github.io/essential-security-for-linux-servers</id><content type="html" xml:base="https://promathx.github.io/essential-security-for-linux-servers/"><![CDATA[<p><br /></p>

<p>This small post will go through the baseline on how to secure a freshly installed Debian based Linux distro. The aim of this approach is to create an acceptable security baseline while not adding extra layers of complexity.</p>

<div class="language-sh highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c"># connect to the server</span>
ssh root@[IP-adress]

</code></pre></div></div>

<p>set the correct timezone and enable network time sync and update the OS.</p>

<div class="language-sh highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c"># set the time zone</span>
dpkg-reconfigure tzdata

<span class="c"># enable network time sync</span>
timedatectl set-ntp <span class="nb">true</span>

<span class="c"># update the repositories</span>
apt update

<span class="c"># upgrade all the packages</span>
apt upgrade

</code></pre></div></div>

<p>Connecting to the root account via ssh is highly discourged. Create an extra user for ssh.</p>

<div class="language-sh highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c"># create the new user</span>
useradd <span class="nt">--create-home</span> <span class="nt">--shell</span> /bin/bash <span class="o">[</span>USERNAME]

<span class="c"># add the user to the sudoers</span>
usermod <span class="nt">-aG</span> <span class="nb">sudo</span> <span class="o">[</span>USERNAME]

<span class="c"># change the password</span>
passwd <span class="o">[</span>USERNAME]

</code></pre></div></div>

<p>Tweak the sshd config according to our security measures.</p>

<div class="language-sh highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c"># edit /etc/ssh/sshd_config</span>
Port 3392
PermitRootLogin no
PubkeyAuthentication <span class="nb">yes
</span>PasswordAuthentication no
AllowUsers <span class="o">[</span>USERNAME]



<span class="c">#As an additional security measure, if you only log into the server from a specific IP address, you can lock down the openssh service even more by changing the last line to this</span>

...
AllowUsers <span class="o">[</span>USERNAME]@[IP-ADDRESS]

<span class="c">#Just make sure you have a static address or you won’t be able to login into the server if your IP changes.</span>
</code></pre></div></div>

<p>A brief explaination of the changes above (in order):</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>[x] - Change the default port to reduce some of the noise caused by bots trying to brute force our openssh server on port 22

[x] - Disable login via ssh using the root account

[x] - Enable public key authentication (we are going to generate a key pair to log into the server)

[x] - Disable password authentication

[x] - Permit login via ssh only to the newly created user
</code></pre></div></div>

<p>Now that the ssh service is secured, lets add our public key to the server to be able to login. With our public key ready we need to create the file structure, add the key to the authorized_keys file and assign the correct permissions:</p>

<div class="language-sh highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c"># create the .ssh folder in the new user home</span>
<span class="nb">mkdir</span> /home/[USERNAME]/.ssh

<span class="c"># restric access to it</span>
<span class="nb">chmod </span>700 /home/[USERNAME]/.ssh

<span class="c"># paste your public key into the authorized_keys file</span>
nano /home/[USERNAME]/.ssh/authorized_keys

<span class="c"># lock down access to the key file</span>
<span class="nb">chmod </span>400 /home/[USERNAME]/.ssh/authorized_keys

<span class="c"># change ownership of the .ssh folder recursively</span>
<span class="nb">chown</span> <span class="o">[</span>USERNAME]:[USERNAME] <span class="nt">-R</span> /home/[USERNAME]/.ssh


</code></pre></div></div>
<p>Next step: We are enabling the firewall to block any unsolicited incoming traffic.</p>

<div class="language-sh highlighter-rouge"><div class="highlight"><pre class="highlight"><code>
<span class="c"># open port 3392 on the firewall</span>
ufw allow 3392/tcp

<span class="c"># enable the firewall at boot</span>
ufw <span class="nb">enable</span>

<span class="c"># check the firewall status</span>
ufw status

</code></pre></div></div>

<p>Reload the daemon to test tour config.</p>

<div class="language-sh highlighter-rouge"><div class="highlight"><pre class="highlight"><code>
systemctl reload sshd

</code></pre></div></div>

<p>At this stage it’s good practice to fire up a new terminal window (while keeping the old ssh session open) to check that we are able to connect using our new user and public key:</p>

<div class="language-sh highlighter-rouge"><div class="highlight"><pre class="highlight"><code>ssh <span class="nt">-p</span> 3392 <span class="o">[</span>USERNAME]@[IP-ADDRESS]

</code></pre></div></div>

<p>If the login fails we can still use the previous terminal session to debug the issue. If the connection works fine we can disconnect and go back to our old session.</p>

<p>In the next step we are going to install and configure two new packages.</p>

<p>The first one, called fail2ban, is a monitoring service that constantly scans the system logs and temporarily bans users via the system firewall if it detects suspicious activity (for instance too many failed ssh log in attempts from a specific IP address over a specified time range).</p>

<p>The second package, called ssmtp, is a simple smtp client that will be used by fail2ban to notify us via email.</p>

<div class="language-sh highlighter-rouge"><div class="highlight"><pre class="highlight"><code>apt <span class="nb">install </span>fail2ban ssmtp

</code></pre></div></div>

<p>To send emails from the server you can use any smtp relay you have access to.</p>

<div class="language-sh highlighter-rouge"><div class="highlight"><pre class="highlight"><code>
<span class="c"># /etc/ssmtp/ssmtp.conf</span>
<span class="nv">root</span><span class="o">=</span>postmaster
<span class="nv">mailhub</span><span class="o">=</span>mail
<span class="nb">hostname</span><span class="o">=[</span>HOSTNAME]
<span class="nv">FromLineOverride</span><span class="o">=</span>YES

<span class="c"># smtp server</span>
<span class="nv">mailhub</span><span class="o">=[</span>SMTP]:[PORT]
<span class="nv">UseSTARTTLS</span><span class="o">=</span><span class="nb">yes
</span><span class="nv">UseTLS</span><span class="o">=</span><span class="nb">yes
</span><span class="nv">AuthUser</span><span class="o">=[</span>EMAIL]
<span class="nv">AuthPass</span><span class="o">=[</span>PASSWORD]


<span class="c"># edit /etc/ssmtp/revaliases</span>
root:[EMAIL]:[SMTP]:[PORT]
postmaster:[EMAIL]:[SMTP]:[PORT]
<span class="o">[</span>USERNAME]:[EMAIL]:[SMTP]:[PORT]


</code></pre></div></div>

<p>Fail2ban uses jail files to configure its behaviour so we need to copy the sample file to create our configuration:</p>

<div class="language-sh highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">cp</span> /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

</code></pre></div></div>

<p>Now that we have a new jail file we can tweak the configuration, it’s always a good idea to check the fail2ban man page for a detailed explaination of the settings:</p>

<div class="language-sh highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c"># edit /etc/fail2ban/jail.local</span>

<span class="c"># to email address</span>
destemail <span class="o">=</span> <span class="o">[</span>EMAIL]

<span class="c"># from email address</span>
sender <span class="o">=</span> <span class="o">[</span>EMAIL]

mta <span class="o">=</span> sendmail

<span class="c"># tweak the action to get additional info in the email</span>
action <span class="o">=</span> %<span class="o">(</span>action_mwl<span class="o">)</span>s

<span class="c"># enable the sshd jail to monitor ssh connections</span>
<span class="o">[</span>sshd]
enabled <span class="o">=</span> <span class="nb">true</span>

</code></pre></div></div>

<p>Finally we can start the fail2ban service:</p>

<div class="language-sh highlighter-rouge"><div class="highlight"><pre class="highlight"><code>systemctl start fail2ban

</code></pre></div></div>
<p>Last step is to setup automatic updates to avoid security breaches caused by vulnerabilities in out-of-date packages.</p>

<div class="language-sh highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c"># /etc/apt/apt.conf.d/10periodic</span>
APT::Periodic::Update-Package-Lists <span class="s2">"1"</span><span class="p">;</span>
APT::Periodic::Download-Upgradeable-Packages <span class="s2">"1"</span><span class="p">;</span>
APT::Periodic::AutocleanInterval <span class="s2">"7"</span><span class="p">;</span>
APT::Periodic::Unattended-Upgrade <span class="s2">"1"</span><span class="p">;</span>


<span class="c">#Then we enable the unattended upgrades:</span>
<span class="c"># /etc/apt/apt.conf.d/50unattended-upgrades</span>
Unattended-Upgrade::Allowed-Origins <span class="o">{</span>
...
<span class="c"># comment-in the updates</span>
<span class="s2">"</span><span class="k">${</span><span class="nv">distro_id</span><span class="k">}</span><span class="s2">:</span><span class="k">${</span><span class="nv">distro_codename</span><span class="k">}</span><span class="s2">-updates"</span><span class="p">;</span>
...
<span class="o">}</span>

<span class="c"># comment-in and enable automatic reboot</span>
Unattended-Upgrade::Automatic-Reboot <span class="s2">"true"</span><span class="p">;</span>

<span class="c"># comment-in the reboot time</span>
Unattended-Upgrade::Automatic-Reboot-Time <span class="s2">"02:00"</span><span class="p">;</span>

</code></pre></div></div>]]></content><author><name></name></author><summary type="html"><![CDATA[]]></summary></entry><entry><title type="html">Logic</title><link href="https://promathx.github.io/logic/" rel="alternate" type="text/html" title="Logic" /><published>2025-07-30T00:00:00+00:00</published><updated>2025-07-30T00:00:00+00:00</updated><id>https://promathx.github.io/logic</id><content type="html" xml:base="https://promathx.github.io/logic/"><![CDATA[<h1 id="logic">Logic</h1>

<p>Logic (from Greek <em>logos</em> – thought/reason/word) is the study of rational reasoning. Logic as such is a term very general – there is common sense logic, philosophical logic, formal and <a href="math.md">mathematical</a> logic, deductive and inductive logic, logic in <a href="logic_circuit.md">circuits</a>, <a href="programming.md">programming</a>, <a href="fuzzy.md">fuzzy</a> logic and many other kinds of logic. Logic is closely intertwined with <a href="math.md">mathematics</a> because mathematics is built on top of logic and adopts many of its tools and concepts, for example axiomatic systems, and in turn mathematicians help further develop deeper knowledge about logic with tools they obtained. As always, here we will focus mainly on logic from programmer’s point of view.</p>

<p>TODO: moar stuff here, different orders of logic etc.</p>

<p>Formal logic is also usable as one of many <strong>programming <a href="paradigm.md">paradigms</a></strong> – a typical example of logic programming language is <a href="prolog.md">Prolog</a>.</p>

<p><strong>Power of logic is limited</strong> (for more please read this excellent resource: http://humanknowledge.net/Thoughts.html) – though logic presents the strongest and most stable platform for knowledge to stand on, it is still not infinitely powerful and indeed does have its limits, despite what any reddit <a href="atheism.md">atheist</a> tells you or even what he believes. This sadly <a href="doom.md">dooms</a> us to certain eternal inability to uncover all there is, we are forced to accept that from a certain point we are completely blind and not even logic will help us see further. <a href="godel.md">Kurt Godel</a> (along with others, e.g. Tarski) mathematically proved, with his <a href="incompleteness.md">incompleteness theorems</a>, that we simply won’t be able to prove everything, not even the validity of formal tools we use to prove things. More specifically he showed that there must exist statements whose truthfulness we’ll be fundamentally unable to ever prove OR disprove, that we’ll forever remain in uncertainty about their truthfulness, and that we won’t even be able to prove that what we CAN prove is indeed correct because we cannot prove the logic we used for the proof is correct (for more details please see also <a href="knowability.md">knowability</a>). Even just in intuitive terms: on the lowest level we start using logic to talk about itself, i.e. if we for instance try to prove that “logic works” using logical arguments, we cannot ever succeed, because if we succeed, the proven fact that “logic works” relies on the fact that logic indeed works; if it perhaps doesn’t work and we used it to prove its own validity, we might have simply gotten a wrong result (it’s just as if we trust someone saying “I am not a liar”, he may as well be lying about not being a liar). By this logic even the previous sentence may or may not actually be true, we simply don’t know, sometimes the best we can do is simply hold on to stronger or weaker beliefs. Imagine we have a function <em>isTrue(x)</em> that automatically checks if statement <em>x</em> is true (returns <em>true</em> or <em>false</em>), now image we have statement <em>y</em> that says <em>isTrue(y) = false</em>; our <em>isTrue</em> function will fail to correctly evaluate statement <em>y</em> (it can’t return neither <em>true</em> nor <em>false</em>, both will lead to contradiction) – this is a proof that there can never be a computable function that in all cases correctly decides whether something is true or not. Logic furthermore cannot talk about many other things; it can tell us how the world works but not for example WHY it works like it does or whether it will ever start working differently. Checkmate <a href="atheist.md">atheists</a>.</p>

<h2 id="what-is-logic-in-its-essence-how-universal-is-it-and-other-mysteries">What Is Logic In Its Essence? How Universal Is It? And Other Mysteries</h2>

<p>{ Thought diarrhea incoming, beware of unoriginal research. ~drummyfish }</p>

<p>Are laws of logic inherent to every kind of existence, constant and unchanging under all possible conditions or are they specific to our <a href="universe.md">Universe</a> and something we simply learn empirically like anything else? Is logic fundamentally different from any other kind of knowledge or “skill”? I.e. it seems logical that for example if <em>A</em> implies <em>B</em> and <em>A</em> holds, <em>B</em> will hold too – but does it always have to be so or is it just so around our specific spacetime coordinates and we simply LEARN this rule, accepting it as a general interdimensional law? It seems logical that putting two individual <a href="rock.md">rocks</a> together forms a single pair of rocks, but maybe in another universe putting two rocks together forms three rocks – would beings in this universe have different laws of logic and different mathematics? This is probably a great philosophical question we can’t satisfyingly answer here, but quite likely it may be the case that such questions <a href="knowability.md">cannot be answered</a> at all.</p>

<p>Indeed, what even is logic? It may be defined as a language, way of thought, the set of lowest level laws of <a href="math.md">mathematics</a> and in many other ways, but how to grasp the true MEANING of what’s logical and what isn’t? Let us try to look at the problem from one possible perspective – that of <a href="compsci.md">computer science</a> – and define logic as the most fundamental part of the <a href="algorithm.md">algorithm</a> that our brains run, the most “basic code” for how a brain thinks. Logic to us is now just a “program” executed by our brains in order to make a decision in life (and note this will apply to computation in general without loss of generality, as computing any <a href="number.md">number</a> can be broken down to deciding the value of its individual <a href="binary.md">binary</a> digits). It is clear that each human’s brain runs a slightly different algorithm due to different <a href="dna.md">DNA</a>, physical structure of brain matter, upbringing, life experience, learned biases and so on, meaning there are many problems that different humans solve (decide, compute, …) differently. Logic is however the part of this grand algorithm that is somehow shared by at least MOST humans, i.e. it’s that part of the algorithm that decides problems so that MOST humans will agree on the solution (because they share the “decision code” and so presented with the same input have to arrive at the same output). In this sense we might view logic as a “<a href="bios.md">BIOS</a>” or perhaps an “<a href="os.md">operating system</a>” that comes preinstalled in all brains and serves the purpose of a “platform”, enabling us to acquire more knowledge (“install more software”) – it’s a very universal code and so it’s the same for (almost) everyone, and as the brains live, they use logic to deduce new knowledge, to “install” and “personalize” the whole brain software, but the basic code at the bottom remains, and that is logic. Now it is also clear that there are humans, whom we might call “insane” or “stupid”, who use faulty logic or don’t accept or understand basic logic, and the same probably goes for almost all animals – these individuals simply have a different “base code” of logic. This is in line with the “you can’t argue with a retard” rule, it IS impossible to force someone to believe logic if he simply refuses to, you can’t logically prove your logic correct if you’re using the very same logic for the proof.</p>

<p>Where did this “base code” in brain come from? It probably <a href="evolution.md">evolved</a> like anything else: by natural selection. We, with the kind of logic we all use, are the result of nature’s long trial and error process leading to organisms that survive and reproduce. In other words we could say that logic isn’t anything magical and different from any other knowledge, but simply a “<a href="good_enough.md">good enough</a>” universal algorithm, which when installed in an organism’s brain leads to the organism surviving and thriving, mainly because this base code allows adaptation, deduction and acquiring of new <a href="abstraction.md">abstract</a> knowledge. Note the term “good enough” however: it’s used to show that our logic may indeed be faulty and fail sometimes, and like mentioned above, we cannot ever prove correctness of our own logic, we use it simply because “<a href="just_werks.md">it works</a>” for us, but it may be the case that our logic would fail us in other parts of the universe, that we could mathematically prove a falsehood true. By this we can hypothesize that logic, like other very basic brain functions, is “preprogrammed” and “hard wired” in a newborn’s brain, instilled by evolution, and that’s why we all share logic and can agree on what’s fundamentally logical. It could even theoretically be possible that our civilization’s logic is incorrect and a mutation occurred in some lone human to use a different, CORRECT system of logic, but we see him as a schizophrenic and lock him up in an asylum, when he’s in fact right and the whole world is wrong.</p>

<p>We don’t know if our logic is always correct or not, we only know it’s useful to us, but we DO know (from the incompleteness theorems) that our logic is limited and will be unusable to prove certain specific problems. And this immediately begs the question: is our logic the only possible one? Could there be an organism that would have evolved a different kind of logic, perhaps one that could prove the problems we cannot prove with our logic? All points to the answer that yes, this is possible, but then of course this new logic will have its own set of undecidable problems, ones that OUR logic may be able to decide. But wait – if we met with the aliens with this kind of different logic, couldn’t we collaborate on deciding ALL problems? Couldn’t they solve our undecidable problem for us in return solving theirs? No, we couldn’t understand each other’s logic and we couldn’t trust their solutions, it might well be that we meet aliens with faulty logic and the solutions they provide to us would be wrong – remember, we couldn’t verify correctness of solutions to our undecidable problems with our logic. This also make sense if we imagine us and the aliens forming a new metaorganism that uses a new kind of logic, or ALGORITHM, by merging our logic and the aliens’ – in the end a combination of algorithms is again a single algorithm and therefore must also suffer the curse of undecidablity etc. So to summarize:</p>

<ul>
  <li>Out logic most likely came from evolution.</li>
  <li>We don’t know if our logic is correct.</li>
  <li>We know it is useful and so we assume it’s at least mostly correct.</li>
  <li>We know our logic is limited (won’t be able to answer some questions).</li>
  <li>There can probably exist different systems of logic, similar to our own.</li>
  <li>Trying to use many different systems of logic together however won’t help, in the end any merger is just a new logic with its own flaws.</li>
</ul>

<p>Let’s now poke a bit on specific examples of different logic systems, demonstrated by a table:</p>

<table>
  <thead>
    <tr>
      <th>logic</th>
      <th>values</th>
      <th>correct?</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>ground truth (undecidable, answers everything)</td>
      <td><code class="language-plaintext highlighter-rouge">100110101100010011010...</code></td>
      <td>yes, by definition</td>
    </tr>
    <tr>
      <td>logic A (answers nothing)</td>
      <td><code class="language-plaintext highlighter-rouge">?????????????????????...</code></td>
      <td>yes, can’t be wrong</td>
    </tr>
    <tr>
      <td>logic B (always tries to give an answer)</td>
      <td><code class="language-plaintext highlighter-rouge">011000111010011100010...</code></td>
      <td>no, can’t be</td>
    </tr>
    <tr>
      <td>logic C (answers something)</td>
      <td><code class="language-plaintext highlighter-rouge">??????1????0?????0?1?...</code></td>
      <td>yes</td>
    </tr>
    <tr>
      <td>logic D (negation of logic C)</td>
      <td><code class="language-plaintext highlighter-rouge">??????0????1?????1?0?...</code></td>
      <td>no</td>
    </tr>
    <tr>
      <td>logic E (answers something)</td>
      <td><code class="language-plaintext highlighter-rouge">10010?101100?10011?10...</code></td>
      <td>yes</td>
    </tr>
    <tr>
      <td>logic F (answers something)</td>
      <td><code class="language-plaintext highlighter-rouge">100?001011000100?1?10...</code></td>
      <td>yes</td>
    </tr>
    <tr>
      <td>logic G (similar to logic F)</td>
      <td><code class="language-plaintext highlighter-rouge">100?011011001100?1?10...</code></td>
      <td>no</td>
    </tr>
    <tr>
      <td>…</td>
      <td>…</td>
      <td>…</td>
    </tr>
  </tbody>
</table>

<p>The <em>logic</em> column lists a selection of possible logic systems (correct or not), and the first one is <em>ground truth</em>, i.e. the definitive correct answer to all decision problems, which however cannot be attained in our universe due to incompleteness theorems – it serves here as a reference to compare other systems of logic to. The <em>values</em> column shows an answer to some subset of questions we might ask – each question has a definitive answer <code class="language-plaintext highlighter-rouge">1</code> (true) or <code class="language-plaintext highlighter-rouge">0</code> (false), given by <em>ground truth</em>, but logic systems are allowed to also answer <code class="language-plaintext highlighter-rouge">?</code>, or “I don’t know”. This is very important because by definition ANY computable logic that always gives a definitive <code class="language-plaintext highlighter-rouge">1</code>/<code class="language-plaintext highlighter-rouge">0</code> answer, like <em>logic B</em>, HAS TO be wrong at least in some cases, and so the only systems that can be correct are those that sometimes admit they don’t know the answer, i.e. output <code class="language-plaintext highlighter-rouge">?</code>. This proves <em>logic A</em> correct as that simply always answers <code class="language-plaintext highlighter-rouge">?</code>, it’s practically equivalent to a lack of logic – if you never answer, you can never be wrong. Then there is <em>logic C</em> that demonstrates this: it answers only a few questions but always correctly (maybe it’s not very powerful and only limits itself to answering just very trivial questions). Our human logic could perhaps be <em>logic E</em>: it answers many questions but also doesn’t know ALL of the answers, which is a prerequisite for correctness. The alien logic might be <em>logic F</em>, which is very similar and differs in (correctly) answering some of our (<em>logic E’s</em>) questions, but for giving up the ability to answer something we can. Now note <em>logic G</em> that’s almost the same as <em>logic F</em> – it might be yet a different alien civilization logic – it similarly answers some of our unanswerable questions, but WRONGLY, and this is why we can’t trust the aliens: for us there is no way to decide if we should trust <em>logic F</em> or <em>logic G</em> on questions where their answers differ. And note here that <em>logic G</em> itself also can’t know that it’s incorrect, it’s giving many correct answers but also a few wrong ones, and from evolutionary perspective this may not matter because they may be highly abstract questions unimportant for survival, and so it may well be the case that our logic is <em>logic G</em>: a good, useful logic, but sometimes a faulty one, and we locked a poor innocent man in an asylum.</p>]]></content><author><name></name></author><summary type="html"><![CDATA[Logic]]></summary></entry><entry><title type="html">Cpu</title><link href="https://promathx.github.io/cpu/" rel="alternate" type="text/html" title="Cpu" /><published>2025-07-15T00:00:00+00:00</published><updated>2025-07-15T00:00:00+00:00</updated><id>https://promathx.github.io/cpu</id><content type="html" xml:base="https://promathx.github.io/cpu/"><![CDATA[<h1 id="cpu">CPU</h1>

<p>WORK IN PROGRESS</p>

<p>Central processing unit (CPU, often just <em>processor</em>) is the main, central part of a <a href="computer.md">computer</a>, one that carries out computation by following instructions of the main <a href="program.md">program</a>, colloquially likened to the computer’s “brain”. CPU stands at the center of computer design because other parts (such as the main <a href="ram.md">memory</a>, <a href="hdd.md">hard disk</a> and <a href="io.md">input/output</a> devices like keyboard and monitor) are present to serve the CPU, their master. CPU is normally composed of <a href="alu.md">ALU</a> (arithmetic logic unit, the circuit performing calculations), CU (<a href="control_unit.md">control unit</a>, the circuit that directs the CPU’s operation), a relatively small amount of memory (e.g. its registers, temporary buffers and <a href="cache.md">cache</a>, the main <a href="ram.md">RAM</a> memory is NOT part of a CPU!) and possibly also other components. A specific model of CPU is characterized by its <a href="isa.md">instruction set</a> (ISA, e.g. <a href="x86.md">x86</a> or <a href="arm.md">Arm</a>, which we mostly divide into <a href="cisc.md">CISC</a> and <a href="risc.md">RISC</a>) which subsequently determines the <a href="machine_code.md">machine code</a> it will understand, then by its <a href="transistor.md">transistor</a> count (nowadays billions), operation <a href="frequency.md">frequency</a> or <strong>clock rate</strong> (defining how many instructions per second it executes, nowadays typically billions; the frequency can also be increased with <a href="overclocking.md">overclocking</a>), number of cores (determining how many programs it can run in parallel) and also other parameters and “features” such as amount of <a href="cache.md">cache</a> memory, possible operation modes etcetc. Very commonly we also associate a CPU with a <strong>number of <a href="bit.md">bits</a></strong> (called <em><a href="word.md">word</a> size</em> or something similar) that’s often connected to the data <a href="bus.md">bus</a> width and the CPU’s native integer size, i.e. for example a 16 bit CPU will likely consist of 16 bit integer registers, it will see the memory as a sequence of 16 bit words, its memory addresses may be limited to 16 bits etc. (note that the CPU can still handle even wider words by emulating them with the native words, but this will suffer performance penalties) – nowadays most mainstream CPUs are 64 bit (to allow ungodly amounts of RAM), but 32 or even 16 and 8 bits is usually enough for <a href="lrs.md">good programs</a>. CPU in form of a single small integrated circuit is called <em>microprocessor</em>. CPU is not to be <a href="often_confused.md">confused</a> with <a href="mcu.md">MCU</a>, a small single board computer which is composed of a CPU and other parts.</p>

<p>CPU is meant to perform <strong>general purpose computation</strong>, i.e. it can execute anything reasonably fast but won’t reach near optimum speed at certain specialized tasks (e.g. processing HD video or <a href="3d_rendering.md">rendering 3D graphics</a>), which is why other specialized processing units such as <a href="gpu.md">GPUs</a> (graphics processing unit) and sound cards exist. Because CPU is a general <a href="algorithm.md">algorithm</a> executing unit, it is made for running <strong>linear</strong> programs, i.e. a series of instructions that go one after another; even though today CPUs more often than not sport multiple cores and with it the capability of running several linear programs in parallel, their level of parallelism is still low, not nearly in the same league as a GPU for example. CPUs are nonetheless <a href="good_enough.md">good enough</a> for most tasks and nowadays reach astronomical speeds anyway, so a <a href="suckless.md">suckless</a>/<a href="lrs.md">LRS</a> program will likely choose to only rely on CPU, knowing it’s safe to assume presence of this most essential part of a computer, and by that our program becomes more <a href="portability.md">portable</a> and <a href="future_proof.md">future proof</a>.</p>

<p>Designs of CPUs differ, some may aim to maximize performance while others prefer lower power consumption or low transistor count – remember, a more complex CPU is more expensive because it requires more <a href="transistor.md">transistors</a>! Of course it will also be harder to design, <a href="debugging.md">debug</a> etc., so it may be better to <a href="kiss.md">keep it simple</a> when designing a CPU. For this reason many CPUs, e.g. those in <a href="embedded.md">embedded</a> <a href="mcu.md">microcontrollers</a>, intentionally lack cache, <a href="microcode.md">microcode</a>, multiple cores or even a complex instruction pipeline. Space technology for instance highly prefers reliability before performance.</p>

<p><strong>WATCH OUT</strong>: <a href="modern.md">modern</a> <a href="mainstream.md">mainstream</a> CPUs (practically the ones in desktops and spyphones) are <a href="shit.md">shit</a>, they are hugely <a href="consumerism.md">consumerist</a>, <a href="bloat.md">bloated</a> (they literally include shit like <a href="gpu.md">GPU</a>s and whole <a href="os.md">operating systems</a>, e.g. Intel’s <a href="me.md">ME</a> runs <a href="minix.md">Minix</a>) and have built-in antifeatures such as <a href="backdoor.md">backdoor</a>s (post 2010 basically all Intel and AMD CPUs, see Intel <a href="me.md">Management Engine</a> and AMD <a href="psp.md">PSP</a>) that can’t be disabled and that allow remote infiltration of your computer by the CPU manufacturer (on hardware level, no matter what operating system you run). You are much better off using a simple CPU if you can (<a href="old.md">older</a>, <a href="embedded.md">embedded</a> etc.).</p>

<h2 id="details">Details</h2>

<p>TODO: diagrams, modes, transistor count history …</p>

<p>Let’s take a look at how our average CPU operates. Indeed the techno world is diverse and so we mustn’t assume that anything is set in <a href="rock.md">stone</a>, CPUs vary in many ways. We may also dumb down some concepts a bit, real world CPUs are remarkably overengineered and complicated as hell.</p>

<p>Firstly then the most pressing question: <strong>what is it that a CPU really does?</strong> In essence it just reads instructions from the memory (depending on specific computer architecture this may be <a href="ram.md">RAM</a> or <a href="rom.md">ROM</a>) and does whatever they dictate – these instructions are super simple, often commands like “add two numbers”, “write a number to memory” and so on. The instructions themselves are nothing more than <a href="binary.md">binary</a> data in memory and their format depends on each CPU, or more precisely its <strong><a href="isa.md">instruction set</a></strong> (basically a very low level language it understands) – each CPU, or rather a CPU family, may generally have a different instruction set, so a program in one instruction set can’t be executed by a CPU that doesn’t understand this instruction set. The whole binary program for the CPU is called <strong><a href="machine_code.md">machine code</a></strong> and machine code corresponds to <strong><a href="assembly.md">assembly</a> language</strong> (basically a textual representation of the machine code, for better readability by humans) of the CPU (or better said its instruction set). So a CPU can be seen as a hardware <a href="interpreter.md">interpreter</a> of specific machine code, machine code depends on the instruction set and programmer can create machine code by writing a program in assembly language (which is different for each instruction set) and then using an assembler to translate the program to machine code. Nowadays mostly two instruction sets are used: <a href="x86.md">x86</a> and <a href="arm.md">Arm</a>, but there are also other ones, AND it’s still not so simple because each instruction set gets some kind of updates and/or has some extensions that may or may not be supported by a specific CPU, so it’s a bit messy. For example <a href="ia_32.md">IA-32</a> and <a href="x86_64.md">x86_64</a> are two different versions of the x86 ISA, one 32 bit and one 64 bit.</p>

<p>The CPU has an internal state (we can picture it as a <a href="finite_state_machine.md">state machine</a>), i.e. it has a few internal variables, called <strong><a href="register.md">registers</a></strong>; these are NOT variables in RAM but rather in the CPU itself, there is only a few of them (let’s say 32 for example) but they are stunningly fast, much faster than any other memory. What exactly these registers are, what they are called, how many <a href="bit.md">bits</a> they can hold and what their purpose is depends again on the instruction set architecture. However there are usually a few special registers, notably the <strong>program counter</strong> which holds the address of the currently executed instruction. After executing an instruction program counter is incremented so that in the nest step the next instruction will be executed, AND we can also modify program counter (sometimes directly, sometimes by specialized instructions) to jump between instruction to implement branching, loops, function calls etc.</p>

<p>So at the beginning (when powered on) the CPU is set to some initial state, most notably it sets its program counter to some initial value (depending on each CPU, it may be e.g. 0) so that it points to the first instruction of the program. Then it performs so called <strong>fetch, decode, execute</strong> cycle, i.e. it reads the instruction, decodes what it means and does what it says. In simpler CPUs this functionality is hard wired, however more complex CPUs (especially <a href="cisc.md">CISC</a>) are programmed in so called <a href="microcode.md">microcode</a>, a code yet at the lower level than machine code, machine code execution is programmed in microcode – microcode is something like “<a href="firmware.md">firmware</a> for the CPU” (or a “CPU <a href="shader.md">shader</a>”?), it basically allows later updates and reprogramming of how the CPU internally works. However this is pretty <a href="bloat.md">overcomplicated</a> and you shouldn’t make crappy CPUs like this.</p>

<p>A CPU works in <strong>clock cycles</strong>, i.e. it is a sequential circuit which has so called <em>clock</em> input; on this input voltage periodically switches between high and low (1 and 0) and each change makes the CPU perform another operation cycle. How fast the clock changes is determined by the clock <strong>frequency</strong> (nowadays usually around 3 GHz) – the faster the frequency, the faster the CPU will compute, but the more it will also heat up (so we can’t just set it up arbitrarily high, but we can <a href="overclocking.md">overclock</a> it a bit if we are cooling it down). WATCH OUT: <strong>one clock cycle doesn’t necessarily equal one executed instruction</strong>, i.e. frequency of 1 Hz doesn’t have to mean the CPU will execute 1 instruction per second because executing an instruction may take several cycles (how many depends on each instruction and also other factors). The number saying how many cycles an instruction takes is called CPI (cycles per instruction) – CPUs try to aim for CPI 1, i.e. they try to execute 1 instruction per cycle, but they can’t always do it.</p>

<p>One way to approach CPI 1 is by optimizing the <em>fetch, decode, execute</em> cycle in hardware so that it’s as BLAZINGLY fast as possible. This is typically done by utilizing an instruction <strong><a href="pipeline.md">pipeline</a></strong> – a pipeline has several stages working in parallel so that as soon as one instruction is entering e.g. the <em>decode</em> stage, another one is already coming to the <em>fetch</em> stage (and the previous instruction is in <em>execute</em> stage), i.e. we don’t have to wait for an instruction to be fully processed before starting to process the next one. This is practically the same principle as that of manufacturing lines in factories; if you have a long car manufacturing pipeline, you can make a factory produce let’s say one car each hour, though it is impossible to make a single car from scratch in one hour (or imagine e.g. a university producing new PhDs each year despite no one being able to actually earn PhD in a year). This is also why branching (jumps between instructions) are considered bad for program performance – a jump to different instruction makes the CPU have to throw away its currently preprocessed instruction because that will not be executed (though CPUs again try to deal with this with so called <em>branch prediction</em>, but it can’t work 100%). Some CPUs even have multiple pipelines, allowing for execution of multiple instructions at the same time – however this can only be done sometimes (the latter instruction must be independent of the former, also the other pipelines may be simpler and able to only handle simple instructions).</p>

<p>In order for a CPU to be useful it has to be able to perform some <strong><a href="io.md">input/output</a></strong>, i.e. it has to be able to retrieve data from the outside and present what it has computed. Notable ways of performing I/O are:</p>

<ul>
  <li>Through <strong>memory</strong>: here some parts of memory serve to pass data to the CPU and to retrieve computed results back. For example a keyboard may be mapped to memory so that when certain keys are pressed, the memory bits are set to 1 – this way a CPU can simply read from memory and know if a key is pressed. Similarly a display may be mapped to memory so that when a CPU writes a value to this address, a pixel appears on the display. Note that his doesn’t always have to PHYSICALLY pass through memory, there may be a special circuit that translate e.g. memory access in some address range to signals to hardware etc., but the CPU is using the same instructions it would use for interacting with memory.</li>
  <li>Through <strong><a href="gpio.md">GPIO</a> pins</strong>: CPUs typically have pins that are reserved for general purpose input/output, i.e. we can electronically communicate through them with whatever device we physically connect to those pins. A CPU can set and read voltage to/from those pins e.g. with some special instructions. This may be convenient if we just want to e.g. light up some LED without having to somehow hook it to the main memory.</li>
  <li><strong><a href="interrupt.md">Interrupts</a></strong>: a CPU can be informed about an external event with an interrupt (see further on).</li>
</ul>

<p>CPUs often also have a <strong><a href="cache.md">cache</a></strong> memory that speeds up communication with the main memory (RAM, ROM, …), though simpler CPUs may live even without cache of course. Mainstream CPUs even have several levels of cache, called L1, L2 etc. Caches are basically transparent for the programmer, they don’t have to deal with them, it’s just something that makes memory access faster, however a programmer knowing how a cache works can write code so as to be friendlier to the cache and utilize it better.</p>

<p>Mainstream consoomer CPUs nowadays have multiple <strong><a href="core.md">cores</a></strong> so that each core can essentially run a separate computation in parallel. The separate cores can be seen kind of like duplicate copies of the single core CPU with some connections between them (details again depend on each model), for example cores may share the cache memory, they will be able to communicate with each other etc. Of course this doesn’t just magically make the whole CPU faster, it can now only run multiple computations at once, but someone has to make programs so as to make use of this – typical use cases are e.g. <a href="multitasking.md">multitasking</a> operating systems which can run different programs (or rather processes) on each core (note that multitasking can be done even with a single core by rapidly switching between the processes, but that’s slower), or <a href="multithreading.md">multithreading</a> programming languages which may run each thread on a separate core.</p>

<p><strong><a href="interrupt.md">Interrupts</a></strong> are an important concept for the CPU and for low level programming, they play a role e.g. in saving power – high level programmers often don’t know what interrupts are, to those interrupts can be likened to “event <a href="callback.md">callbacks</a>”. An interrupt occurs on some sort of event, for example upon a key press, when timer ticks, when error occurred etc. (An interrupt can also be raised by the CPU itself, this is how operating system <a href="syscall.md">syscalls</a> are often implemented). What kinds of interrupts there are depends on each CPU architecture (consult your datasheet) and one can usually configure which interrupts to enable and which “callbacks” to use for them – this is often done through so called <strong><a href="vector.md">vector</a> table</strong>, a special area in memory that records addresses (“vectors”) of routines (functions/subprograms) to be called on specified interrupts. When interrupt happens, the current program execution is paused and the CPU automatically jumps to the subroutine for handling the interrupt – after returning from the subroutine the main program execution continues. Interrupts are contrasted with <strong><a href="polling.md">polling</a></strong>, i.e. manually checking some state and handling things as part of the main program, e.g. executing an infinite loop in which we repeatedly check keyboard state until some key is pressed. However polling is inefficient, it wastes power by constantly performing computation just by waiting – interrupts on the other hand are a hard wired functionality that just performs a task when it happens without any overhead of polling. Furthermore interrupts can make programming easier (you save many condition checks and memory reads) and mainly <strong>interrupts allow CPU to go into sleep mode</strong> and so save a lot of power. When a CPU doesn’t have any computation to do, it can stop itself and go into waiting state, not executing any instructions – however interrupts still work and when something happens, the CPU jumps back in to work. This is typically what the <code class="language-plaintext highlighter-rouge">sleep</code>/<code class="language-plaintext highlighter-rouge">wait</code> function in your programming language does – it puts the CPU to sleep and sets a timer interrupt to wake up after given amount of time. As a programmer you should know that you should call this sleep/wait function in your main program loop to relieve the CPU – if you don’t, you will notice the <strong>CPU utilization</strong> (amount of time it is performing computations) will go to 100%, it will heat up, your computer starts spinning the fans and be noisy because you don’t let it rest.</p>

<p>Frequently there are several <strong>modes</strong> of operation in a CPU which is typically meant for <a href="os.md">operating systems</a> – there will usually be some kind of privileged mode in which the CPU can do whatever it wants (this is the mode for the OS kernel) and a restricted mode in which there are “restrictions”, e.g. on which areas of memory can be accessed or which instructions can be used (this will be used for user program). Thanks to this a user program won’t be able to crash the operating system, it will at worst crash itself. Most notably x86 CPUs have the <em>real mode</em> (addresses correspond to real, physical addresses) and <em>protected mode</em> (memory is <a href="virtual_memory.md">virtualized</a>, protected, addresses don’t generally correspond to physical addresses).</p>

<p>A CPU may also have integrated some <strong><a href="coprocessor.md">coprocessors</a></strong>, though sometimes coprocessors are really a separate chip. Coprocessors that may be inside the CPU include e.g. the FPU (<a href="float.md">floating point</a> unit) or encryption coprocessor. Again, this will make the CPU a lot more expensive.</p>

<p>TODOOOOOOO: ALU, virtual memory, IP cores, architectures (register, …), …</p>

<h2 id="notable-cpus">Notable CPUs</h2>

<p>UNDER CONSTRUCTION</p>

<p>Here are listed some notable CPUs (or sometimes CPU families or cores).</p>

<p>{ I’m not so great with HW, suggest me improvements for this section please, thanks &lt;3 ~drummyfish }</p>

<p>{ WTF, allthetropes has quite a big list of famous CPUs, isn’t it a site about movies? https://allthetropes.org/wiki/Central_Processing_Unit. ~drummyfish }</p>

<p>TODO: add more, mark CPUs with ME, add features like MMX, FPU, …</p>

<table>
  <thead>
    <tr>
      <th>CPU</th>
      <th>year</th>
      <th>bits (/a)</th>
      <th>ISA</th>
      <th>~tr. c.</th>
      <th>tr. size</th>
      <th>freq.</th>
      <th>pins</th>
      <th>cores</th>
      <th>other</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Intel 4004</td>
      <td>1971</td>
      <td>4 / 12</td>
      <td>own</td>
      <td>2.3 K</td>
      <td>10 um</td>
      <td>75O K</td>
      <td>16</td>
      <td>1</td>
      <td> </td>
    </tr>
    <tr>
      <td>Intel 8008</td>
      <td>1972</td>
      <td>8 / 14</td>
      <td>own</td>
      <td>3.5 K</td>
      <td>10 um</td>
      <td>800 K</td>
      <td>18</td>
      <td>1</td>
      <td> </td>
    </tr>
    <tr>
      <td>Intel 8080</td>
      <td>1974</td>
      <td>8 / 16</td>
      <td>own</td>
      <td>6 K</td>
      <td>6 um</td>
      <td>3 M</td>
      <td>40</td>
      <td>1</td>
      <td> </td>
    </tr>
    <tr>
      <td>AMD Am9080</td>
      <td>1975</td>
      <td>8 / 16</td>
      <td>own</td>
      <td>6 K</td>
      <td>6 um</td>
      <td>4 M</td>
      <td>40</td>
      <td>1</td>
      <td> </td>
    </tr>
    <tr>
      <td>MOS Technology 6502</td>
      <td>1975</td>
      <td>8 / 16</td>
      <td>own</td>
      <td>3.5 K</td>
      <td>8 um</td>
      <td>3 M</td>
      <td>40</td>
      <td>1</td>
      <td> </td>
    </tr>
    <tr>
      <td>Zilog Z80</td>
      <td>1976</td>
      <td>8 / 16</td>
      <td>own</td>
      <td>8.5 K</td>
      <td>4 um</td>
      <td>10 M</td>
      <td>40</td>
      <td>1</td>
      <td> </td>
    </tr>
    <tr>
      <td>Intel 8086</td>
      <td>1978</td>
      <td>16 / 20</td>
      <td>x86 (x86-16)</td>
      <td>29 K</td>
      <td>3 um</td>
      <td>10 M</td>
      <td>40</td>
      <td>1</td>
      <td> </td>
    </tr>
    <tr>
      <td>Motorola 68000</td>
      <td>1979</td>
      <td>32 / 24</td>
      <td>own (CISC)</td>
      <td>68 K</td>
      <td> </td>
      <td> </td>
      <td>64</td>
      <td>1</td>
      <td> </td>
    </tr>
    <tr>
      <td>Intel (80)286</td>
      <td>1982</td>
      <td>16 / 24</td>
      <td>x86 (x86-16)</td>
      <td>130 K</td>
      <td>1.5 um</td>
      <td>25 M</td>
      <td>68</td>
      <td>1</td>
      <td> </td>
    </tr>
    <tr>
      <td>Intel (80)386</td>
      <td>1985</td>
      <td>32</td>
      <td>x86 (IA-32)</td>
      <td>275 K</td>
      <td>1 um</td>
      <td>40 M</td>
      <td>132</td>
      <td>1</td>
      <td> </td>
    </tr>
    <tr>
      <td>Intel (80)486</td>
      <td>1989</td>
      <td>32</td>
      <td>x86 (IA-32)</td>
      <td>1.6 M</td>
      <td>600 nm</td>
      <td>100 M</td>
      <td>196</td>
      <td>1</td>
      <td>16 K cache, FPU</td>
    </tr>
    <tr>
      <td>AMD Am386</td>
      <td>1991</td>
      <td>32</td>
      <td>x86 (IA-32)</td>
      <td>275 K</td>
      <td>800 nm</td>
      <td>40 M</td>
      <td>132</td>
      <td>1</td>
      <td> </td>
    </tr>
    <tr>
      <td>Intel Pentium P5</td>
      <td>1993</td>
      <td>32</td>
      <td>x86 (IA-32)</td>
      <td>3 M</td>
      <td>800 nm</td>
      <td>60 M</td>
      <td>273</td>
      <td>1</td>
      <td>16 K cache</td>
    </tr>
    <tr>
      <td>AMD K5</td>
      <td>1996</td>
      <td>32</td>
      <td>x86 (IA-32)</td>
      <td>4.3 M</td>
      <td>500 nm</td>
      <td>133 M</td>
      <td>296</td>
      <td>1</td>
      <td>24 K cache</td>
    </tr>
    <tr>
      <td>Intel Pentium II</td>
      <td>1997</td>
      <td>32</td>
      <td>x86 (IA-32)</td>
      <td>7 M</td>
      <td>180 nm</td>
      <td>450 M</td>
      <td>240</td>
      <td>1</td>
      <td>512 K L2 cache, MMX</td>
    </tr>
    <tr>
      <td>ARM7TDMI</td>
      <td>1994</td>
      <td>32</td>
      <td>ARM</td>
      <td> </td>
      <td> </td>
      <td>100 M</td>
      <td> </td>
      <td>1</td>
      <td> </td>
    </tr>
    <tr>
      <td>AMD Athlon 1000 Thunderbird</td>
      <td>2000</td>
      <td>32</td>
      <td>x86 (IA-32)</td>
      <td>37 M</td>
      <td>180 nm</td>
      <td>1 G</td>
      <td>453</td>
      <td>1</td>
      <td>~300 K cache</td>
    </tr>
    <tr>
      <td>RAD750</td>
      <td>2001</td>
      <td>32</td>
      <td>PowerPC</td>
      <td>10 M</td>
      <td>150 nm</td>
      <td>200 M</td>
      <td>360</td>
      <td>1</td>
      <td>64 K cache</td>
    </tr>
    <tr>
      <td>AMD Opteron</td>
      <td>2003</td>
      <td>64</td>
      <td>x86 (x86-64)</td>
      <td>105 M</td>
      <td>130 nm</td>
      <td>1.6 G</td>
      <td>940</td>
      <td>1</td>
      <td>~1 M cache</td>
    </tr>
    <tr>
      <td>Intel Pentium D 820</td>
      <td>2005</td>
      <td>64</td>
      <td>x86 (x86-64)</td>
      <td>230 M</td>
      <td>90 nm</td>
      <td>2.8 G</td>
      <td>775</td>
      <td>2</td>
      <td>~2 M cache</td>
    </tr>
    <tr>
      <td>Intel Core i5-2500K</td>
      <td>2011</td>
      <td>64</td>
      <td>x86 (x86-64)</td>
      <td>1 B</td>
      <td>32 nm</td>
      <td>3.3 G</td>
      <td> </td>
      <td>4</td>
      <td>~6 M cache, ME</td>
    </tr>
    <tr>
      <td>PicoRV32</td>
      <td>2015?</td>
      <td>32</td>
      <td>RISC-V (RV32IMC)</td>
      <td> </td>
      <td> </td>
      <td>~700 M</td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td>Apple A9</td>
      <td>2015</td>
      <td>64</td>
      <td>ARM (ARMv8)</td>
      <td>2 B</td>
      <td>14 nm</td>
      <td>1.8 G</td>
      <td> </td>
      <td>2</td>
      <td>~7 M cache</td>
    </tr>
    <tr>
      <td>AMD Ryzen Threadrip. PRO 5995WX</td>
      <td>2022</td>
      <td>64</td>
      <td>x86 (x86-64)</td>
      <td>33 B</td>
      <td>7 nm</td>
      <td>4.5 G</td>
      <td>4094</td>
      <td>64</td>
      <td>~300 M cache, PSP</td>
    </tr>
    <tr>
      <td><a href="talos_es.md">Talos ES</a></td>
      <td>2023</td>
      <td>8</td>
      <td>own (RISC)</td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
  </tbody>
</table>]]></content><author><name></name></author><summary type="html"><![CDATA[CPU]]></summary></entry><entry><title type="html">Computational_complexity</title><link href="https://promathx.github.io/computational_complexity/" rel="alternate" type="text/html" title="Computational_complexity" /><published>2025-06-30T00:00:00+00:00</published><updated>2025-06-30T00:00:00+00:00</updated><id>https://promathx.github.io/computational_complexity</id><content type="html" xml:base="https://promathx.github.io/computational_complexity/"><![CDATA[<h1 id="computational-complexity">Computational Complexity</h1>

<p>Computational complexity is a formal (<a href="math.md">mathematical</a>) study of resource usage (usually time and memory) by <a href="computer.md">computers</a> as they’re solving various types of problems. For example when using computers to <a href="sorting.md">sort</a> arrays of <a href="number.md">numbers</a>, computational complexity allows us to tell which <a href="algorithm.md">algorithm</a> will be fastest as the size of the array grows, which one will demand the least amount of memory (<a href="ram.md">RAM</a>) and even what’s generally the fastest way in which this can be done. While time (“speed”, number of steps) and memory (also <em>space</em>) are generally our primary resources of interest, other can be considered too, e.g. network or power usage. Complexity theory is invaluable and extremely important, it belongs among the most essential disciplines of <a href="compsci.md">computer science</a>; it is also immensely practically important as it helps us <a href="optimization.md">optimize</a> our programs, it teaches us useful knowledge such as that we can trade time and space complexity (i.e. make program run faster on detriment of memory and vice versa) etc.</p>

<p>Primarily we have to distinguish between two basic types of complexity:</p>

<ul>
  <li><strong><a href="algorithm.md">algorithm</a> complexity</strong>: Complexity of a specific algorithm. For example <a href="quick_sort.md">quick sort</a> and <a href="bubble_sort.md">bubble sort</a> are both algorithms for sorting arrays but quick sort has better time complexity. (Sometimes we may extend this meaning and talk e.g. about memory complexity of a <a href="data_structure.md">data structure</a> etc.)</li>
  <li><strong>problem complexity</strong>: Complexity of the best algorithm that can solve particular problem; e.g. time complexity of sorting an array is given by time complexity of the algorithm that can sort arrays the fastest.</li>
</ul>

<h2 id="algorithm-complexity">Algorithm Complexity</h2>

<p>Let us now focus on algorithm complexity, as problem complexity follows from it. OK, so <strong>what really is the “algorithm complexity”?</strong> Given resource <em>R</em> – let’s consider e.g. time, or the number of steps the algorithm needs to finish solving a problem – let’s say that complexity of a specific algorithm is a <a href="function.md">function</a> <em>f(N)</em> where <em>N</em> is the size of input data (for example length of the array to be sorted), which returns the amount of the resource (here number of steps of the algorithm). However we may spot issues emerging here, most importantly that the number of steps may not only depend on the size of input data but also the data itself (e.g. with sorting it may take shorter time to sort and already sorted array) and on the computer we use (for example some computers may be unable to perform multiplication natively and will emulate it with SEVERAL additions, increasing the number of steps), and also the exact complexity function will be pretty messy (it likely won’t be a nice smooth function but rather something that jumps around a bit). So this kind of <a href="suck.md">sucks</a>. We have to make several steps to get a nice, usable theory.</p>

<p>The <strong>solution</strong> to presented problem will be achieved in several steps.</p>

<p>FIRSTLY let’s clarify with better precision what <em>f(N)</em> returns exactly – when computing algorithm complexity we will always be interested in one of the following:</p>

<ul>
  <li><strong>best case</strong> scenario: Here we assume <em>f(N)</em> always returns the best possible value for given <em>N</em>, usually the lowest (i.e. least number of steps, least amount of memory etc.). So e.g. with array sorting for each array length we will assume the input array has such values that the given algorithm will achieve its best result (fastest sorting, best memory usage, …). I.e. this is the <strong>lower bound</strong> for all possible values the function could give for given <em>N</em>.</li>
  <li><strong>average case</strong> scenario: Here <em>f(N)</em> returns the average, i.e. taking all possible inputs for given input size <em>N</em>, we just average the performance of our algorithm and this is what the function tells us.</li>
  <li><strong>worst case</strong> scenario: Here <em>f(N)</em> return the worst possible values for given <em>N</em>, i.e. the opposite of best case scenario. This is the <strong>upper bound</strong> for all possible value the function could give for given <em>N</em>.</li>
</ul>

<p>This just deals with the fact that some algorithms may perform vastly different for different data – imagine e.g. linear searching of a specific value in a list; if the searched value is always at the beginning, the algorithm always performs just one step, no matter how long the list is, on the other hand if the searched value is at the end, the number of steps will increase with the list size. So when analyzing an algorithm <strong>we always specify which kind of case we are analyzing</strong> (WATCH OUT: do not confuse these cases with differences between big O, big Omega and big Theta defined below). So let’s say from now on we’ll be implicitly examining worst case scenarios.</p>

<p>SECONDLY rather than being interested in PRECISE complexity functions we will rather focus on so called <strong>asymptotic complexity</strong> – this kind of complexity is only concerned with how fast the resource usage generally GROWS as the size of input data approaches big values (infinity). So again, taking the example of array sorting, we don’t really desire to know exactly how many steps we will need to sort any given array, but rather how the time needed to sort bigger and bigger arrays will grow. This is also aligned with practice in another way: we don’t really care how fast our program will be for small amount of data, it doesn’t matter if it takes 1 or 2 microseconds to sort a small array, but we want to know how our program will <a href="scalability.md">scale</a> – if we have 10 TB of data, will it take 10 minutes or half a year to sort? If this data doubles in size, will the sorting time also double or will it increase 1000 times? This kind of complexity also no longer depends on what machine we use, the rate of growth will be the same on fast and slow machine alike, so we can conveniently just consider some standardized computer such as <a href="turing_machine.md">Turing machine</a> to mathematically study complexity of algorithms.</p>

<p>Rather than exact value of resource usage (such as exact number of steps or exact number of bytes in RAM) asymptotic complexity tells us a <strong><a href="class.md">class</a></strong> into which our complexity falls. These classes are given by mathematical functions that grow as fast as our complexity function. So basically we get kind of “tiers”, like <em>constant</em>, <em>linear</em>, <em>logarithmic</em>, <em>quadratic</em> etc., and our complexity simply falls under one of them. Some common complexity classes, from “best” to “worst”, are following (note this isn’t an exhaustive list):</p>

<ul>
  <li><strong>constant</strong>: Given by function <em>f(x) = 1</em> (i.e. complexity doesn’t depend on input data size). Best.</li>
  <li><strong><a href="log.md">logarithmic</a></strong>: Given by function <em>f(x) = log(x)</em>. Note the base of logarithm doesn’t matter.</li>
  <li><strong>linear</strong>: Given by function <em>f(x) = x</em>.</li>
  <li><strong>linearithmic</strong>: Given by function <em>f(x) = x * log(x)</em>.</li>
  <li><strong>quadratic</strong>: Given by function <em>f(x) = x^2</em>.</li>
  <li><strong>cubic</strong>: Given by function <em>f(x) = x^3</em>.</li>
  <li><strong>exponential</strong>: Given by function <em>f(x) = n^x</em>. This is considered very bad, practically unusable for larger amounts of data.</li>
</ul>

<p>Now we just put all the above together, introduce some formalization and notation that computer scientists use to express algorithm complexity, you will see it anywhere where this is discussed. There are the following:</p>

<ul>
  <li><strong>big O (Omicron) notation</strong>, written as <em>O(f(N))</em>: Says the algorithm complexity (for whatever we are measuring, i.e. time, space etc. and also the specific kind of case, i.e. worst/best/average) is asymptotically bounded from ABOVE by function <em>f(N)</em>, i.e. says the <strong>upper bound</strong> of complexity. This is probably the most common information regarding complexity you will encounter (we usually want this “pessimistic” view). More mathematically: complexity <em>f(x)</em> belongs to class <em>O(g(y))</em> if from some <em>N0</em> (we ignore some initial oscillations before this value) the function <em>f</em> always stays under function <em>g</em> multiplied by some positive constant <em>C</em>. Formally: <em>f(x) belongs to O(g(y)) =&gt; exists C &gt; 0 and N0 &gt; 0: for all n &gt;= N0: 0 &lt;= f(n) &lt;= C * g(n)</em>.</li>
  <li><strong>big Omega notation</strong>, written as <em>Omega(f(N))</em>: Says the algorithm complexity <strong>lower bound</strong> is given by function <em>f(N)</em>. Formally: <em>f(x) belongs to Omega(g(y)) =&gt; exists C &gt; 0 and N0 &gt; 0: for all n &gt;= N0: 0 &lt;= C * g(n) &lt;= f(n)</em>.</li>
  <li><strong>big Theta notation</strong>, written as <em>Theta(f(N))</em>: This just means the complexity is both <em>O(f(N))</em> and <em>Omega(f(N))</em>, i.e. the complexity is tightly bounded by given function.</li>
</ul>

<p>Please note that big O/Omega/Theta are a different thing than analyzing best/worst/average case! We can compute big O, big Omega and big Theta for all best, worst and average case, getting 9 different “complexities”.</p>

<p>Now notice (also check by the formal definitions) that we simply don’t care about additive and multiplicative constants and we also don’t care about some initial oscillations of the complexity function – it doesn’t matter if the complexity function is <em>f(x) = x</em> or <em>f(x) = 100000000 + 100000000 * x</em>, it still falls under linear complexity! If we have algorithm <em>A</em> and <em>B</em> and <em>A</em> has better complexity, <em>A</em> doesn’t necessarily ALWAYS perform better, it’s just that as we scale our data size to very high values, <em>A</em> will prevail in the end.</p>

<p>Another thing we have to clear up: <strong>what does input size really mean?</strong> I.e. what exactly is the <em>N</em> in <em>f(N)</em>? We’ve said that e.g. with array sorting we saw <em>N</em> as the length of the array to be sorted, but there are several things to additionally talk about. Firstly it usually doesn’t matter if we measure the size of input in bits, bytes or number of items – note that as we’re now dealing with asymptotic complexity, i.e. only growth rate towards infinity, we’ll get the same complexity class no matter the units (e.g. a linear growth will always be linear, no matter if our <em>x</em> axis measures meters or centimeters or light years). SECONDLY however it sometimes DOES matter how we define the input size, take e.g. an algorithm that takes a square image with resolution <em>R * R</em> on the input, iterates over all pixels and find the brightest one; now we can define the input size either as the total number of pixels of the image (i.e. <em>N = R * R</em>) OR the length of the image side (i.e. <em>N = R</em>) – with the former definition we conclude the algorithm to have linear time complexity (for <em>N</em> input pixels the algorithm makes roughly <em>N</em> steps), with the latter definition we get QUADRATIC time complexity (for image with side length <em>N</em> the algorithm makes roughly <em>N * N</em> steps). What now, how to solve this? Well, this isn’t such an issue – we can define the input size however we want, we just have to <strong>stay consistent</strong> so that we are able to compare different algorithms (i.e. it holds that if algorithm <em>A</em> has better complexity than algorithm <em>B</em>, it will stay so under whatever definition of input size we set), AND when mentioning complexity of some algorithm we should mention how we define the input size so as to prevent confusion.</p>

<p>With memory complexity we face a similar issue – we may define memory consumption either as an EXTRA memory or TOTAL memory that includes the memory storing the input data. For example with array sorting if an algorithm works <a href="in_situ.md">in situ</a> (in place, needing no extra memory), considering the former we conclude memory complexity to be constant (extra memory needed doesn’t depend on size of input array), considering the latter we conclude the memory complexity to be linear (total memory needed grows linearly with the size of input array). The same thing as above holds: whatever definition we choose, we should just mention which one we chose and stay consistent in using it.</p>

<h2 id="problem-complexity">Problem Complexity</h2>

<h3 id="p-vs-np">[P vs NP]</h3>
<p>The class P stands for polynomial and is defined as all problems that can be solved by an algorithm run on a deterministic Turing machine (a theoretical computer) with a polynomial time complexity.</p>

<p>The class NP stands for non-deterministic polynomial and is defined as all problems that can be solved by an algorithm run on a non-deterministic Turing machine with a polynomial time complexity. I.e. the definition is the same as for the P class with the difference that the Turing machine is non-deterministic – such a machine is faster because it can make kind of “random correct guesses” that lead to the solution more quickly. Non-deterministic computers are only theoretical (at least for now), computers we have in real life cannot perform such randomly correct guesses. It is known that the solution to all NP problems can be verified in polynomial time even by a deterministic Turing machine, we just don’t know if the solution can also be found this quickly.</p>

<p>As said, problem complexity is tied to algorithm complexity; a complexity of specific problem (e.g. sorting an array, factorization of a number, searching a sorted list etc.) is determined by the best possible <a href="algorithm.md">algorithm</a> that solves the problem (best in terms of analyzed complexity). Traditionally we use <a href="turing_machine.md">Turing machines</a> and <a href="formal_language.md">formal languages</a> to analyze problem complexity. Here we’ll stay a bit informal and only mention some ideas.</p>

<p>Similarly to algorithm complexity, with problems we again define <strong><a href="class.md">classes</a></strong> that are only about “how quickly the resource usage grows as we increase input size”. The main difference is we are examining problems, so the classes we get are classes of PROBLEMS (usually classes of formal languages, like e.g. in Chomsky’s language hierarchy), not classes of functions (seen in algorithm complexity). Some common classes are:</p>

<ul>
  <li><strong>DTime(f(x))</strong>: Problems for whose solution a <a href="determinism.md">DETERMINISTIC</a> Turing machine has an algorithm with time complexity <em>O(f(x))</em>.</li>
  <li><strong>NTime(f(x))</strong>: Problems for whose solution a NON-DETERMINISTIC Turing machine has an algorithm with time complexity <em>O(f(x))</em>.</li>
  <li><strong>DSpace(f(x))</strong>: Same as DTime but for space complexity.</li>
  <li><strong>NSpace(f(x))</strong>: Same as NTime but for space complexity.</li>
  <li><strong><a href="p.md">P</a></strong>: Union of all classes <em>DTime(n^k)</em>, i.e. all problems that can be solved by a DETERMINISTIC Turing machine with <a href="polynomial.md">polynomial</a> time complexity.</li>
  <li><strong><a href="np.md">NP</a></strong>: Union of all classes <em>NTime(n^k)</em>, i.e. the same as above but for NON-DETERMINISTIC Turing machine. It is currently not know <a href="p_vs_np.md">if classes P and NP are the same</a>, though it’s believed to not be the case – in fact this is probably the most famous yet unsolved problem of computer science.</li>
  <li><strong>EXP</strong>: Union of all classes <em>DTime(2^n^k)</em>.</li>
  <li>…</li>
</ul>

<h2 id="examples">Examples</h2>

<p>Practically analyzing time complexity of algorithms mostly involves looking at the loops in our algorithm as these are what makes number of steps in algorithm variable. Linear sequences of instructions (such as initializations) don’t interest us, no matter how long, as these have no effect on asymptotic complexity. But remember that looping may also be achieved with <a href="recursion.md">recursion</a> etc., so just look carefully.</p>

<p>Let’s consider the simple <a href="bubble_sort.md">bubble sort</a> array sorting algorithm, with the simple optimization that ends as soon as the array is sorted; here it is written in <a href="c.md">C</a>:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>void bubbleSort(int *array)
{
  for (int i = 0; i &lt; N - 1; ++i)
  {
    int end = 1;

    for (int j = 0; j &lt; N - 1 - i; ++j)
      if (a[j] &gt; a[j + 1])
      {
        swap(&amp;a[j],&amp;a[j + 1]);
        end = 0;
      }

    if (end) // no swap happened =&gt; sorted, end
      break;
  }
}
</code></pre></div></div>

<p>The size of input data is the length of input array (<code class="language-plaintext highlighter-rouge">N</code>), for memory we consider only the extra memory used. Let’s see about different complexities now:</p>

<ul>
  <li><strong>asymptotic TIME complexity for WORST case</strong>: Worst case happens when the optimizing condition (<code class="language-plaintext highlighter-rouge">if (end)</code>) never triggers and so both loops (outer and inner one) in our algorithm run all their iterations; the outer loop is performed approximately <em>N</em> times (actually <em>N - 1</em> times but remember that asymptotic complexity ignores -1 here as an additive constant) and for each of its iterations the inner loop runs approximately <em>N - i</em> times. So e.g. for <em>N = 5</em> we get approximately 5 + 4 + 3 + 2 + 1 steps, so for given <em>N</em> we basically have to sum up numbers from 1 to <em>N</em> – there is a formula for computing such sum and that is <em>(N *(N + 1)) / 2 = N^2 / 2 + N / 2</em>. With asymptotic complexity we just take the biggest term and ignore any multiplication constant (division by two), so we just see <em>N^2</em> here and conclude that the time complexity of worst case for bubble sort is quadratic, i.e. <em>O(N^2)</em>. Notice that technically we can also say the complexity belongs to any “worse” class, e.g. <em>O(N^3)</em> (as <em>O(N^2)</em> is its subclass), which is technically true but doesn’t tell us as much. So here it’s better to further more precisely say the complexity of worst case also belongs to <em>Omega(N^2)</em> (the lower bound) and therefore (by belonging to both <em>O(N^2)</em> and <em>Omega(N^2)</em>) also belongs to <em>Theta(N^2)</em>, i.e. it “won’t be slower BUT NOR faster than <em>N^2</em>”.</li>
  <li><strong>asymptotic TIME complexity for BEST case</strong>: Best case happens when the input array is already sorted – here the algorithm enters the outer loop, then runs the inner loop – approximately <em>N</em> iterations – and then (since no swap happened) ends at the final condition. So the time complexity is linear – we can say that the upper asymptotic bound on best case scenario is <em>O(N)</em>. Again we see the complexity is linearly bound from the bottom too so it’s better to say the complexity of best case belongs to <em>Theta(N)</em>.</li>
  <li><strong>space (memory) complexity</strong>: Bubble sort works in place and though it uses extra variables, the size of those variables doesn’t depend on the size of input array, so (as we only count extra memory requirements) we can say memory complexity is constant, i.e. <em>O(1)</em>, and also <em>Theta(1)</em>.</li>
</ul>]]></content><author><name></name></author><summary type="html"><![CDATA[Computational Complexity]]></summary></entry><entry><title type="html">Prime</title><link href="https://promathx.github.io/prime/" rel="alternate" type="text/html" title="Prime" /><published>2025-05-26T00:00:00+00:00</published><updated>2025-05-26T00:00:00+00:00</updated><id>https://promathx.github.io/prime</id><content type="html" xml:base="https://promathx.github.io/prime/"><![CDATA[<h1 id="prime-number">Prime Number</h1>

<p>Prime number (or just <em>prime</em>) is a whole positive numbers only divisible by 1 and itself, except for the number 1. I.e. prime numbers are 2, 3, 5, 7, 11, 13, 17 etc. Non-prime numbers are called <em>composite numbers</em>. The simplicity of definition might bring about an impression that prime numbers are something plain, akin to odd numbers for example, but this couldn’t be further from the truth: you’ll learn from any mathematician that primes are not only vitally important to their field, but immensely interesting, complex and even mysterious for their intricate properties and relatively “random” distribution among other numbers, they have for millennia fascinated mathematics because they are so simple and complex at the same time, and they’re still not fully understood to this day. Nowadays they are studied by so called number theory, a subfield of mathematics Primes are also of practical use, for example in asymmetric cryptography Primes can be seen as the opposite of highly composate numbers (also antiprimes, numbers that have more divisors than any lower number). Numbers of comparable status and similarly mysterious properties to prime numbers are for example perfect numbers, whose importance is however a bit diminished by current lack of practical use.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>.##.#.#...#.#...#.#...#.....#.#.....#...#.#...#.....#.....#.#.....#...
</code></pre></div></div>

<p><em>Prime number positions up to 70.</em></p>

<p>The largest known prime number as of 2022 is 2^82589933 - 1 (it is so called Mersenne prime, i.e. a prime of form 2^N - 1).</p>

<p>Every natural number greater than 1 has a unique <strong>prime factorization</strong>, i.e. a <a href="set.md">set</a> of prime numbers whose product it is. For example 75 is a product of three primes: 3 * 5 * 5. This is called the <em>fundamental theorem of arithmetic</em>. Naturally, each prime has a factorization consisting of a single number – itself – while factorizations of non-primes consist of at least two primes. To mathematicians prime numbers are what chemical elements are to chemists – a kind of basic building blocks.</p>

<p><strong>Why is 1 not a prime?</strong> Out of convenience – if 1 was a prime, the fundamental theorem of arithmetic would not hold because 75’s factorization could be 3 * 5 * 5 but also 1 * 3 * 5 * 5, 1 * 1 * 3 * 5 * 5 etc. It also makes sense under some different definitions – imagine for example we create a <a href="tree.md">tree</a> of numbers, assign each number <em>N</em> a parent number <em>M</em> which is the maximum of all <em>N</em>’s divisors that we check from 1 (including) to <em>N</em> (excluding); in this tree prime numbers are all numbers in depth 1, i.e. those that are direct children of 1, but 1 itself is not at this level, it’s at the root, having no parent (as it would be its own parent), so by this definition 1 is also not a prime.</p>

<p>The unique factorization can also nicely be used to encode <a href="multiset.md">multisets</a> as numbers. We can assign each prime number its sequential number (2 is 0, 3 is 1, 5 is 2, 7 is 3 etc.), then any number encodes a set of numbers (i.e. just their presence, without specifying their order) in its factorization. E.g. 75 = 3 * 5 * 5 encodes a multiset {1, 2, 2}. This can be exploited in cool ways in some <a href="cypher.md">cyphers</a> etc.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>       _ _   _   _       _   _       _   _       _           _   _
     1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3
                       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
     ____________________________________________________________________
  1 |&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;
  2 |  &lt;&gt;||&lt;&gt;||&lt;&gt;||&lt;&gt;  &lt;&gt;||&lt;&gt;||&lt;&gt;  &lt;&gt;||&lt;&gt;||&lt;&gt;  &lt;&gt;||&lt;&gt;  &lt;&gt;  &lt;&gt;||&lt;&gt;||&lt;&gt;  &lt;&gt;
  3 |    &lt;&gt;  ||&lt;&gt;||  &lt;&gt;  ||&lt;&gt;||  &lt;&gt;  ||&lt;&gt;||  &lt;&gt;  ||&lt;&gt;    &lt;&gt;  ||&lt;&gt;||  &lt;&gt;
  4 |      &lt;&gt;||  ||&lt;&gt;    ||&lt;&gt;||    &lt;&gt;||  ||&lt;&gt;    ||&lt;&gt;      &lt;&gt;||  ||&lt;&gt;
  5 |        &lt;&gt;  ||    &lt;&gt;||  ||  &lt;&gt;  ||  ||&lt;&gt;    ||  &lt;&gt;      ||&lt;&gt;||
  6 |          &lt;&gt;||      ||&lt;&gt;||      ||&lt;&gt;||      ||          ||  ||
  7 |            &lt;&gt;      ||  ||&lt;&gt;    ||  ||  &lt;&gt;  ||          ||  ||
  8 |              &lt;&gt;    ||  ||    &lt;&gt;||  ||      ||&lt;&gt;        ||  ||
  9 |                &lt;&gt;  ||  ||      ||&lt;&gt;||      ||      &lt;&gt;  ||  ||
 10 |                  &lt;&gt;||  ||      ||  ||&lt;&gt;    ||          ||&lt;&gt;||
 11 |                    &lt;&gt;  ||      ||  ||    &lt;&gt;||          ||  ||  &lt;&gt;
 12 |                      &lt;&gt;||      ||  ||      ||&lt;&gt;        ||  ||
 13 |                        &lt;&gt;      ||  ||      ||    &lt;&gt;    ||  ||
 14 |                          &lt;&gt;    ||  ||      ||        &lt;&gt;||  ||
 15 |                            &lt;&gt;  ||  ||      ||          ||&lt;&gt;||
 16 |                              &lt;&gt;||  ||      ||          ||  ||&lt;&gt;
 17 |                                &lt;&gt;  ||      ||          ||  ||    &lt;&gt;
 18 |                                  &lt;&gt;||      ||          ||  ||
 19 |                                    &lt;&gt;      ||          ||  ||
 20 |                                      &lt;&gt;    ||          ||  ||
</code></pre></div></div>

<p><em>Divisibility of numbers on top by numbers on the left – we can see prime numbers as the ones avoided by all divisors (except 1 and self), i.e. the ones for which we can draw a continuous straight vertical line between the top line (divisibility by 1) and the diagonal (divisibility by self).</em></p>

<p>When in 1974 the Arecibo radio message was sent to space to carry a message for <a href="alien.md">aliens</a>, the resolution of the bitmap image it carried was chosen to be 73 x 23 pixels – two primes. This was cleverly done so that when aliens receive the 1679 sequential values, there are only two possible ways to interpret them as a 2D bitmap image: 23 x 73 (incorrect) and 73 x 23 (correct). This increased the probability of correct interpretation against the case of sending an arbitrary resolution image.</p>

<p><strong>There are infinitely many prime numbers</strong>. The proof is quite elementary (shown below), however it’s pretty fascinating that it has still not been proven whether there are infinitely many <strong><a href="twin_prime.md">twin primes</a> (primes that differ by 2)</strong>, which, despite its similarity to the original problem, shows to be an incomparably more difficult question to answer. The notion of “twin prime” can be extended to prime triplets (3 primes with gaps 2 and 4, in either order), prime cousins (primes spaced by 4), sexy primes (primes spaced by 6) and eventually generalized to so called prime <em>k-tuples</em>: tuples describing prime patterns with offsets, e.g. [0, 2, 6] specifies one possible form of a prime triplet etc. – you may even hear the term <em>prime constellation</em> (it’s almost as if prime numbers are like stars in the sky, together forming certain shapes). Another simple but unproven conjecture related to prime numbers is <a href="goldbachs_conjecture.md">Goldbach’s conjecture</a> stating that every even number greater than 2 can be written as a sum of two primes.</p>

<p>Euklid’s <a href="proof.md">proof</a> shows there are infinitely many primes, it is conducted by contradiction and goes as follows: suppose there are finitely many primes <em>p1</em>, <em>p2</em>, … <em>pn</em>. Now let’s consider a number <em>s</em> = <em>p1</em> * <em>p2</em> * … * <em>pn</em> + 1. This means <em>s</em> - 1 is divisible by each prime <em>p1</em>, <em>p2</em>, … <em>pn</em>, but <em>s</em> itself is not divisible by any of them (as it is just 1 greater than <em>s</em> and multiples of some number <em>q</em> greater than 1 have to be spaced by <em>q</em>, i.e. more than 1). If <em>s</em> isn’t divisible by any of the considered primes, it itself has to be a prime. However that is in contradiction with the original assumption that <em>p1</em>, <em>p2</em>, … <em>pn</em> are all existing primes. Therefore a finite list of primes cannot exist, there have to be infinitely many of them.</p>

<p><strong>Distribution and occurrence of primes</strong>: the occurrence of primes seems kind of “”“<a href="random.md">random</a>””” (a bit like digits of <a href="decimal.md">decimal</a> representation of <a href="pi.md">pi</a>), without a simple pattern. Obviously <em>randomness</em> here doesn’t stand for “true randomness”, as primality of any number is perfectly <a href="determinism.md">determined</a> and <a href="decidability.md">decidable</a>, but as far as our knowledge reaches, there aren’t simple ways to perfectly predict for example occurrence of primes with a simple, fast to compute formula, i.e. <em>randomness</em> rather stands for <a href="chaos.md">chaos</a>. Hints of patterns appear such as the <a href="ulam_spiral.md">Ulam spiral</a> – upon plotting natural numbers in a square spiral and marking the primes, we visually perceive a dimly appearing 45 degree diagonals as well as horizontal and vertical lines. Furthermore the <strong>density of primes decreases</strong> the further away we go from 0. The <em>prime number theorem</em> states that a number randomly chosen between 0 and <em>N</em> (for large <em>N</em>) has approximately 1/log(N) <a href="probability.md">probability</a> of being a prime. <strong>Prime counting function</strong> is a <a href="function.md">function</a> which for <em>N</em> tells the number of primes smaller or equal to <em>N</em>. While there are 25 primes under 100 (25%), there are 9592 under 100000 (~9.5%) and only 50847534 under 1000000000 (~5%).</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>         ,   ,     ,    ',  '    ,'  , ,  '
      ',',          '      ,  ' ',  ' ',
,,   ,   ,'     '     ',   ,'      ,'  ,      ' '
 ,     ,    ', ,  '  ,' ', ,'             '  ,', ,
      '     ',',     ,   ,          ',    ',
,    , ,               ,'  ,' '   ',   , ,      '
     ,',   ,',  '  , ,     ,' '  ,  '        ,
, ',  ' ',     ,   ,'      ,    ',  '     ' '    ,
  ',', , ,'   '  ,     ,'   ' '  ,',' ',     , ,
            '     '   ' ', ,     ,   ,  ', ,  ', ,
      '   '   '   ' '   ', ,   ,'   ' '         ',
  ' ',  ', , , ,', ,',',   ,' ' '  ,  ' ' '  , ,
     ,             ,   ,',;,', , ,   , ,',     ,
  ' '       '       ',' ',
,,' '  ,  '  ,' ',  ','   ',  ' ', ,'  ,' '   ',
     , ,         , ,  '  ,   ,   ,'      , ,  '
      ' ',    ',  '  ,   , ,  '       '     '
,,       , , , ,    ' '  ,',    ',  '  , ,'  ,','
 ,     ,   ,',  '  ,     ,  ',',           ,     ,
  ',    ' '       ', ,   , ,'       ',    ',
,   ',       , ,'   ',    '     '      ,  ' '
 ,    '   '       '           '   ',    ',   ,
   ,  '   ' '   '    ,'    ,   , ,'             ',
 ,'     ',     ,   ,',   ,  '    ,     ,    '   '
     ,            ',     ,   , ,     ,   ,'   ',
</code></pre></div></div>

<p><em>Ulam spiral: the center of the image is the number 1, the number line continues counter clockwise, each point represents a prime.</em></p>

<p>Here are prime numbers under 1000: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997.</p>

<p>Here are twin prime numbers under 1000: 3, 5, 7, 11, 13, 17, 19, 29, 31, 41, 43, 59, 61, 71, 73, 101, 103, 107, 109, 137, 139, 149, 151, 179, 181, 191, 193, 197, 199, 227, 229, 239, 241, 269, 271, 281, 283, 311, 313, 347, 349, 419, 421, 431, 433, 461, 463, 521, 523, 569, 571, 599, 601, 617, 619, 641, 643, 659, 661, 809, 811, 821, 823, 827, 829, 857, 859, 881, 883.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>                                      ______/
                                     /     /
                        _____ ______/_    /
                 ____  /     X     /__\  /
          ___   /    \/__   / \   /__  \/
         /   \ /     /\  \ /   \ /   \ /\
--2--3--/--5--X--7--/--\--X--11-X--13-X--\--
   \__\/ \  \/ \__\/ \  \/ \__\/ \  \/ \__\/
       \__\_/\  \ /\  \ /\__\_/\  \ /\  \ /\
           \__\__X  \  X  \  X  \  X__\__X
               \__\__\/ \  \/ \  \/ \  \/ \
                   \__\__\_/\  \ /\  \ /\  \
</code></pre></div></div>

<p>There also exists a term <strong>pseudoprime</strong> – it stands for a number which is not actually a prime but appears so because it passes some quick primality tests.</p>

<p><strong>Higher order primes</strong>, also <strong>superprimes</strong> or prime-indexed primes, are primes occupying prime numberth position within prime numbers, i.e. one of first higher order primes is for example number 5 because it is the 3rd prime and 3 itself is a prime. 5 is also one of second order higher primer numbers because it is 2nd first higher order prime number and 2 is a prime number. Etc. So we may generalize this concept to a prime number order <em>R(x)</em>, which says the highest order that number <em>x</em> achieves in this sense, with <em>R(x) = 0</em> meaning <em>x</em> is not prime at all. One of very high superprimes is for example number 174440041 (lowest number with <em>R(x) = 12</em>). Prime orders for numbers up to 1000 are (leaving out the ones with order 0):</p>

<p>2: 1, 3: 2, 5: 3, 7: 1, 11: 4, 13: 1, 17: 2, 19: 1, 23: 1, 29: 1, 31: 5, 37: 1, 41: 2, 43: 1, 47: 1, 53: 1, 59: 3, 61: 1, 67: 2, 71: 1, 73: 1, 79: 1, 83: 2, 89: 1, 97: 1, 101: 1, 103: 1, 107: 1, 109: 2, 113: 1, 127: 6, 131: 1, 137: 1, 139: 1, 149: 1, 151: 1, 157: 2, 163: 1, 167: 1, 173: 1, 179: 3, 181: 1, 191: 2, 193: 1, 197: 1, 199: 1, 211: 2, 223: 1, 227: 1, 229: 1, 233: 1, 239: 1, 241: 2, 251: 1, 257: 1, 263: 1, 269: 1, 271: 1, 277: 4, 281: 1, 283: 2, 293: 1, 307: 1, 311: 1, 313: 1, 317: 1, 331: 3, 337: 1, 347: 1, 349: 1, 353: 2, 359: 1, 367: 2, 373: 1, 379: 1, 383: 1, 389: 1, 397: 1, 401: 2, 409: 1, 419: 1, 421: 1, 431: 3,
433: 1, 439: 1, 443: 1, 449: 1, 457: 1, 461: 2, 463: 1, 467: 1, 479: 1, 487: 1, 491: 1, 499: 1, 503: 1, 509: 2, 521: 1, 523: 1, 541: 1, 547: 2, 557: 1, 563: 2, 569: 1, 571: 1, 577: 1, 587: 2, 593: 1, 599: 3, 601: 1, 607: 1, 613: 1, 617: 2, 619: 1, 631: 1, 641: 1, 643: 1, 647: 1, 653: 1, 659: 1, 661: 1, 673: 1, 677: 1, 683: 1, 691: 1, 701: 1, 709: 7, 719: 1, 727: 1, 733: 1, 739:
2, 743: 1, 751: 1, 757: 1, 761: 1, 769: 1, 773: 2, 787: 1, 797: 2, 809: 1, 811: 1, 821: 1, 823: 1, 827: 1, 829: 1, 839: 1, 853: 1, 857: 1, 859: 2, 863: 1, 877: 2, 881: 1, 883: 1, 887: 1, 907: 1, 911: 1, 919: 3, 929: 1, 937: 1, 941: 1, 947: 1, 953: 1, 967: 2, 971: 1, 977: 1, 983: 1, 991: 2, 997: 1.</p>

<p>Out of autistic curiosity we may turn this into a “race” for firsts, i.e. look for first prime of order <em>N</em> for each <em>N</em>. Here we go: 2 (order 1), 3 (2), 5 (3), 11 (4), 31 (5), 127 (6), 709 (7), 5381 (8), 52711 (9), 648391 (10), 9737333 (11), …</p>

<p><strong>Prime gaps</strong>: statistically gaps between consecutive primes increase. The size of the gaps themselves make another number sequence that starts like this 1, 2, 2, 4, 2, 4, 2, 4, 6, 2, 6, 4, 2, 4, 6, 6, 2, 6, 4, 2, 6, 4, 6, 8, 4, 2, 4, 2, 4, 14, 4, 6, 2, 10, 2, 6, 6, 4, 6, 6, 2, 10, 2, 4, 2, 12, 12, 4, 2, 4, 6, 2, 10, 6, 6, 6, 2, 6, 4, 2, 10, 14, 4, 2, 4, 14, 6, 10, 2, 4, 6, 8, 6, 6, 4, 6, 8, 4, 8, 10.</p>

<p><strong><a href="fun.md">Fun</a> with primes</strong>: thanks to their interesting, mysterious and <a href="randomness.md">random</a> nature, primes can be played around with – of course, you can examine them mathematically, which is always fun, but you can also play sort of <a href="game.md">games</a> with them. For example the prime race: you make two teams of primes, one that gives 1 modulo 4, the other one that gives 3; then you go prime by prime and add points to each team depending on which one the prime falls in; the interesting thing is that team 3 is almost always in lead just by a tiny amount (this is known as Chebyshev bias, only after 2946 primes team 1 gets in the lead for a while, then at 50378 etc.). Similar thing can be done by evaluating the Mobius function: set total sum to 0, then go number by number and if it only has unique prime factors, add 1 if the number of those factors is even, otherwise subtract 1 – see how the function behaves. Of course you can go crazy, make primes paint pictures or compose <a href="music.md">music</a> – people also like to do this with digits of numbers, e.g. those of <a href="pi.md">pi</a> or <a href="e.md">e</a>.</p>

<p><strong>Can we generalize/modify the concept of prime numbers?</strong> Yeah, sure, why not? The ways are many, we’ll rather run into the issue of analysis paralysis – choosing the interesting generalization of out of the many possible ways. Some possible generalizations include:</p>

<ul>
  <li><strong>pseudoprimes</strong>: the above mentioned, i.e. non-primes passing many prime tests.</li>
  <li><strong>almost primes</strong>: a number is <em>n</em>-almost prime if it has <em>n</em> prime factors, so 1-almost primes are just regular primes (they have 1 prime divisor – themselves) but then there are 2 almost primes like 9 or 15 that are kind of closer to being primes than let’s say 5-almost-primes such as 48 or 80. We take the idea of numbers having either none (primes) or some (non-primes) divisors and generalized it by says a number is more prime like if it has fewer divisors.</li>
  <li>Another idea hinted on above: make a <a href="tree.md">tree</a> of numbers with 1 as its root, assign each number a parent that’s its greatest divisor (excluding the number itself); in this tree 1 is above prime numbers, prime numbers are on level 1, second level may be seen as the “next best thing” to primes (4, 6, 9, 10, 15, …), third level the next (8, 12, 18, 27) and so on, i.e. we define the “primeness” as the depth in this tree, the number of times we have to replace the number with its greatest divisor before we get to 1.</li>
  <li><strong><a href="complex_number.md">complex</a> (Gaussian) primes</strong>: This is not a strict generalization because we remove some primes by were primes before, but we may define prime numbers also within complex integers. Here we get primes to be 3, 7, 11, 19, 23 etc.</li>
  <li>Similarly we may try to play on this observation: a non-prime is a number that is divisible by something, i.e. there is some number that when dividing the original number gives remainder after division zero; primes are those for which no number gives remainder zero, but some primes might be considered “weaker” by giving very low or very high remainder such as 1, i.e. being “not quite but almost” divisible by something (of course we have to somehow account for the fact that low divisors can only ever give low remainders) – ideal prime would have remainders after division near the half of the dividing number (it would dodge multiples of other numbers with some margin), which we can formalize and define kind of “prime strength”.</li>
  <li>TODO: generalization to non integers? haven’t found anything</li>
  <li>…</li>
</ul>

<h2 id="algorithms">Algorithms</h2>

<p><strong>Primality test</strong>: testing whether a number is a prime is quite easy and not computationally difficult (unlike factoring the number). A <a href="naive.md">naive</a> algorithm is called <em>trial division</em> and it tests whether any number from 2 up to the tested number divides the tested number (if so, then the number is not a prime, otherwise it is). This can be <a href="optimization.md">optimized</a> by only testing numbers up to the <a href="sqrt.md">square root</a> (including) of the tested number (if there is a factor greater than the square root, there is also another smaller than it which would already have been tested). A further simple optimization is to to test division by 2, 3 and then only numbers of the form 6q +- 1 (other forms are divisible by either 2 or 3, e.g 6q + 4 is always divisible by 2). Further optimizations exist and for maximum speed a <a href="lut.md">look up table</a> may be used for smaller primes. A simple <a href="c.md">C</a> function for primality test may look e.g. like this:</p>

<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">int</span> <span class="nf">isPrime</span><span class="p">(</span><span class="kt">int</span> <span class="n">n</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">if</span> <span class="p">(</span><span class="n">n</span> <span class="o">&lt;</span> <span class="mi">4</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">n</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">;</span>

  <span class="k">if</span> <span class="p">(</span><span class="n">n</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span> <span class="o">||</span> <span class="n">n</span> <span class="o">%</span> <span class="mi">3</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>

  <span class="kt">int</span> <span class="n">test</span> <span class="o">=</span> <span class="mi">6</span><span class="p">;</span>

  <span class="k">while</span> <span class="p">(</span><span class="n">test</span> <span class="o">&lt;=</span> <span class="n">n</span> <span class="o">/</span> <span class="mi">2</span><span class="p">){</span> <span class="c1">// replace n / 2 by sqrt(n) if available {</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">n</span> <span class="o">%</span> <span class="p">(</span><span class="n">test</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span> <span class="o">||</span> <span class="n">n</span> <span class="o">%</span> <span class="p">(</span><span class="n">test</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">){</span>
      <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="n">test</span> <span class="o">+=</span> <span class="mi">6</span><span class="p">;</span>

    <span class="p">}</span>

  <span class="p">}</span>

  <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
<span class="err">}</span>
</code></pre></div></div>

<p><a href="sieve_of_eratosthenes.md">Sieve of Eratosthenes</a> is a simple algorithm to find prime numbers up to a certain bound <em>N</em>. The idea of it is following: create a list of numbers up to <em>N</em> and then iteratively mark multiples of whole numbers as non-primes. At the end all remaining (non-marked) numbers are primes. If we need to find all primes under <em>N</em>, this algorithm is more efficient than testing each number under <em>N</em> for primality separately (we’re making use of a kind of <a href="dynamic_programming.md">dynamic programming</a> approach).</p>

<p><strong><a href="factorization.md">Prime factorization</a></strong>: We can factor a number by repeatedly <a href="brute_force.md">brute force</a> checking its divisibility by individual primes and there exist many algorithms applying various optimizations (wheel factorization, Dixon’s factorization, …), however for factoring large (hundreds of bits) primes there exists no known efficient algorithm, i.e. one that would run in <a href="polynomial_time.md">polynomial time</a>, and it is believed no such algorithm exists (see <a href="p_vs_np.md">P vs NP</a>). Many cryptographic algorithms, e.g. <a href="rsa.md">RSA</a>, rely on factorization being inefficient. For <a href="quantum.md">quantum</a> computers a polynomial (“fast”) algorithm exists, it’s called <a href="shors_algorithm.md">Shor’s algorithm</a>.</p>]]></content><author><name></name></author><summary type="html"><![CDATA[Prime Number]]></summary></entry><entry><title type="html">The Future</title><link href="https://promathx.github.io/future-posts-plans/" rel="alternate" type="text/html" title="The Future" /><published>2025-04-11T00:00:00+00:00</published><updated>2025-04-11T00:00:00+00:00</updated><id>https://promathx.github.io/future-posts-plans</id><content type="html" xml:base="https://promathx.github.io/future-posts-plans/"><![CDATA[<h2 id="notice">Notice</h2>

<p>Lost access to my original GitHub account: <a href="https://github.com/znkkw">github.com/znkkw</a> — locked out due to 2FA with no recovery.</p>

<p>New code mirror is live at: <a href="https://codeberg.org/znkkw">codeberg.org/znkkw</a>. Repositories will be gradually re-uploaded there.</p>

<p>Currently inactive due to study commitments. Expect minimal updates for now.
A proper write-up will follow when time permits.</p>]]></content><author><name></name></author><summary type="html"><![CDATA[Notice]]></summary></entry><entry><title type="html">The Internet is dead, there is nothing we can do about it</title><link href="https://promathx.github.io/the-internet-is-dead/" rel="alternate" type="text/html" title="The Internet is dead, there is nothing we can do about it" /><published>2025-01-04T00:00:00+00:00</published><updated>2025-01-04T00:00:00+00:00</updated><id>https://promathx.github.io/the-internet-is-dead</id><content type="html" xml:base="https://promathx.github.io/the-internet-is-dead/"><![CDATA[<p>The speed of the Internet increased, opportunities expanded, and intercontinental communication between humans flourished. These are the core concepts of the Internet.
However, we as humanity have failed it miserably. It has become the monster it is today. Visiting a website without an ad blocker and anti-fingerprinting technologies is now unimaginable. Ads, cookies, and autoplay videos are just a few of the many annoyances one must endure.</p>

<p>Yet, it was manageable and doable. You could install Pi-hole on your Raspberry Pi, run it as your local DNS, harden Firefox, or even use Tor and its associated browser. The Internet was usable and, to some extent, enjoyable.</p>

<p>With the rise of Large Language Models (LLMs), often incorrectly referred to as AI by the technically illiterate, everything changed. So-called ‘slop’ emerged from the TPUs of these neural networks2. An unprecedented amount of data and content flooded the web. Everyone wants to create LLM content, but simultaneously, everyone wants to dismantle it. People appreciate human-like content, texts, poems, movies, articles, and essays, yet few create them, instead letting machines do the work. It’s despicable.
The concerning part is that it’s becoming increasingly difficult to differentiate between human-created and machine-generated content2. You cannot easily see or notice if it was made by a human or hallucinated by a machine.</p>

<p>Therefore, avoiding the Internet in its current state may be the most prudent course of action one could take</p>]]></content><author><name></name></author><summary type="html"><![CDATA[The speed of the Internet increased, opportunities expanded, and intercontinental communication between humans flourished. These are the core concepts of the Internet. However, we as humanity have failed it miserably. It has become the monster it is today. Visiting a website without an ad blocker and anti-fingerprinting technologies is now unimaginable. Ads, cookies, and autoplay videos are just a few of the many annoyances one must endure.]]></summary></entry><entry><title type="html">Cguide</title><link href="https://promathx.github.io/CGuide/" rel="alternate" type="text/html" title="Cguide" /><published>2024-05-06T00:00:00+00:00</published><updated>2024-05-06T00:00:00+00:00</updated><id>https://promathx.github.io/CGuide</id><content type="html" xml:base="https://promathx.github.io/CGuide/"><![CDATA[<h1 id="c">C</h1>

<p><strong>Is C low or high level?</strong> This depends on the context. Firstly back in the day when most computers were programmed in <a href="assembly.md">assembly</a>, C was seen as high level, simply because it offered the highest level of abstraction at the time, while nowadays with languages like <a href="python.md">Python</a> and <a href="js.md">JavaScript</a> around people see C as very low level by comparison – so it really depends on if you talk about C in context of “old” or “modern” programming and which languages you compare it to. Secondly it also depends on HOW you program in C – you may choose to imitate assembly programming in C a lot, avoid using libraries, touch hardware directly, avoid using complex features and creating your own abstractions – here you are really doing low level programming. On the other hand you can emulate the “modern” high-level style programming in C too, you can even mimic <a href="oop.md">OOP</a> and make it kind of “C++ with different syntax”, you may use libraries that allow you to easily work with strings, heavy macros that pimp the language to some spectacular abomination, you may write your own garbage collector etc. – here you are basically doing high level programming in C.</p>

<h2 id="standards">Standards</h2>

<p>C is not a single language, there have been a few standards over the years since its inception in 1970s. The notable standards and versions are:</p>

<ul>
  <li><strong>K&amp;R C</strong>: C as described by its inventors in the book <em>The C Programming Language</em>, before official standardization. This is kind of too ancient nowadays.</li>
  <li><strong>C89/C90 (ANSI/ISO C)</strong>: First fully standardized version, usable even today, many hardcore C programmers stick to this version so as to enjoy maximum compiler support.</li>
  <li><strong>C95</strong>: A minor update of the previous standard, adds wide character support.</li>
  <li><strong>C99</strong>: Updated standard from the year 1999, striking a nice balance between “<a href="modern.md">modern</a>” and “good old”. This is a good version to use in <a href="lrs.md">LRS</a> programs, but will be a little less supported than C89, even though still very well supported. Notable new features against C89 include <code class="language-plaintext highlighter-rouge">//</code> comments, <a href="stdint.md">stdint</a> library (fixed-width integer types), <a href="float.md">float</a> and <code class="language-plaintext highlighter-rouge">long long</code> type, variable length stack-allocated <a href="array.md">arrays</a>, variadic <a href="macro.md">macros</a> and declaration of variables “anywhere” (not just at function start).</li>
  <li><strong>C11</strong>: Updated standard from the year 2011. This one is too  <a href="bloat.md">bloated</a> and isn’t worth using.</li>
  <li><strong>C17/C18</strong>: Yet another update, yet more bloated and not worth using anymore.</li>
</ul>

<h2 id="standard-library">Standard Library</h2>

<p>Besides the pure C language the C standard specifies a set of <a href="library.md">libraries</a> that have to come with a standard-compliant C implementation – so called standard library. This includes e.g. the <em>stdio</em> library for performing standard <a href="io.md">input/output</a> (reading/writing to/from screen/files) or the <em>math</em> library for mathematical functions. It is usually relatively okay to use these libraries as they are required by the standard to exist so the <a href="dependency.md">dependency</a> they create is not as dangerous, however many C implementations aren’t completely compliant with the standard and may come without the standard library. Also many stdlib implementations suck or you just can’t be sure what the implementation will prefer (size? speed?) etc. So for sake of <a href="portability.md">portability</a> it is best if you can avoid using standard library.</p>

<p>The standard library (libc) is a subject of live debate because while its interface and behavior are given by the C standard, its implementation is a matter of each compiler; since the standard library is so commonly used, we should take great care in assuring it’s extremely well written, however we ALWAYS have to choose our priorities and make tradeoffs, there just mathematically CANNOT be an ultimate implementation that will be all extremely fast and extremely memory efficient and extremely portable and extremely small. So choosing your C environment usually comprises of choosing the C compiler and the stdlib implementation. As you probably guessed, the popular implementations (<a href="glibc.md">glibc</a> et al) are <a href="bloat.md">bloat</a> and also often just <a href="shit.md">shit</a>. Better alternatives thankfully exist, such as:</p>

<ul>
  <li><a href="musl.md">musl</a></li>
  <li><a href="uclibc.md">uclibc</a></li>
  <li><a href="dependency.md">not using</a> the standard library :)</li>
</ul>

<h2 id="basics">Basics</h2>

<p>This is a quick overview, for a more in depth tutorial see <a href="c_tutorial.md">C tutorial</a>.</p>

<p>A simple program in C that writes “welcome to C” looks like this:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt; // standard I/O library

int main(void)
{
  // this is the main program    

  puts("welcome to C");

  return 0; // end with success
}
</code></pre></div></div>

<p>You can simply paste this code into a file which you name e.g. <code class="language-plaintext highlighter-rouge">program.c</code>, then you can compile the program from command line like this:</p>

<p><code class="language-plaintext highlighter-rouge">gcc -o program program.c</code></p>

<p>Then if you run the program from command line (<code class="language-plaintext highlighter-rouge">./program</code> on Unix like systems) you should see the message.</p>

<h2 id="cheatsheetoverview">Cheatsheet/Overview</h2>

<p>Here is a quick reference cheatsheet of some of the important things in C, also a possible overview of the language.</p>

<p><strong>data types</strong> (just some):</p>

<table>
  <thead>
    <tr>
      <th>data type</th>
      <th>values (size)</th>
      <th>printf</th>
      <th>notes</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">int</code> (<code class="language-plaintext highlighter-rouge">signed int</code>, …)</td>
      <td>integer, at least -32767 to 32767 (16 bit), often more</td>
      <td><code class="language-plaintext highlighter-rouge">%d</code></td>
      <td>native integer, <strong>fast</strong> (prefer for speed)</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">unsigned int</code></td>
      <td>integer, non-negative, at least 0 to 65535, often more</td>
      <td><code class="language-plaintext highlighter-rouge">%u</code></td>
      <td>same as <code class="language-plaintext highlighter-rouge">int</code> but no negative values</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">signed char</code></td>
      <td>integer, at least -127 to 127, mostly -128 to 127</td>
      <td><code class="language-plaintext highlighter-rouge">%c</code>, <code class="language-plaintext highlighter-rouge">%hhi</code></td>
      <td><code class="language-plaintext highlighter-rouge">char</code> forced to be signed</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">unsigned char</code></td>
      <td>integer, at least 0 to 255 (almost always the case)</td>
      <td><code class="language-plaintext highlighter-rouge">%c</code>, <code class="language-plaintext highlighter-rouge">%hhu</code></td>
      <td>smallest memory chunk, <strong><a href="byte.md">byte</a></strong></td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">char</code></td>
      <td>integer, at least 256 values</td>
      <td><code class="language-plaintext highlighter-rouge">%c</code></td>
      <td>signed or unsigned, used for string characters</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">short</code></td>
      <td>integer, at least -32767 to 32767 (16 bit)</td>
      <td><code class="language-plaintext highlighter-rouge">%hd</code></td>
      <td>like <code class="language-plaintext highlighter-rouge">int</code> but supposed to be smaller</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">unsigned short</code></td>
      <td>integer, non-negative, at least 0 to 65535</td>
      <td><code class="language-plaintext highlighter-rouge">%hu</code></td>
      <td>like <code class="language-plaintext highlighter-rouge">short</code> but unsigned</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">long</code></td>
      <td>integer, at least -2147483647 to 2147483647 (32 bit)</td>
      <td><code class="language-plaintext highlighter-rouge">%ld</code></td>
      <td>for big signed values</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">unsigned long</code></td>
      <td>integer, at least 0 to 4294967295 (32 bit)</td>
      <td><code class="language-plaintext highlighter-rouge">%lu</code></td>
      <td>for big unsigned values</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">long long</code></td>
      <td>integer, at least some -9 * 10^18 to 9 * 10^18 (64 bit)</td>
      <td><code class="language-plaintext highlighter-rouge">%lld</code></td>
      <td>for very big signed values</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">unsigned long long</code></td>
      <td>integer, at least 0 to 18446744073709551615 (64 bit)</td>
      <td><code class="language-plaintext highlighter-rouge">%llu</code></td>
      <td>for very big unsigned values</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">float</code></td>
      <td>floating point, some -3 * 10^38 to 3 * 10^38</td>
      <td><code class="language-plaintext highlighter-rouge">%f</code></td>
      <td><a href="float.md">float</a>, tricky, bloat, can be slow, avoid</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">double</code></td>
      <td>floating point, some -1 * 10^308 to 10^308</td>
      <td><code class="language-plaintext highlighter-rouge">%lf</code></td>
      <td>like <code class="language-plaintext highlighter-rouge">float</code> but bigger</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">T [N]</code></td>
      <td>array of <code class="language-plaintext highlighter-rouge">N</code> values of type <code class="language-plaintext highlighter-rouge">T</code></td>
      <td> </td>
      <td><strong>array</strong>, if <code class="language-plaintext highlighter-rouge">T</code> is <code class="language-plaintext highlighter-rouge">char</code> then <strong>string</strong></td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">T *</code></td>
      <td>memory address</td>
      <td><code class="language-plaintext highlighter-rouge">%p</code></td>
      <td>pointer to type <code class="language-plaintext highlighter-rouge">T</code>, (if <code class="language-plaintext highlighter-rouge">char</code> then <strong>string</strong>)</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">uint8_t</code></td>
      <td>0 to 255 (8 bit)</td>
      <td><code class="language-plaintext highlighter-rouge">PRIu8</code></td>
      <td>exact width, two’s compl., must include <code class="language-plaintext highlighter-rouge">&lt;stdint.h&gt;</code></td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">int8_t</code></td>
      <td>-128 to 127 (8 bit)</td>
      <td><code class="language-plaintext highlighter-rouge">PRId8</code></td>
      <td>like <code class="language-plaintext highlighter-rouge">uint8_t</code> but signed</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">uint16_t</code></td>
      <td>0 to 65535 (16 bit)</td>
      <td><code class="language-plaintext highlighter-rouge">PRIu16</code></td>
      <td>like <code class="language-plaintext highlighter-rouge">uint8_t</code> but 16 bit</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">int16_t</code></td>
      <td>-32768 to 32767 (16 bit)</td>
      <td><code class="language-plaintext highlighter-rouge">PRId16</code></td>
      <td>like <code class="language-plaintext highlighter-rouge">uint16_t</code> but signed</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">uint32_t</code></td>
      <td>-2147483648 to 2147483647 (32 bit)</td>
      <td><code class="language-plaintext highlighter-rouge">PRIu32</code></td>
      <td>like <code class="language-plaintext highlighter-rouge">uint8_t</code> but 32 bit</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">int32_t</code></td>
      <td>0 to 4294967295 (32 bit)</td>
      <td><code class="language-plaintext highlighter-rouge">PRId32</code></td>
      <td>like <code class="language-plaintext highlighter-rouge">uint32_t</code> but signed</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">int_least8_t</code></td>
      <td>at least -128 to 127</td>
      <td><code class="language-plaintext highlighter-rouge">PRIdLEAST8</code></td>
      <td>signed integer with at least 8 bits, <code class="language-plaintext highlighter-rouge">&lt;stdint.h&gt;</code></td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">int_fast8_t</code></td>
      <td>at least -128 to 127</td>
      <td><code class="language-plaintext highlighter-rouge">PRIdFAST8</code></td>
      <td>fast signed int. with at least 8 bits, <code class="language-plaintext highlighter-rouge">&lt;stdint.h&gt;</code></td>
    </tr>
    <tr>
      <td>struct</td>
      <td> </td>
      <td> </td>
      <td>structured data type</td>
    </tr>
  </tbody>
</table>

<p>There is no <strong>bool</strong> (true, false), use any integer type, 0 is false, everything else is true (there may be some bool type in the stdlib, don’t use that). A <strong>string</strong> is just array of chars, it has to end with value 0 (NOT ASCII character for “0” but literally integer value 0)!</p>

<p><strong>main program structure</strong>:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;

int main(void)
{
  // code here
  return 0;
}
</code></pre></div></div>

<p><strong>branching aka if-then-else</strong>:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>if (CONDITION)
{
  // do something here
}
else // optional
{
  // do something else here
}
</code></pre></div></div>

<p><strong>for loop</strong> (repeat given number of times):</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>for (int i = 0; i &lt; MAX; ++i)
{
  // do something here, you can use i
}
</code></pre></div></div>

<p><strong>while loop</strong> (repeat while CONDITION holds):</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>while (CONDITION)
{
  // do something here
}
</code></pre></div></div>

<p><strong>do while loop</strong> (same as <em>while</em> but CONDITION at the end), not used that much:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>do
{
  // do something here
} while (CONDITION);
</code></pre></div></div>

<p><strong>function definition</strong>:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>RETURN_TYPE myFunction (TYPE1 param1, TYPE2 param2, ...)
{ // return type can be void
  // do something here
}
</code></pre></div></div>
<p><strong>compilation</strong> (you can replace <code class="language-plaintext highlighter-rouge">gcc</code> with another compiler):</p>

<ul>
  <li>quickly compile and run: <code class="language-plaintext highlighter-rouge">gcc myprogram.c &amp;&amp; ./a.out</code>.</li>
  <li>compile more properly: <code class="language-plaintext highlighter-rouge">gcc -std=c99 -Wall -Wextra -pedantic -O3 -o myprogram myprogram.c</code>.</li>
</ul>

<p>To <strong>link</strong> a library use <code class="language-plaintext highlighter-rouge">-llibrary</code>, e.g. <code class="language-plaintext highlighter-rouge">-lm</code> (when using <code class="language-plaintext highlighter-rouge">&lt;math.h&gt;</code>), <code class="language-plaintext highlighter-rouge">-lSDL2</code> etc.</p>

<p>The following are some symbols (functions, macros, …) from the <strong>standard library</strong>:</p>

<table>
  <thead>
    <tr>
      <th>symbol</th>
      <th>library</th>
      <th>description</th>
      <th>example</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><em>putchar(c)</em></td>
      <td><em>stdio.h</em></td>
      <td>Writes a single character to output.</td>
      <td><code class="language-plaintext highlighter-rouge">putchar('a');</code></td>
    </tr>
    <tr>
      <td><em>getchar()</em></td>
      <td><em>stdio.h</em></td>
      <td>Reads a single character from input.</td>
      <td><code class="language-plaintext highlighter-rouge">int inputChar = getchar();</code></td>
    </tr>
    <tr>
      <td><em>puts(s)</em></td>
      <td><em>stdio.h</em></td>
      <td>Writes string to output (adds newline at the end).</td>
      <td><code class="language-plaintext highlighter-rouge">puts("hello");</code></td>
    </tr>
    <tr>
      <td><em>printf(s, a, b, …)</em></td>
      <td><em>stdio.h</em></td>
      <td>Complex print func., allow printing numbers, their formatting etc.</td>
      <td><code class="language-plaintext highlighter-rouge">printf("value is %d\n",var);</code></td>
    </tr>
    <tr>
      <td><em>scanf(s, a, b, …)</em></td>
      <td><em>stdio.h</em></td>
      <td>Complex reading func., allows reading numbers etc.</td>
      <td><code class="language-plaintext highlighter-rouge">scanf("%d",&amp;var);</code></td>
    </tr>
    <tr>
      <td><em>fopen(f,mode)</em></td>
      <td><em>stdio.h</em></td>
      <td>Opens file with given name in specific mode, returns pointer.</td>
      <td><code class="language-plaintext highlighter-rouge">FILE *myFile = fopen("myfile.txt","r");</code></td>
    </tr>
    <tr>
      <td><em>fclose(f)</em></td>
      <td><em>stdio.h</em></td>
      <td>Closes previously opened file.</td>
      <td><code class="language-plaintext highlighter-rouge">fclose(myFile);</code></td>
    </tr>
    <tr>
      <td><em>fputc(c,f)</em></td>
      <td><em>stdio.h</em></td>
      <td>Writes a single character to file.</td>
      <td><code class="language-plaintext highlighter-rouge">fputc('a',myFile);</code></td>
    </tr>
    <tr>
      <td><em>fgetc(f)</em></td>
      <td><em>stdio.h</em></td>
      <td>Reads a single character from file.</td>
      <td><code class="language-plaintext highlighter-rouge">int fileChar = fgetc(myFile);</code></td>
    </tr>
    <tr>
      <td><em>fputs(s,f)</em></td>
      <td><em>stdio.h</em></td>
      <td>Writes string to file (without newline at end).</td>
      <td><code class="language-plaintext highlighter-rouge">fputs("hello",myFile);</code></td>
    </tr>
    <tr>
      <td><em>fprintf(s, a, b, …)</em></td>
      <td><em>stdio.h</em></td>
      <td>Like <code class="language-plaintext highlighter-rouge">printf</code> but outputs to a file.</td>
      <td><code class="language-plaintext highlighter-rouge">fprintf(myFile,"value is %d\n",var);</code></td>
    </tr>
    <tr>
      <td><em>fscanf(f, s, a, b, …)</em></td>
      <td><em>stdio.h</em></td>
      <td>Like <code class="language-plaintext highlighter-rouge">scanf</code> but reads from a file.</td>
      <td><code class="language-plaintext highlighter-rouge">fscanf(myFile,"%d",&amp;var);</code></td>
    </tr>
    <tr>
      <td><em>fread(data,size,n,f)</em></td>
      <td><em>stdio.h</em></td>
      <td>Reads <em>n</em> elems to <em>data</em> from <em>file</em>, returns no. of elems read.</td>
      <td><code class="language-plaintext highlighter-rouge">fread(myArray,sizeof(item),1,myFile);</code></td>
    </tr>
    <tr>
      <td><em>fwrite(data,size,n,f)</em></td>
      <td><em>stdio.h</em></td>
      <td>Writes <em>n</em> elems from <em>data</em> to <em>file</em>, returns no. of elems writ.</td>
      <td><code class="language-plaintext highlighter-rouge">fwrite(myArray,sizeof(item),1,myFile);</code></td>
    </tr>
    <tr>
      <td><em>EOF</em></td>
      <td><em>stdio.h</em></td>
      <td><a href="eof.md">End of file</a> value.</td>
      <td><code class="language-plaintext highlighter-rouge">int c = getchar(); if (c == EOF) break;</code></td>
    </tr>
    <tr>
      <td><em>rand()</em></td>
      <td><em>stdlib.h</em></td>
      <td>Returns pseudorandom number.</td>
      <td><code class="language-plaintext highlighter-rouge">char randomLetter = 'a' + rand() % 26;</code></td>
    </tr>
    <tr>
      <td><em>srand(n)</em></td>
      <td><em>stdlib.h</em></td>
      <td>Seeds pseudorandom number generator.</td>
      <td><code class="language-plaintext highlighter-rouge">srand(time(NULL));</code></td>
    </tr>
    <tr>
      <td><em>NULL</em></td>
      <td><em>stdlib.h</em>, …</td>
      <td>Value assigned to pointers that point “nowhere”.</td>
      <td><code class="language-plaintext highlighter-rouge">int *myPointer = NULL;</code></td>
    </tr>
    <tr>
      <td><em>malloc(size)</em></td>
      <td><em>stdlib.h</em></td>
      <td>Dynamically allocates memory, returns pointer to it (or NULL).</td>
      <td><code class="language-plaintext highlighter-rouge">int *myArr = malloc(sizeof(int) * 10);</code></td>
    </tr>
    <tr>
      <td><em>realloc(mem,size)</em></td>
      <td><em>stdlib.h</em></td>
      <td>Resizes dynamically allocates memory, returns pointer (or NULL).</td>
      <td><code class="language-plaintext highlighter-rouge">myArr = realloc(myArr,sizeof(int) * 20);</code></td>
    </tr>
    <tr>
      <td><em>free(mem)</em></td>
      <td><em>stdlib.h</em></td>
      <td>Frees dynamically allocated memory.</td>
      <td><code class="language-plaintext highlighter-rouge">free(myArr);</code></td>
    </tr>
    <tr>
      <td><em>atof(str)</em></td>
      <td><em>stdlib.h</em></td>
      <td>Converts string to floating point number.</td>
      <td><code class="language-plaintext highlighter-rouge">double val = atof(answerStr);</code></td>
    </tr>
    <tr>
      <td><em>atoi(str)</em></td>
      <td><em>stdlib.h</em></td>
      <td>Converts string to integer number.</td>
      <td><code class="language-plaintext highlighter-rouge">int val = atof(answerStr);</code></td>
    </tr>
    <tr>
      <td><em>EXIT_SUCCESS</em></td>
      <td><em>stdlib.h</em></td>
      <td>Value the program should return on successful exit.</td>
      <td><code class="language-plaintext highlighter-rouge">return EXIT_SUCCESS;</code></td>
    </tr>
    <tr>
      <td><em>EXIT_FAILURE</em></td>
      <td><em>stdlib.h</em></td>
      <td>Value the program should return on exit with error.</td>
      <td><code class="language-plaintext highlighter-rouge">return EXIT_FAILURE;</code></td>
    </tr>
    <tr>
      <td><em>sin(x)</em></td>
      <td><em>math.h</em></td>
      <td>Returns <a href="sin.md">sine</a> of angle in <a href="rad.md">RADIANS</a>.</td>
      <td><code class="language-plaintext highlighter-rouge">float angleSin = sin(angle);</code></td>
    </tr>
    <tr>
      <td><em>cos(x)</em></td>
      <td><em>math.h</em></td>
      <td>Like <code class="language-plaintext highlighter-rouge">sin</code> but returns cosine.</td>
      <td><code class="language-plaintext highlighter-rouge">float angleCos = cos(angle);</code></td>
    </tr>
    <tr>
      <td><em>tan(x)</em></td>
      <td><em>math.h</em></td>
      <td>Returns <a href="tan.md">tangent</a> of angle in RADIANS.</td>
      <td><code class="language-plaintext highlighter-rouge">float angleTan = tan(angle);</code></td>
    </tr>
    <tr>
      <td><em>asin(x)</em></td>
      <td><em>math.h</em></td>
      <td>Returns arcus sine of angle, in RADIANS.</td>
      <td><code class="language-plaintext highlighter-rouge">float angle = asin(angleSine);</code></td>
    </tr>
    <tr>
      <td><em>ceil(x)</em></td>
      <td><em>math.h</em></td>
      <td>Rounds a floating point value up.</td>
      <td><code class="language-plaintext highlighter-rouge">double x = ceil(y);</code></td>
    </tr>
    <tr>
      <td><em>floor(x)</em></td>
      <td><em>math.h</em></td>
      <td>Rounds a floating point value down.</td>
      <td><code class="language-plaintext highlighter-rouge">double x = floor(y);</code></td>
    </tr>
    <tr>
      <td><em>fmod(a,b)</em></td>
      <td><em>math.h</em></td>
      <td>Returns floating point reminded after division.</td>
      <td><code class="language-plaintext highlighter-rouge">double rem = modf(x,3.5);</code></td>
    </tr>
    <tr>
      <td><em>isnan(x)</em></td>
      <td><em>math.h</em></td>
      <td>Checks if given float value is NaN.</td>
      <td><code class="language-plaintext highlighter-rouge">if (!isnan(x))</code></td>
    </tr>
    <tr>
      <td><em>NAN</em></td>
      <td><em>math.h</em></td>
      <td>Float quiet <a href="nan.md">NaN</a> (not a number) value, don’t compare!</td>
      <td><code class="language-plaintext highlighter-rouge">if (y == 0) return NAN;</code></td>
    </tr>
    <tr>
      <td><em>log(x)</em></td>
      <td><em>math.h</em></td>
      <td>Computes natural <a href="log.md">logarithm</a> (base <a href="e.md">e</a>).</td>
      <td><code class="language-plaintext highlighter-rouge">double x = log(y);</code></td>
    </tr>
    <tr>
      <td><em>log10(x)</em></td>
      <td><em>math.h</em></td>
      <td>Computes decadic <a href="log.md">logarithm</a> (base 10).</td>
      <td><code class="language-plaintext highlighter-rouge">double x = log10(y);</code></td>
    </tr>
    <tr>
      <td><em>log2(x)</em></td>
      <td><em>math.h</em></td>
      <td>Computes binary <a href="log.md">logarithm</a> (base 2).</td>
      <td><code class="language-plaintext highlighter-rouge">double x = log2(y);</code></td>
    </tr>
    <tr>
      <td><em>exp(x)</em></td>
      <td><em>math.h</em></td>
      <td>Computes exponential function (<em>e^x</em>).</td>
      <td><code class="language-plaintext highlighter-rouge">double x = exp(y);</code></td>
    </tr>
    <tr>
      <td><em>sqrt(x)</em></td>
      <td><em>math.h</em></td>
      <td>Computes floating point <a href="sqrt.md">square root</a>.</td>
      <td><code class="language-plaintext highlighter-rouge">double dist = sqrt(dx * dx + dy * dy);</code></td>
    </tr>
    <tr>
      <td><em>pow(a,b)</em></td>
      <td><em>math.h</em></td>
      <td>Power, raises <em>a</em> to <em>b</em> (both floating point).</td>
      <td><code class="language-plaintext highlighter-rouge">double cubeRoot = pow(var,1.0/3.0);</code></td>
    </tr>
    <tr>
      <td><em>abs(x)</em></td>
      <td><em>math.h</em></td>
      <td>Computes <a href="abs.md">absolute value</a>.</td>
      <td><code class="language-plaintext highlighter-rouge">double varAbs = abs(var);</code></td>
    </tr>
    <tr>
      <td><em>INT_MAX</em></td>
      <td><em>limits.h</em></td>
      <td>Maximum value that can be stored in <code class="language-plaintext highlighter-rouge">int</code> type.</td>
      <td><code class="language-plaintext highlighter-rouge">printf("int max: %d\n",INT_MAX);</code></td>
    </tr>
    <tr>
      <td><em>memset(mem,val,size)</em></td>
      <td><em>string.h</em></td>
      <td>Fills block of memory with given values.</td>
      <td><code class="language-plaintext highlighter-rouge">memset(myArr,0,sizeof(myArr));</code></td>
    </tr>
    <tr>
      <td><em>memcpy(dest,src,size)</em></td>
      <td><em>string.h</em></td>
      <td>Copies bytes of memory from one place to another, returns dest.</td>
      <td><code class="language-plaintext highlighter-rouge">memcpy(destArr,srcArr,sizeof(srcArr);</code></td>
    </tr>
    <tr>
      <td><em>strcpy(dest,src)</em></td>
      <td><em>string.h</em></td>
      <td>Copies string (zero terminated) to dest, unsafe.</td>
      <td><code class="language-plaintext highlighter-rouge">char myStr[16]; strcpy(myStr,"hello");</code></td>
    </tr>
    <tr>
      <td><em>strncpy(dest,src,n)</em></td>
      <td><em>string.h</em></td>
      <td>Like <code class="language-plaintext highlighter-rouge">strcpy</code> but limits max number of bytes to copy, safer.</td>
      <td><code class="language-plaintext highlighter-rouge">strncpy(destStr,srcStr,sizeof(destStr));</code></td>
    </tr>
    <tr>
      <td><em>strcmp(s1,s2)</em></td>
      <td><em>string.h</em></td>
      <td>Compares two strings, returns 0 if equal.</td>
      <td><code class="language-plaintext highlighter-rouge">if (!strcmp(str1,"something"))</code></td>
    </tr>
    <tr>
      <td><em>strlen(str)</em></td>
      <td><em>string.h</em></td>
      <td>Returns length of given string.</td>
      <td><code class="language-plaintext highlighter-rouge">int l = strlen(myStr);</code></td>
    </tr>
    <tr>
      <td><em>strstr(str,substr)</em></td>
      <td><em>string.h</em></td>
      <td>Finds substring in string, returns pointer to it (or NULL).</td>
      <td><code class="language-plaintext highlighter-rouge">if (strstr(cmdStr,"quit") != NULL)</code></td>
    </tr>
    <tr>
      <td><em>time(t)</em></td>
      <td><em>time.h</em></td>
      <td>Stores calendar time (often Unix t.) in t (can be NULL), returns it.</td>
      <td><code class="language-plaintext highlighter-rouge">printf("tstamp: %d\n",(int) time(NULL));</code></td>
    </tr>
    <tr>
      <td><em>clock()</em></td>
      <td><em>time.h</em></td>
      <td>Returns approx. CPU cycle count since program start.</td>
      <td><code class="language-plaintext highlighter-rouge">printf("CPU ticks: %d\n",(int) clock());</code></td>
    </tr>
    <tr>
      <td><em>CLOCKS_PER_SEC</em></td>
      <td><em>time.h</em></td>
      <td>Number of CPU ticks per second.</td>
      <td><code class="language-plaintext highlighter-rouge">int sElapsed = clock() / CLOCKS_PER_SEC;</code></td>
    </tr>
  </tbody>
</table>

<h1 id="c-tutorial">C Tutorial</h1>

<p>This is a relatively quick <a href="c.md">C</a> tutorial.</p>

<p>You should probably how at least some basic awareness of essential programming concepts before reading this (what’s a <a href="programming_language.md">programming language</a>, <a href="source_code.md">source code</a>, [command line]
(cli.md) etc.). If you’re as far as already somewhat knowing another language, this should be pretty easy to understand.</p>

<p>This tutorial focuses on teaching pure C, i.e. <strong>mostly just command line text-only programs</strong>. There is a small bonus that shows some very basics of doing graphics programming at the end, but bear in mind it’s 
inevitable to learn step by step, as much as you want to start programming graphical games, you first HAVE TO learn the language itself well. Don’t rush it. Trust this advice, it is sincere.</p>

<p>If you do two chapters a day (should take like half and hour), in a week you’ll know some basic C.</p>

<p>Potentially supplemental articles to this tutorial are:</p>

<ul>
  <li><a href="c.md">C</a></li>
  <li><a href="algorithm.md">algorithm</a></li>
  <li><a href="programming_tips.md">programming tips</a></li>
  <li><a href="programming_style.md">programming style</a></li>
  <li><a href="debugging.md">debugging</a></li>
  <li><a href="exercises.md">exercises</a></li>
  <li><a href="c_pitfalls.md">C pitfalls</a></li>
  <li><a href="memory_management.md">memory management</a></li>
  <li><a href="optimization.md">optimization</a></li>
  <li>…</li>
</ul>

<h2 id="about-c-and-programming">About C And Programming</h2>

<p><a href="c.md">C</a> is</p>

<ul>
  <li>A <strong><a href="programming_language.md">programming language</a></strong>, i.e. a language that lets you express <a href="algorithm.md">algorithms</a>.</li>
  <li><a href="compiled.md">Compiled</a> language (as opposed to <a href="interpreted.md">interpreted</a>), i.e. you have to compile the code you write (with <a href="compiler.md">compiler</a>) in order to obtain a <a href="native.md">native</a> executable 
program (a binary file that you can run directly).</li>
  <li>Extremely <strong>fast and efficient</strong>.</li>
  <li>Very <strong>widely supported and portable</strong> to almost anything.</li>
  <li><strong><a href="low_level.md">Low level</a></strong>, i.e. there is relatively little <a href="abstraction.md">abstraction</a> and not many comfortable built-in functionality such as <a href="garbage_collection.md">garbage collection</a>, you have to write 
many things yourself, you will deal with <a href="pointer.md">pointers</a>, <a href="endianness.md">endianness</a> etc.</li>
  <li><a href="imperative.md">Imperative</a> (based on sequences of commands), without <a href="oop.md">object oriented programming</a>.</li>
  <li>Considered <strong>hard</strong>, but in certain ways it’s simple, it lacks <a href="bloat.md">bloat</a> and <a href="bullshit.md">bullshit</a> of “<a href="modern.md">modern</a>” languages which is an essential thing. It will take long to learn (don’t 
worry, not nearly as long as learning a foreign language) but it’s the most basic thing you should know if you want to create good software. You won’t regret.</li>
  <li><strong>Not holding your hand</strong>, i.e. you may very easily “shoot yourself in your foot” and crash your program. This is the price for the language’s power.</li>
  <li>Very old, well established and tested by time.</li>
  <li>Recommended by us for serious programs.</li>
</ul>

<p>If you come from a language like <a href="python.md">Python</a> or <a href="javascript.md">JavaScript</a>, you may be shocked that C doesn’t come with its own <a href="package_manager.md">package manager</a>, <a href="debugger.md">debugger</a> or <a href="build_system.md">build 
system</a>, it doesn’t have <a href="module.md">modules</a>, <a href="generics.md">generics</a>, <a href="garbage_collection.d">garabage collection</a>, <a href="oop.md">OOP</a>, <a href="hashmap.md">hashmaps</a>, dynamic <a href="list.md">lists</a>, [type inference]
(type_inference.md) and similar “<a href="modern.md">modern</a>” features. When you truly get into C, you’ll find it’s a good thing.</p>

<p>Programming in C works like this:</p>

<ol>
  <li>You write a C source code into a file.</li>
  <li>You compile the file with a C <a href="compiler.md">compiler</a> such as <a href="gcc.md">gcc</a> (which is just a program that turns source code into a runnable program). This gives you the executable program.</li>
  <li>You run the program, test it, see how it works and potentially get back to modifying the source code (step 1).</li>
</ol>

<p>So, for writing the source code you’ll need a <a href="text_editor.md">text editor</a>; any <a href="plain_text.md">plain text</a> editor will do but you should use some that can highlight C <a href="syntax.md">syntax</a>  – this helps very much 
when programming and is practically a necessity. Ideal editor is <a href="vim.md">vim</a> but it’s a bit difficult to learn so you can use something as simple as <a href="gedit.md">Gedit</a> or <a href="geany.md">Geany</a>. We do NOT recommend 
using huge programming <a href="ide.md">IDEs</a> such as “VS Code” and whatnot. You definitely can NOT use an advanced document editor that works with <a href="rich_text.md">rich text</a> such as <a href="libreoffice.md">LibreOffice</a> or that 
<a href="shit.md">shit</a> from Micro$oft, this won’t work because it’s not plain text.</p>

<p>Next you’ll need a C <a href="compiler.md">compiler</a>, the program that will turn your source code into a runnable program. We’ll use the most commonly used one called <a href="gcc.md">gcc</a> (you can try different ones such as 
<a href="clang.md">clang</a> or <a href="tcc.md">tcc</a> if you want). If you’re on a <a href="unix.md">Unix</a>-like system such as <a href="gnu.md">GNU</a>/<a href="linux.md">Linux</a> (which you probably should), gcc is probably already installed. Open up a terminal 
and write <code class="language-plaintext highlighter-rouge">gcc</code> to see if it’s installed – if not, then install it (e.g. with <code class="language-plaintext highlighter-rouge">sudo apt install build-essential</code> if you’re on a Debian-based system).</p>

<p>If you’re extremely lazy, there are online web C compilers that work in a web browser (find them with a search engine). You can use these for quick experiments but note there are some limitations (e.g. not being 
able to work with files), and you should definitely know how to compile programs yourself.</p>

<p>Last thing: there are multiple standards of C. Here we will be covering <a href="c99.md">C99</a>, but this likely doesn’t have to bother you at this point.</p>

<h2 id="first-program">First Program</h2>

<p>Let’s quickly try to compile a tiny program to test everything and see how everything works in practice.</p>

<p>Open your text editor and paste this code:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>/* simple C program! */

#include &lt;stdio.h&gt; // include IO library

int main(void)
{
  puts("It works.");
  
  return 0;
}
</code></pre></div></div>

<p>Save this file and name it <code class="language-plaintext highlighter-rouge">program.c</code>. Then open a terminal emulator (or an equivalent command line interface), locate yourself into the directory where you saved the file (e.g. <code class="language-plaintext highlighter-rouge">cd somedirectory</code>) and compile the 
program with the following command:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>gcc -o program program.c
</code></pre></div></div>

<p>The program should compile and the executable <code class="language-plaintext highlighter-rouge">program</code> should appear in the directory. You can run it with</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>./program
</code></pre></div></div>

<p>And you should see</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>It works.
</code></pre></div></div>

<p>written in the command line.</p>

<p>Now let’s see what the source code means:</p>

<ul>
  <li><code class="language-plaintext highlighter-rouge">/* simple C program! */</code> is so called <em>block comment</em>, it does nothing, it’s here only for the humans that will read the source code. Such comments can be almost anywhere in the code. The comment starts at <code class="language-plaintext highlighter-rouge">/*</code> 
and ends with <code class="language-plaintext highlighter-rouge">*/</code>.</li>
  <li><code class="language-plaintext highlighter-rouge">// include IO library</code> is another comment, but this is a <em>line comment</em>, it starts with <code class="language-plaintext highlighter-rouge">//</code> and ends with the end of line.</li>
  <li><code class="language-plaintext highlighter-rouge">#include &lt;stdio.h&gt;</code> tells the compiler we want to include a library named <em>stdio</em> (the weird <a href="syntax.md">syntax</a> will be explained in the future). This is a standard library with input output functions, we need 
it to be able to use the function <code class="language-plaintext highlighter-rouge">puts</code> later on. We can include more libraries if we want to. These includes are almost always at the very top of the source code.</li>
  <li><code class="language-plaintext highlighter-rouge">int main(void)</code> is the start of the main program. What exactly this means will be explained later, for now just remember there has to be this function named <code class="language-plaintext highlighter-rouge">main</code> in basically every program – inside it there 
are commands that will be executed when the program is run. Note that the curly brackets that follow (<code class="language-plaintext highlighter-rouge">{</code> and <code class="language-plaintext highlighter-rouge">}</code>) denote the block of code that belongs to this function, so we need to write our commands between 
these brackets.</li>
  <li><code class="language-plaintext highlighter-rouge">puts("It works.");</code> is a “command” for printing text strings to the command line (it’s a command from the <code class="language-plaintext highlighter-rouge">stdio</code> library included above). Why exactly this is written like this will be explained later, but for 
now notice the following. The command starts with its name (<code class="language-plaintext highlighter-rouge">puts</code>, for <em>put string</em>), then there are left and right brackets (<code class="language-plaintext highlighter-rouge">(</code> and <code class="language-plaintext highlighter-rouge">)</code>) between which there are arguments to the command, in our case there is 
one, the text string <code class="language-plaintext highlighter-rouge">"It works."</code>. Text strings have to be put between quotes (<code class="language-plaintext highlighter-rouge">"</code>), otherwise the compiler would think the words are other commands (the quotes are not part of the string itself, they won’t be 
printed out). The command is terminated by <code class="language-plaintext highlighter-rouge">;</code> – all “normal” commands in C have to end with a semicolon.</li>
  <li><code class="language-plaintext highlighter-rouge">return 0;</code> is another “command”, it basically tells the operating system that everything was terminated successfully (<code class="language-plaintext highlighter-rouge">0</code> is a code for success). This command is an exception in that it doesn’t have to have 
brackets (<code class="language-plaintext highlighter-rouge">(</code> and <code class="language-plaintext highlighter-rouge">)</code>). This doesn’t have to bother us too much now, let’s just remember this will always be the last command in our program.</li>
</ul>

<p>Also notice how the source code is formatted, e.g. the indentation of code within the <code class="language-plaintext highlighter-rouge">{</code> and <code class="language-plaintext highlighter-rouge">}</code> brackets. White characters (spaces, new lines, tabs) are ignored by the compiler so we can theoretically write our 
program on a single line, but that would be unreadable. We use indentation, spaces and empty lines to format the code to be well readable.</p>

<p>To sum up let’s see a general structure of a typical C program. You can just copy paste this for any new program and then just start writing commands in the <code class="language-plaintext highlighter-rouge">main</code> function.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt; // include the I/O library
// more libraries can be included here

int main(void)
{
  // write commands here
  
  return 0; // always the last command
}
</code></pre></div></div>

<h2 id="variables-arithmetic-data-types">Variables, Arithmetic, Data Types</h2>

<p>Programming is a lot like mathematics, we compute equations and transform numerical values into other values. You probably know in mathematics we use <em>variables</em> such as <em>x</em> or <em>y</em> to denote numerical values that 
can change (hence variables). In programming we also use variables – here <strong><a href="variable.md">variable</a> is a place in memory which has a name</strong> (and in this place there will be stored a value that can change over 
time).</p>

<p>We can create variables named <code class="language-plaintext highlighter-rouge">x</code>, <code class="language-plaintext highlighter-rouge">y</code>, <code class="language-plaintext highlighter-rouge">myVariable</code> or <code class="language-plaintext highlighter-rouge">score</code> and then store specific values (for now let’s only consider numbers) into them. We can read from and write to these variables at any time. These 
variables physically reside in <a href="ram.md">RAM</a>, but we don’t really care where exactly (at which address) they are located – this is e.g. similar to houses, in common talk we normally say things like <em>John’s house</em> 
or <em>the pet store</em> instead of <em>house with address 3225</em>.</p>

<p>Variable names can’t start with a digit (and they can’t be any of the <a href="keyword.md">keywords</a> reserved by C). By convention they also shouldn’t be all uppercase or start with uppercase (these are normally used for 
other things). Normally we name variables like this: <code class="language-plaintext highlighter-rouge">myVariable</code> or <code class="language-plaintext highlighter-rouge">my_variable</code> (pick one style, don’t mix them).</p>

<p>In C as in other languages each variable has a certain <strong><a href="data_type.md">data type</a></strong>; that is each variable has associated an information of what kind of data is stored in it. This can be e.g. a <em>whole number</em>, 
<em>fraction</em>, a <em>text character</em>, <em>text string</em> etc. Data types are a more complex topic that will be discussed later, for now we’ll start with the most basic one, the <strong>integer type</strong>, in C called <code class="language-plaintext highlighter-rouge">int</code>. An <code class="language-plaintext highlighter-rouge">int</code> 
variable can store whole numbers in the range of at least -32768 to 32767 (but usually much more).</p>

<p>Let’s see an example.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;

int main(void)
{
  int myVariable;
  
  myVariable = 5;
  
  printf("%d\n",myVariable);
  
  myVariable = 8;
  
  printf("%d\n",myVariable);
}
</code></pre></div></div>

<ul>
  <li><code class="language-plaintext highlighter-rouge">int myVariable;</code> is so called <strong>variable declaration</strong>, it tells the compiler we are creating a new variable with the name <code class="language-plaintext highlighter-rouge">myVariable</code> and data type <code class="language-plaintext highlighter-rouge">int</code>. Variables can be created almost anywhere in the code 
(even outside the <code class="language-plaintext highlighter-rouge">main</code> function) but that’s a topic for later.</li>
  <li><code class="language-plaintext highlighter-rouge">myVariable = 5;</code> is so called <strong>variable assignment</strong>, it stores a value 5 into variable named <code class="language-plaintext highlighter-rouge">myVariable</code>. IMPORTANT NOTE: the <code class="language-plaintext highlighter-rouge">=</code> does NOT signify mathematical equality but an assignment (equality in C is 
written as <code class="language-plaintext highlighter-rouge">==</code>); when compiler encounters <code class="language-plaintext highlighter-rouge">=</code>, it simply takes the value on the right of it and writes it to the variable on the left side of it. Sometimes people confuse assignment with an equation that the 
compiler solves – this is NOT the case, assignment is much more simple, it simply writes a value into variable. So <code class="language-plaintext highlighter-rouge">x = x + 1;</code> is a valid command even though mathematically it would be an equation without a 
solution.</li>
  <li><code class="language-plaintext highlighter-rouge">printf("%d\n",myVariable);</code> prints out the value currently stored in <code class="language-plaintext highlighter-rouge">myVariable</code>. Don’t get scared by this complicated command, it will be explained later (once we learn about <a href="pointer.md">pointers</a>). For now 
only know this prints the variable content.</li>
  <li><code class="language-plaintext highlighter-rouge">myVariable = 8;</code> assigns a new value to <code class="language-plaintext highlighter-rouge">myVariable</code>, overwriting the old.</li>
  <li><code class="language-plaintext highlighter-rouge">printf("%d\n",myVariable);</code> again prints the value in <code class="language-plaintext highlighter-rouge">myVariable</code>.</li>
</ul>

<p>After compiling and running of the program you should see:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>5
8
</code></pre></div></div>

<p>Last thing to learn is <strong>arithmetic operators</strong>. They’re just normal math operators such as +, - and /. You can use these along with brackets (<code class="language-plaintext highlighter-rouge">(</code> and <code class="language-plaintext highlighter-rouge">)</code>) to create <strong><a href="expression.md">expressions</a></strong>. Expressions 
can contain variables and can themselves be used in many places where variables can be used (but not everywhere, e.g. on the left side of variable assignment, that would make no sense). E.g.:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;

int main(void)
{
  int heightCm = 175;
  int weightKg = 75;
  int bmi = (weightKg * 10000) / (heightCm * heightCm);

  printf("%d\n",bmi);
}
</code></pre></div></div>

<p>calculates and prints your BMI (body mass index).</p>

<p>Let’s quickly mention how you can read and write values in C so that you can begin to experiment with your own small programs. You don’t have to understand the following <a href="syntax.md">syntax</a> as of yet, it will be 
explained later, now simply copy-paste the commands:</p>

<ul>
  <li><code class="language-plaintext highlighter-rouge">puts("hello");</code>: Prints a text string with newline.</li>
  <li><code class="language-plaintext highlighter-rouge">printf("hello");</code>: Same as above but without newline.</li>
  <li><code class="language-plaintext highlighter-rouge">printf("%d\n",x);</code>: Prints the value of variable <code class="language-plaintext highlighter-rouge">x</code> with newline.</li>
  <li><code class="language-plaintext highlighter-rouge">printf("%d ");</code>: Same as above but without a newline.</li>
  <li><code class="language-plaintext highlighter-rouge">scanf("%d",&amp;x);</code>: Read a number from input to the variable <code class="language-plaintext highlighter-rouge">x</code>. Note there has to be <code class="language-plaintext highlighter-rouge">&amp;</code> in front of <code class="language-plaintext highlighter-rouge">x</code>.</li>
</ul>

<h2 id="branches-and-loops-if-while-for">Branches And Loops (If, While, For)</h2>

<p>When creating <a href="algorithm.md">algorithms</a>, it’s not enough to just write linear sequences of commands. Two things (called <a href="control_structure.md">control structures</a>) are very important to have in addition:</p>

<ul>
  <li><strong><a href="branch.md">branches</a></strong>: Conditionally executing or skipping certain commands (e.g. if a user enters password we want to either log him in if the password was correct or write error if the password was 
incorrect). This is informally known as <strong>“if-then-else”</strong>.</li>
  <li><strong><a href="loop.md">loops</a></strong> (also called <strong>iteration</strong>): Repeating certain commands given number of times or as long as some condition holds (e.g. when searching a text we repeatedly compare words one by one to the 
searched word until a match is found or end of text is reached).</li>
</ul>

<p>Let’s start with <strong>branches</strong>. In C the command for a branch is <code class="language-plaintext highlighter-rouge">if</code>. E.g.:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>if (x &gt; 10)
  puts("X is greater than 10.");
</code></pre></div></div>

<p>The <a href="syntax.md">syntax</a> is given, we start with <code class="language-plaintext highlighter-rouge">if</code>, then brackets (<code class="language-plaintext highlighter-rouge">(</code> and <code class="language-plaintext highlighter-rouge">)</code>) follow inside which there is a condition, then a command or a block of multiple commands (inside <code class="language-plaintext highlighter-rouge">{</code> and <code class="language-plaintext highlighter-rouge">}</code>) follow. If the 
condition in brackets holds, the command (or block of commands) gets executed, otherwise it is skipped.</p>

<p>Optionally there may be an <em>else</em> branch which is gets executed only if the condition does NOT hold. It is denoted with the <code class="language-plaintext highlighter-rouge">else</code> keyword which is again followed by a command or a block of multiple commands. 
Branching may also be nested, i.e. branches may be inside other branches. For example:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>if (x &gt; 10)
  puts("X is greater than 10.");
else
{
  puts("X is not greater than 10.");

  if (x &lt; 5)
    puts("And it is also smaller than 5.");
}
</code></pre></div></div>

<p>So if <code class="language-plaintext highlighter-rouge">x</code> is equal e.g. 3, the output will be:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>X is not greater than 10.
And it is also smaller than 5.
</code></pre></div></div>

<p>About <strong>conditions</strong> in C: a condition is just an expression (variables/functions along with arithmetic operators). The expression is evaluated (computed) and the number that is obtained is interpreted as <em>true</em> or 
<em>false</em> like this: <strong>in C 0 (zero) means false, 1 (and everything else) means true</strong>. Even comparison operators like <code class="language-plaintext highlighter-rouge">&lt;</code> and <code class="language-plaintext highlighter-rouge">&gt;</code> are technically arithmetic, they compare numbers and yield either 1 or 0. Some 
operators commonly used in conditions are:</p>

<ul>
  <li><code class="language-plaintext highlighter-rouge">==</code> (equals): yields 1 if the operands are equal, otherwise 0.</li>
  <li><code class="language-plaintext highlighter-rouge">!=</code> (not equal): yields 1 if the operands are NOT equal, otherwise 0.</li>
  <li><code class="language-plaintext highlighter-rouge">&lt;</code> (less than): yields 1 if the first operand is smaller than the second, otherwise 0.</li>
  <li><code class="language-plaintext highlighter-rouge">&lt;=</code>: yields 1 if the first operand is smaller or equal to the second, otherwise 0.</li>
  <li><code class="language-plaintext highlighter-rouge">&amp;&amp;</code> (logical <a href="and.md">AND</a>): yields 1 if both operands are non-0, otherwise 0.</li>
  <li><code class="language-plaintext highlighter-rouge">||</code> (logical <a href="or.md">OR</a>): yields 1 if at least one operand is non-0, otherwise 0.</li>
  <li><code class="language-plaintext highlighter-rouge">!</code> (logical <a href="not.md">NOT</a>): yields 1 if the operand is 0, otherwise 0.</li>
</ul>

<p>E.g. an if statement starting as <code class="language-plaintext highlighter-rouge">if (x == 5 || x == 10)</code> will be true if <code class="language-plaintext highlighter-rouge">x</code> is either 5 or 10.</p>

<p>Next we have <strong>loops</strong>. There are multiple kinds of loops even though in theory it is enough to only have one kind of loop (there are multiple types out of convenience). The loops in C are:</p>

<ul>
  <li><strong>while</strong>: Loop with condition at the beginning.</li>
  <li><strong>do while</strong>: Loop with condition at the end, not used so often so we’ll ignore this one.</li>
  <li><strong>for</strong>: Loop executed a fixed number of times. This is a very common case, that’s why there is a special loop for it.</li>
</ul>

<p>The <strong>while</strong> loop is used when we want to repeat something without knowing in advance how many times we’ll repeat it (e.g. searching a word in text). It starts with the <code class="language-plaintext highlighter-rouge">while</code> keyword, is followed by brackets 
with a condition inside (same as with branches) and finally a command or a block of commands to be looped. For instance:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>while (x &gt; y) // as long as x is greater than y
{
  printf("%d %d\n",x,y); // prints x and y  

  x = x - 1; // decrease x by 1
  y = y * 2; // double y
}

puts("The loop ended.");
</code></pre></div></div>

<p>If <code class="language-plaintext highlighter-rouge">x</code> and <code class="language-plaintext highlighter-rouge">y</code> were to be equal 100 and 20 (respectively) before the loop is encountered, the output would be:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>100 20
99 40
98 60
97 80
The loop ended.
</code></pre></div></div>

<p>The <strong>for</strong> loop is executed a fixed number of time, i.e. we use it when we know in advance how many time we want to repeat our commands. The <a href="syntax.md">syntax</a> is a bit more complicated: it starts with the 
keywords <code class="language-plaintext highlighter-rouge">for</code>, then brackets (<code class="language-plaintext highlighter-rouge">(</code> and <code class="language-plaintext highlighter-rouge">)</code>) follow and then the command or a block of commands to be looped. The inside of the brackets consists of an initialization, condition and action separated by semicolon (<code class="language-plaintext highlighter-rouge">;
</code>) – don’t worry, it is enough to just remember the structure. A for loop may look like this:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>puts("Counting until 5...");

for (int i = 0; i &lt; 5; ++i)
  printf("%d\n",i); // prints i
</code></pre></div></div>

<p><code class="language-plaintext highlighter-rouge">int i = 0</code> creates a new temporary variable named <code class="language-plaintext highlighter-rouge">i</code> (name normally used by convention) which is used as a <strong>counter</strong>, i.e. this variable starts at 0 and increases with each iteration (cycle), and it can be used 
inside the loop body (the repeated commands). <code class="language-plaintext highlighter-rouge">i &lt; 5</code> says the loop continues to repeat as long as <code class="language-plaintext highlighter-rouge">i</code> is smaller than 5 and <code class="language-plaintext highlighter-rouge">++i</code> says that <code class="language-plaintext highlighter-rouge">i</code> is to be increased by 1 after each iteration (<code class="language-plaintext highlighter-rouge">++i</code> is basically just 
a shorthand for <code class="language-plaintext highlighter-rouge">i = i + 1</code>). The above code outputs:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Counting until 5...
0
1
2
3
4
</code></pre></div></div>

<p>IMPORTANT NOTE: in programming we <strong>count from 0</strong>, not from 1 (this is convenient e.g. in regards to <a href="pointer.md">pointers</a>). So if we count to 5, we get 0, 1, 2, 3, 4. This is why <code class="language-plaintext highlighter-rouge">i</code> starts with value 0 and the end condition is <code class="language-plaintext highlighter-rouge">i &lt; 5</code> (not <code class="language-plaintext highlighter-rouge">i &lt;= 5</code>).</p>

<p>Generally if we want to repeat the <code class="language-plaintext highlighter-rouge">for</code> loop <em>N</em> times, the format is <code class="language-plaintext highlighter-rouge">for (int i = 0; i &lt; N; ++i)</code>.</p>

<p>Any loop can be exited at any time with a special command called <code class="language-plaintext highlighter-rouge">break</code>. This is often used with so called infinite loop, a <em>while</em> loop that has <code class="language-plaintext highlighter-rouge">1</code> as a condition; recall that 1 means true, i.e. the loop condition always holds and the loop never ends. <code class="language-plaintext highlighter-rouge">break</code> allows us to place conditions in the middle of the loop and into multiple places. E.g.:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>while (1) // infinite loop
{
  x = x - 1;
  
  if (x == 0)
    break; // this exits the loop!
    
  y = y / x;
}
</code></pre></div></div>

<p>The code above places a condition in the middle of an infinite loop to prevent division by zero in <code class="language-plaintext highlighter-rouge">y = y / x</code>.</p>

<p>Again, loops can be nested (we may have loops inside loops) and also loops can contain branches and vice versa.</p>

<h2 id="simple-game-guess-a-number">Simple Game: Guess A Number</h2>

<p>With what we’ve learned so far we can already make a simple <a href="game.md">game</a>: guess a number. The computer thinks a random number in range 0 to 9 and the user has to guess it. The source code is following.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;

int main(void)
{
  srand(clock()); // random seed
  
  while (1) // infinite loop
  {
    int randomNumber = rand() % 10;
      
    puts("I think a number. What is it?");
    
    int guess;
    
    scanf("%d",&amp;guess); // read the guess
    
    getchar();

    if (guess == randomNumber)
      puts("You guessed it!");
    else
      printf("Wrong. The number was %d.\n",randomNumber);
      
    puts("Play on? [y/n]");
    
    char answer;

    scanf("%c",&amp;answer); // read the answer
    
    if (answer == 'n')
      break;
  }

  puts("Bye.");
  
  return 0; // return success, always here
}
</code></pre></div></div>

<ul>
  <li><code class="language-plaintext highlighter-rouge">#include &lt;stdlib.h&gt;</code>, <code class="language-plaintext highlighter-rouge">#include &lt;time.h&gt;</code>: we’re including additional libraries because we need some specific functions from them (<code class="language-plaintext highlighter-rouge">rand</code>, <code class="language-plaintext highlighter-rouge">srand</code>, <code class="language-plaintext highlighter-rouge">clock</code>).</li>
  <li><code class="language-plaintext highlighter-rouge">srand(clock());</code>: don’t mind this line too much, its purpose is to <a href="">seed</a> a pseudorandom number generator. Without doing this the game would always generate the same sequence of random numbers when run again.</li>
  <li><code class="language-plaintext highlighter-rouge">while (1)</code> is an infinite game loop – it runs over and over, in each cycle we perform one game round. The loop can be exited with the <code class="language-plaintext highlighter-rouge">break</code> statement later on (if the user answers he doesn’t want to continue 
playing).</li>
  <li><code class="language-plaintext highlighter-rouge">int randomNumber = rand() % 10;</code>: this line declares a variable named <code class="language-plaintext highlighter-rouge">randomNumber</code> and immediately assigns a value to it. The value is a random number from 0 to 9. This is achieved with a function <code class="language-plaintext highlighter-rouge">rand</code> (from 
the above included <code class="language-plaintext highlighter-rouge">stdlib</code> library) which returns a random number, and with the modulo (remainder after division) arithmetic operator (<code class="language-plaintext highlighter-rouge">%</code>) which ensures the number is in the correct range (less than 10).</li>
  <li><code class="language-plaintext highlighter-rouge">int guess;</code> creates another variable in which we’ll store the user’s guessed number.</li>
  <li><code class="language-plaintext highlighter-rouge">scanf("%d",&amp;guess);</code> reads a number from the input to the variable named <code class="language-plaintext highlighter-rouge">guess</code>. Again, don’t be bothered by the complicated structure of this command, for now just accept that this is how it’s done.</li>
  <li><code class="language-plaintext highlighter-rouge">getchar();</code>: don’t mind this line, it just discards a newline character read from the input.</li>
  <li><code class="language-plaintext highlighter-rouge">if (guess == randomNumber) ...</code>: this is a branch which checks if the user guess is equal to the generated random number. If so, a success message is printed out. If not, a fail message is printed out along with 
the secret number. Note we use the <code class="language-plaintext highlighter-rouge">puts</code> function for the first message as it only prints a text sting, while for the latter message we have to use <code class="language-plaintext highlighter-rouge">printf</code>, a more complex function, because that requires 
inserting a number into the printed string. More on these functions later.</li>
  <li><code class="language-plaintext highlighter-rouge">char answer;</code> declares a variable to store user’s answer to a question of whether to play on. It is of <code class="language-plaintext highlighter-rouge">char</code> data type which can store a single text character.</li>
  <li><code class="language-plaintext highlighter-rouge">scanf("%c",&amp;answer);</code> reads a single character from input to the <code class="language-plaintext highlighter-rouge">answer</code> variable.</li>
  <li><code class="language-plaintext highlighter-rouge">if (answer == 'n') break;</code> is a branch that exits the infinite loop with <code class="language-plaintext highlighter-rouge">break</code> statement if the answer entered was <code class="language-plaintext highlighter-rouge">n</code> (<em>no</em>).</li>
</ul>

<h2 id="functions-subprograms">Functions (Subprograms)</h2>

<p>Functions are extremely important, no program besides the most primitive ones can be made without them (well, in theory any program can be created without functions, but in practice such programs would be extremely 
complicated and unreadable).</p>

<p><strong><a href="function.md">Function</a> is a subprogram</strong> (in other languages functions are also called procedures or subroutines), i.e. it is code that solves some smaller subproblem that you can repeatedly invoke, for instance 
you may have a function for computing a <a href="sqrt.md">square root</a>, for encrypting data or for playing a sound from speakers. We have already met functions such as <code class="language-plaintext highlighter-rouge">puts</code>, <code class="language-plaintext highlighter-rouge">printf</code> or <code class="language-plaintext highlighter-rouge">rand</code>.</p>

<p>Functions are similar to but <strong>NOT the same as mathematical functions</strong>. Mathematical function (simply put) takes a number as input and outputs another number computed from the input number, and this output number 
depends only on the input number and nothing else. C functions can do this too but they can also do additional things such as modify variables in other parts of the program or make the computer do something (such 
as play a sound or display something on the screen) – these are called <strong><a href="side_effect.md">side effects</a></strong>; things done besides computing an output number from an input number. For distinction mathematical 
functions are called <em>pure</em> functions and functions with side effects are called non-pure.</p>

<p><strong>Why are function so important?</strong> Firstly they help us divide a big problem into small subproblems and make the code better organized and readable, but mainly they help us respect the <a href="dry.md">DRY</a> (<em>Don’t Repeat 
Yourself</em>) principle – this is extremely important in programming. Imagine you need to solve a <a href="quadratic_equation.md">quadratic equation</a> in several parts of your program; you do NOT want to solve it in each 
place separately, you want to make a function that solves a quadratic equation and then only invoke (call) that function anywhere you need to solve your quadratic equation. This firstly saves space (source code 
will be shorter and compiled program will be smaller), but it also makes your program manageable and eliminates bugs – imagine you find a better (e.g. faster) way to solving quadratic equations; without functions 
you’d have to go through the whole code and change the algorithm in each place separately which is impractical and increases the chance of making errors. With functions you only change the code in one place (in the 
function) and in any place where your code invokes (calls) this function the new better and updated version of the function will be used.</p>

<p>Besides writing programs that can be directly executed programmers write <strong><a href="library.md">libraries</a></strong> – collections of functions that can be used in other projects. We have already seen libraries such as <em>stdio</em>, 
<em>standard input/output library</em>, a standard (official, bundled with every C compiler) library for input/output (reading and printing values); <em>stdio</em> contains functions such as <code class="language-plaintext highlighter-rouge">puts</code> which is used to printing out 
text strings. Examples of other libraries are the standard <em>math</em> library containing function for e.g. computing <a href="sine.md">sine</a>, or <a href="sdl.md">SDL</a>, a 3rd party multimedia library for such things as drawing to 
screen, playing sounds and handling keyboard and mouse input.</p>

<p>Let’s see a simple example of a function that writes out a temperature in degrees of Celsius as well as in Kelvin:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;

void writeTemperature(int celsius)
{
  int kelvin = celsius + 273;
  printf("%d C (%d K)\n",celsius,kelvin);
}

int main(void)
{
  writeTemperature(-50);
  writeTemperature(0);
  writeTemperature(100);

  return 0;
}
</code></pre></div></div>

<p>The output is</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>-50 C (223 K)
0 C (273 K)
100 C (373 K)
</code></pre></div></div>

<p>Now imagine we decide we also want our temperatures in Fahrenheit. We can simply edit the code in <code class="language-plaintext highlighter-rouge">writeTemperature</code> function and the program will automatically be writing temperatures in the new way.</p>

<p>Let’s see how to create and invoke functions. Creating a function in code is done between inclusion of libraries and the <code class="language-plaintext highlighter-rouge">main function</code>, and we formally call this <strong>defining a function</strong>. The function definition 
format is following:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>RETURN_TYPE FUNCTION_NAME(FUNCTION_PARAMETERS)
{
  FUNCTION_BODY
}
</code></pre></div></div>

<ul>
  <li><code class="language-plaintext highlighter-rouge">RETURN_TYPE</code> is the <a href="data_type.md">data type</a> the function returns. A function may or may not return a certain value, just as the pure mathematical function do. This may for example be <code class="language-plaintext highlighter-rouge">int</code>, if the function 
returns an integer number. If the function doesn’t return anything, this type is <code class="language-plaintext highlighter-rouge">void</code>.</li>
  <li><code class="language-plaintext highlighter-rouge">FUNCTION_NAME</code> is the name of the function, it follows the same rules as the names for variables.</li>
  <li><code class="language-plaintext highlighter-rouge">FUNCTION_PARAMETERS</code> specifies the input values of the function. The function can take any number of parameters (e.g. a function <code class="language-plaintext highlighter-rouge">playBeep</code> may take 0 arguments, <code class="language-plaintext highlighter-rouge">sine</code> function takes 1, <code class="language-plaintext highlighter-rouge">logarithm</code> may take two 
etc.). This list is comma-separated and each item consists of the parameter data type and name. If there are 0 parameters, there should be the word <code class="language-plaintext highlighter-rouge">void</code> inside the brackets, but compilers tolerate just having 
empty brackets.</li>
  <li><code class="language-plaintext highlighter-rouge">FUNCTION_BODY</code> are the commands executed by the function, just as we know them from the <em>main</em> function.</li>
</ul>

<p>Let’s see another function:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;

int power(int x, int n)
{
  int result = 1;
  
  for (int i = 0; i &lt; n; ++i) // repeat n times
    result = result * x;
    
  return result;
}

int main(void)
{
  for (int i = 0; i &lt; 5; ++i)
  {
    int powerOfTwo = power(2,i);
    printf("%d\n",powerOfTwo);
  }

  return 0;
}
</code></pre></div></div>

<p>The output is:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>2
4
8
16
</code></pre></div></div>

<p>The function power takes two parameters: <code class="language-plaintext highlighter-rouge">x</code> and <code class="language-plaintext highlighter-rouge">n</code>, and returns <code class="language-plaintext highlighter-rouge">x</code> raised to the <code class="language-plaintext highlighter-rouge">n</code>s power. Note that unlike the first function we saw here the return type is <code class="language-plaintext highlighter-rouge">int</code> because this function does return a value. 
<strong>Notice the command <code class="language-plaintext highlighter-rouge">return</code></strong> – it is a special command that causes the function to terminate and return a specific value. In functions that return a value (their return type is not <code class="language-plaintext highlighter-rouge">void</code>) there has to be a 
<code class="language-plaintext highlighter-rouge">return</code> command. In function that return nothing there may or may not be one, and if there is, it has no value after it (<code class="language-plaintext highlighter-rouge">return;</code>);</p>

<p>Let’s focus on how we invoke the function – in programming we say we <strong>call the function</strong>. The function call in our code is <code class="language-plaintext highlighter-rouge">power(2,i)</code>. If a function returns a value (return type is not <code class="language-plaintext highlighter-rouge">void</code>), its function 
call can be used in any expression, i.e. almost anywhere where we can use a variable or a numerical value – just imagine the function computes a return value and this value is <strong>substituted to the place where we 
call the function</strong>. For example we can imagine the expression <code class="language-plaintext highlighter-rouge">power(3,1) + power(3,0)</code> as simply <code class="language-plaintext highlighter-rouge">3 + 1</code>.</p>

<p>If a function returns nothing (return type is <code class="language-plaintext highlighter-rouge">void</code>), it can’t be used in expressions, it is used “by itself”; e.g. <code class="language-plaintext highlighter-rouge">playBeep();</code>. (Function that do return a value can also be used like this – their return value 
is in this case simply ignored.)</p>

<p>We call a function by writing its name (<code class="language-plaintext highlighter-rouge">power</code>), then adding brackets (<code class="language-plaintext highlighter-rouge">(</code> and <code class="language-plaintext highlighter-rouge">)</code>) and inside them we put <strong>arguments</strong> – specific values that will substitute the corresponding parameters inside the function 
(here <code class="language-plaintext highlighter-rouge">x</code> will take the value <code class="language-plaintext highlighter-rouge">2</code> and <code class="language-plaintext highlighter-rouge">n</code> will take the current value of <code class="language-plaintext highlighter-rouge">i</code>). If the function takes no parameters (the function parameter list is <code class="language-plaintext highlighter-rouge">void</code>), we simply put nothing inside the brackets (e.g. <code class="language-plaintext highlighter-rouge">playBeep
();</code>);</p>

<p>Here comes the nice thing: <strong>we can nest function calls</strong>. For example we can write <code class="language-plaintext highlighter-rouge">x = power(3,power(2,1));</code> which will result in assigning the variable <code class="language-plaintext highlighter-rouge">x</code> the value of 9. <strong>Functions can also call other 
functions</strong> (even themselves, see <a href="recursion.md">recursion</a>), but only those that have been defined before them in the source code (this can be fixed with so called <a href="forward_decl.md">forward declarations</a>).</p>

<p>Notice that the <code class="language-plaintext highlighter-rouge">main</code> function we always have in our programs is also a function definition. The definition of this function is required for runnable programs, its name has to be <code class="language-plaintext highlighter-rouge">main</code> and it has to return <code class="language-plaintext highlighter-rouge">int</code> 
(an error code where 0 means no error). It can also take parameters but more on that later.</p>

<p>These is the most basic knowledge to have about C functions. Let’s see one more example with some pecularities that aren’t so important now, but will be later.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;

void writeFactors(int x) // writes divisors of x
{
  printf("factors of %d:\n",x);
  
  while (x &gt; 1) // keep dividing x by its factors
  {
    for (int i = 2; i &lt;= x; ++i) // search for a factor
      if (x % i == 0) // i divides x without remainder?
      {
        printf("  %d\n",i); // i is a factor, write it
        x = x / i; // divide x by i
        break; // exit the for loop
      }
  }
}

int readNumber(void)
{
  int number;
  
  puts("Please enter a number to factor (0 to quit).");
  scanf("%d",&amp;number);
  
  return number;
}

int main(void)
{
  while (1) // infinite loop
  {
    int number = readNumber(); // &lt;- function call

    if (number == 0) // 0 means quit
      break;
      
    writeFactors(number); // &lt;- function call
  }
    
  return 0;
}
</code></pre></div></div>

<p>We have defined two functions: <code class="language-plaintext highlighter-rouge">writeFactors</code> and <code class="language-plaintext highlighter-rouge">readNumber</code>. <code class="language-plaintext highlighter-rouge">writeFactors</code> return no values but it has side effects (print text to the command line). <code class="language-plaintext highlighter-rouge">readNumber</code> takes no parameters but return a value; it 
prompts the user to enter a value and returns the read value.</p>

<p>Notice that inside <code class="language-plaintext highlighter-rouge">writeFactors</code> we modify its parameter <code class="language-plaintext highlighter-rouge">x</code> inside the function body – this is okay, it won’t affect the argument that was passed to this function (the <code class="language-plaintext highlighter-rouge">number</code> variable inside the <code class="language-plaintext highlighter-rouge">main</code> 
function won’t change after this function call). <code class="language-plaintext highlighter-rouge">x</code> can be seen as a <strong><a href="local_variable.md">local variable</a></strong> of the function, i.e. a variable that’s created inside this function and can only be used inside it – 
when <code class="language-plaintext highlighter-rouge">writeFactors</code> is called inside <code class="language-plaintext highlighter-rouge">main</code>, a new local variable <code class="language-plaintext highlighter-rouge">x</code> is created inside <code class="language-plaintext highlighter-rouge">writeFactors</code> and the value of <code class="language-plaintext highlighter-rouge">number</code> is copied to it.</p>

<p>Another local variable is <code class="language-plaintext highlighter-rouge">number</code> – it is a local variable both in <code class="language-plaintext highlighter-rouge">main</code> and in <code class="language-plaintext highlighter-rouge">readNumber</code>. Even though the names are the same, these are two different variables, each one is local to its respective function 
(modifying <code class="language-plaintext highlighter-rouge">number</code> inside <code class="language-plaintext highlighter-rouge">readNumber</code> won’t affect <code class="language-plaintext highlighter-rouge">number</code> inside <code class="language-plaintext highlighter-rouge">main</code> and vice versa).</p>

<p>And a last thing: keep in mind that not every command you write in C program is a function call. E.g. control structures (<code class="language-plaintext highlighter-rouge">if</code>, <code class="language-plaintext highlighter-rouge">while</code>, …) and special commands (<code class="language-plaintext highlighter-rouge">return</code>, <code class="language-plaintext highlighter-rouge">break</code>, …) are not function calls.</p>

<h2 id="more-details-globals-switch-float-forward-decls-program-arguments-">More Details (Globals, Switch, Float, Forward Decls, Program Arguments, …)</h2>

<p>We’ve skipped a lot of details and small tricks for simplicity. Let’s go over some of them. Many of the following things are so called <a href="sugar.md">syntactic sugar</a>: convenient syntax shorthands for common operations.</p>

<p>Multiple variables can be defined and assigned like this:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>int x = 1, y = 2, z;
</code></pre></div></div>

<p>The meaning should be clear, but let’s mention that <code class="language-plaintext highlighter-rouge">z</code> doesn’t generally have a defined value here – it will have a value but you don’t know what it is (this may differ between different computers and platforms). 
See <a href="undefined_behavior.md">undefined behavior</a>.</p>

<p>The following is a shorthand for using operators:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>x += 1;      // same as: x = x + 1;
x -= 10;     // same as: x = x - 1;
x *= x + 1;  // same as: x = x * (x + 1);
x++;         // same as: x = x + 1;
x--;         // same as: x = x - 1;
// etc.
</code></pre></div></div>

<p>The last two constructs are called <strong><a href="increment.md">incrementing</a></strong> and <strong><a href="decrement.md">decrementing</a></strong>. This just means adding/subtracting 1.</p>

<p>In C there is a pretty unique operator called the <strong><a href="ternary_operator.md">ternary operator</a></strong> (ternary for having three <a href="operand.md">operands</a>). It can be used in expressions just as any other operators such as <code class="language-plaintext highlighter-rouge">+</code> 
or <code class="language-plaintext highlighter-rouge">-</code>. Its format is:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>CONDITION ? VALUE1 : VALUE2
</code></pre></div></div>

<p>It evaluates the <code class="language-plaintext highlighter-rouge">CONDITION</code> and if it’s true (non-0), this whole expression will have the value of <code class="language-plaintext highlighter-rouge">VALUE1</code>, otherwise its value will be <code class="language-plaintext highlighter-rouge">VALUE2</code>. It allows for not using so many <code class="language-plaintext highlighter-rouge">if</code>s. For example instead of</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>if (x &gt;= 10)
  x -= 10;
else
  x = 10;
</code></pre></div></div>

<p>we can write</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>x = x &gt;= 10 ? x - 10 : 10;
</code></pre></div></div>

<p><strong><a href="global_variable.md">Global variables</a></strong>: we can create variables even outside function bodies. Recall than variables inside functions are called <em>local</em>; variables outside functions are called <em>global</em> – they 
can basically be accessed from anywhere and can sometimes be useful. For example:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt; // for rand()

int money = 0; // total money, global variable

void printMoney(void)
{
  printf("I currently have $%d.\n",money);
}

void playLottery(void)
{
  puts("I'm playing lottery.");
  
  money -= 10; // price of lottery ticket
    
  if (rand() % 5) // 1 in 5 chance
  {
    money += 100;
    puts("I've won!");
  }
  else
    puts("I've lost!");

  printMoney();
}

void work(void)
{
  puts("I'm going to work :(");
  
  money += 200; // salary

  printMoney();
}

int main()
{
  work();
  playLottery();
  work();
  playLottery();
  
  return 0;
}
</code></pre></div></div>

<p>In C programs you may encounter a <strong>switch</strong> statement – it is a control structure similar to a branch <code class="language-plaintext highlighter-rouge">if</code> which can have more than two branches. It looks like this:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>  switch (x)
  {
    case 0: puts("X is zero. Don't divide by it."); break;
    case 69: puts("X is 69, haha."); break;
    case 42: puts("X is 42, the answer to everything."); break;
    default: printf("I don't know anything about X."); break;
  }
</code></pre></div></div>

<p>Switch can only compare exact values, it can’t e.g. check if a value is greater than something. Each branch starts with the keyword <code class="language-plaintext highlighter-rouge">case</code>, then the match value follows, then there is a colon (<code class="language-plaintext highlighter-rouge">:</code>) and the branch 
commands follow. IMPORTANT: there has to be the <code class="language-plaintext highlighter-rouge">break;</code> statement at the end of each case branch (we won’t go into details). A special branch is the one starting with the word <code class="language-plaintext highlighter-rouge">default</code> that is executed if no case 
label was matched.</p>

<p>Let’s also mention some additional data types we can use in programs:</p>

<ul>
  <li><code class="language-plaintext highlighter-rouge">char</code>: A single text character such as <em>‘a’</em>, <em>‘G’</em> or <em>‘_‘</em>. We can assign characters as <code class="language-plaintext highlighter-rouge">char c = 'a';</code> (single characters are enclosed in apostrophes similarly to how text strings are inside quotes). We can 
read a character as <code class="language-plaintext highlighter-rouge">c = getchar();</code> and print it as <code class="language-plaintext highlighter-rouge">putchar(c);</code>. Special characters that can be used are <code class="language-plaintext highlighter-rouge">\n</code> (newline) or <code class="language-plaintext highlighter-rouge">\t</code> (tab). Characters are in fact small numbers (usually with 256 possible values) and 
can be used basically anywhere a number can be used (for example we can compare characters, e.g. <code class="language-plaintext highlighter-rouge">if (c &lt; 'b') ...</code>). Later we’ll see characters are basic building blocks of text strings.</li>
  <li><code class="language-plaintext highlighter-rouge">unsigned int</code>: Integer that can only take positive values or 0 (i.e. no negative values). It can store higher positive values than normal <code class="language-plaintext highlighter-rouge">int</code> (which is called a <em>signed int</em>).</li>
  <li><code class="language-plaintext highlighter-rouge">long</code>: Big integer, takes more memory but can store number in the range of at least a few billion.</li>
  <li><code class="language-plaintext highlighter-rouge">float</code> and <code class="language-plaintext highlighter-rouge">double</code>: <a href="float.md">Floating point</a> number (<code class="language-plaintext highlighter-rouge">double</code> is bigger and more precise than <code class="language-plaintext highlighter-rouge">float</code>) – an approximation of <a href="real_number.md">real numbers</a>, i.e. numbers with a fractional part such as 2.5 or 
0.0001. You can print these numbers as <code class="language-plaintext highlighter-rouge">printf("%lf\n",x);</code> and read them as <code class="language-plaintext highlighter-rouge">scanf("%f",&amp;x);</code>.</li>
</ul>

<p>Here is a short example with the new data types:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;

int main(void)
{
  char c;
  float f;
  
  puts("Enter character.");
  c = getchar(); // read character
  
  puts("Enter float.");
  scanf("%f",&amp;f);
  
  printf("Your character is :%c.\n",c);
  printf("Your float is %lf\n",f);
 
  float fSquared = f * f;
  int wholePart = f; // this can be done
  
  printf("It's square is %lf.\n",fSquared);
  printf("It's whole part is %d.\n",wholePart);
  
  return 0;
}
</code></pre></div></div>

<p>Notice mainly how we can assign a <code class="language-plaintext highlighter-rouge">float</code> value into the variable of <code class="language-plaintext highlighter-rouge">int</code> type (<code class="language-plaintext highlighter-rouge">int wholePart = f;</code>). This can be done even the other way around and with many other types. C can do automatic <strong>type conversions</strong> 
(<em><a href="cast.md">casting</a></em>), but of course, some information may be lost in this process (e.g. the fractional part).</p>

<p>In the section about functions we said a function can only call a function that has been defined before it in the source code – this is because the compiler read the file from start to finish and if you call a 
function that hasn’t been defined yet, it simply doesn’t know what to call. But sometimes we need to call a function that will be defined later, e.g. in cases where two functions call each other (function <em>A</em> calls 
function <em>B</em> in its code but function <em>B</em> also calls function <em>A</em>). For this there exist so called <strong><a href="forward_decl.md">forward declarations</a></strong> – a forward declaration is informing that a function of certain name 
(and with certain parameters etc.) will be defined later in the code. Forward declaration look the same as a function definition, but it doesn’t have a body (the part between <code class="language-plaintext highlighter-rouge">{</code> and <code class="language-plaintext highlighter-rouge">}</code>), instead it is terminated 
with a semicolon (<code class="language-plaintext highlighter-rouge">;</code>). Here is an example:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;

void printDecorated2(int x, int fancy); // forward declaration

void printDecorated1(int x, int fancy)
{
  putchar('~');
  
  if (fancy)
    printDecorated2(x,0); // would be error without f. decl. 
  else
    printf("%d",x);
  
  putchar('~');
}

void printDecorated2(int x, int fancy)
{
  putchar('&gt;');
  
  if (fancy)
    printDecorated1(x,0);
  else
    printf("%d",x);
  
  putchar('&lt;');
}

int main()
{
  printDecorated1(10,1);
  putchar('\n'); // newline
  printDecorated2(20,1);
}
</code></pre></div></div>

<p>which prints</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>~&gt;10&lt;~
&gt;~20~&lt;
</code></pre></div></div>

<p>The functions <code class="language-plaintext highlighter-rouge">printDecorated1</code> and <code class="language-plaintext highlighter-rouge">printDecorated2</code> call each other, so this is the case when we have to use a forward declaration of <code class="language-plaintext highlighter-rouge">printDecorated2</code>. Also note the condition <code class="language-plaintext highlighter-rouge">if (fancy)</code> which is the same 
thing as <code class="language-plaintext highlighter-rouge">if (fancy != 0)</code> (imagine <code class="language-plaintext highlighter-rouge">fancy</code> being 1 and 0 and about what the condition evaluates to in each case).</p>

<p>And one more important thing: our program as a whole can be passed parameters when it’s executed, which inside the program we can access as so called <strong>command line arguments</strong> (also known as <em>flags</em>, <em>switches</em> 
etc.). This is important especially under <a href="unix.md">Unix</a> operating systems where we run programs from command line and where programs often work in non-interactive ways and are composed into bigger programs 
(similarly to how we compose small C functions into one big program); command line arguments are similar to arguments we pass to functions, they can inform our program to behave in certain way, for example to open 
a certain config file at start, to run in fullscreen mode, to print help and so on. When we compile our programs with the gcc compiler, e.g. like <code class="language-plaintext highlighter-rouge">gcc -o myprogram myprogram.c</code>, all the text after <code class="language-plaintext highlighter-rouge">gcc</code> are in fact 
arguments telling gcc which program to compile, how to compile it, how to name the output and so on. To allow our program to receive these arguments we add two parameters to the <code class="language-plaintext highlighter-rouge">main</code> function, one called <code class="language-plaintext highlighter-rouge">argc</code> 
(argument count) of integer type, saying how many arguments we get, and another called <code class="language-plaintext highlighter-rouge">argv</code> (argument <a href="vector.md">vector</a>) of pointer to a pointer to char type (please don’t get scared), holding an array of 
strings (each argument will be of string type). Operating system will automatically fill these arguments in when our program is started. Here is a short example demonstrating this:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;

int main(int argc, char **argv)
{
  puts("You passed these arguments:");

  for (int i = 0; i &lt; argc; ++i)
    printf("- \"%s\"\n",argv[i]);

  return 0;
}
</code></pre></div></div>

<p>If you compile this program and run it e.g. like</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>./program hello these are "my arguments"
</code></pre></div></div>

<p>The program will output:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>You passed these arguments:
- "./program"
- "hello"
- "these"
- "are"
- "my arguments"
</code></pre></div></div>

<p>Things to notice here are following: we passed 4 arguments but got 5 – the first argument is the path of our program itself, i.e. we will always get at least this argument. Then we also see that our arguments are 
separated by spaces, but if we put them into double quotes (like the last one), it will become just one argument, keeping the spaces (but not the quotes). For now this knowledge will suffice, you will most 
definitely encounter command line arguments in real programs – now you know what they are.</p>

<h2 id="header-files-libraries-compilationbuilding">Header Files, Libraries, Compilation/Building</h2>

<p>So far we’ve only been writing programs into a single source code file (such as <code class="language-plaintext highlighter-rouge">program.c</code>). More complicated programs consist of multiple files and libraries – we’ll take a look at this now.</p>

<p>In C we normally deal with two types of source code files:</p>

<ul>
  <li>
    <p><em>.c files</em>: These files contain so called <strong><a href="implementation.md">implementation</a></strong> of algorithms, i.e. code that translates into actual program instructions. These files are what’s handed to the compiler.</p>
  </li>
  <li>
    <p><em>.h files</em>, or <strong><a href="header_file.md">header files</a></strong>: These files typically contain <strong>declarations</strong> such as constants and function headers (but not their bodies, i.e. implementations).</p>
  </li>
</ul>

<p>When we have multiple source code files, we typically have pairs of <em>.c</em> and <em>.h</em> files. E.g. if there is a library called <em>mathfunctions</em>, it will consist of files <em>mathfunctions.c</em> and <em>mathfunctions.h</em>. The <em>.h</em> 
file will contain the function headers (in the same manner as with forward declarations) and constants such as <a href="pi.md">pi</a>. The <em>.c</em> file will then contain the implementations of all the functions declared in the <em>.
h</em> file. But why do we do this?</p>

<p>Firstly <em>.h</em> files may serve as a nice documentation of the library for programmers: you can simply open the <em>.h</em> file and see all the functions the library offers without having to skim over thousands of lines of 
code. Secondly this is for how multiple source code files are compiled into a single executable program.</p>

<p>Suppose now we’re compiling a single file named <em>program.c</em> as we’ve been doing until now. The compilation consists of several steps:</p>

<ol>
  <li>The compiler reads the file <em>program.c</em> and makes sense of it.</li>
  <li>It then creates an intermediate file called <em>program.o</em>. This is called an <a href="object_file.md">object file</a> and is a binary compiled file which however cannot yet be run because it is not <em>linked</em> – in this code 
all memory addresses are relative and it doesn’t yet contain the code from external libraries (e.g. the code of <code class="language-plaintext highlighter-rouge">printf</code>).</li>
  <li>The compiler then runs a <strong><a href="linker.md">linker</a></strong> which takes the file <em>program.o</em> and the object files of libraries (such as the <em>stdio</em> library) and it puts them all together into the final executable file 
called <em>program</em>. This is called <strong>linking</strong>; the code from the libraries is copied to complete the code of our program and the memory addresses are settled to some specific values.</li>
</ol>

<p>So realize that when the compiler is compiling our program (<em>program.c</em>), which contains function such as <code class="language-plaintext highlighter-rouge">printf</code> from a separate library, it doesn’t have the code of these functions available – this code is not 
in our file. Recall that if we want to call a function, it must have been defined before and so in order for us to be able to call <code class="language-plaintext highlighter-rouge">printf</code>, the compiler must know about it. This is why we include the <em>stdio</em> 
library at the top of our source code with <code class="language-plaintext highlighter-rouge">#include &lt;stdio.h&gt;</code> – this basically copy-pastes the content of the header file of the <em>stdio</em> library to the top of our source code file. In this header there are 
forward declarations of functions such as <code class="language-plaintext highlighter-rouge">printf</code>, so the compiler now knows about them (it knows their name, what they return and what parameters they take) and we can call them.</p>

<p>Let’s see a small example. We’ll have the following files (all in the same directory).</p>

<p><em>library.h</em> (the header file):</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>// Returns the square of n.
int square(int n);
</code></pre></div></div>

<p><em>library.c</em> (the implementation file):</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>int square(int x)
{
  // function implementation
  return x * x;
}
</code></pre></div></div>

<p><em>program.c</em> (main program):</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;
#include "library.h"

int main(void)
{
  int n = square(5);

  printf("%d\n",n);

  return 0;
}
</code></pre></div></div>

<p>NOTE: <code class="language-plaintext highlighter-rouge">"library.h"</code> here is between double quotes, unlike <code class="language-plaintext highlighter-rouge">&lt;stdio.h&gt;</code>. This just says we specify an absolute path to the file as it’s not in the directory where installed libraries go.</p>

<p>Now we will manually compile the library and the final program. First let’s compile the library, in command line run:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>gcc -c -o library.o library.c
</code></pre></div></div>

<p>The <code class="language-plaintext highlighter-rouge">-c</code> flag tells the compiler to only compile the file, i.e. only generate the object (<em>.o</em>) file without trying to link it. After this command a file <em>library.o</em> should appear. Next we compile the main program 
in the same way:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>gcc -c -o program.o program.c
</code></pre></div></div>

<p>This will generate the file <em>program.o</em>. Note that during this process the compiler is working only with the <em>program.c</em> file, it doesn’t know the code of the function <code class="language-plaintext highlighter-rouge">square</code>, but it knows this function exists, 
what it returns and what parameter it has thanks to us including the library header <em>library.h</em> with <code class="language-plaintext highlighter-rouge">#include "library.h"</code> (quotes are used instead of <code class="language-plaintext highlighter-rouge">&lt;</code> and <code class="language-plaintext highlighter-rouge">&gt;</code> to tell the compiler to look for the files in the 
current directory).</p>

<p>Now we have the file <em>program.o</em> in which the compiled <code class="language-plaintext highlighter-rouge">main</code> function resides and file <em>library.o</em> in which the compiled function <code class="language-plaintext highlighter-rouge">square</code> resides. We need to link them together. This is done like this:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>gcc -o program program.o library.o
</code></pre></div></div>

<p>For linking we don’t need to use any special flag, the compiler knows that if we give it several <em>.o</em> files, it is supposed to link them. The file <em>program</em> should appear that we can already run and it should print</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>25
</code></pre></div></div>

<p>This is the principle of compiling multiple C files (and it also allows for combining C with other languages). This process is normally automated, but you should know how it works. The systems that automate this 
action are called <strong><a href="build_system.md">build systems</a></strong>, they are for example <a href="make.md">Make</a> and <a href="cmake.md">Cmake</a>. When using e.g. the Make system, the whole codebase can be built with a single command <code class="language-plaintext highlighter-rouge">make</code> in 
the command line.</p>

<p>Some programmers simplify this whole process further so that they don’t even need a build system, e.g. with so called <a href="header_only.md">header-only libraries</a>, but this is outside the scope of this tutorial.</p>

<p>As a bonus, let’s see a few useful compiler flags:</p>

<ul>
  <li><code class="language-plaintext highlighter-rouge">-O1</code>, <code class="language-plaintext highlighter-rouge">-O2</code>, <code class="language-plaintext highlighter-rouge">-O3</code>: Optimize for speed (higher number means better optimization). Adding <code class="language-plaintext highlighter-rouge">-O3</code> normally instantly speeds up your program. This is recommended.</li>
  <li><code class="language-plaintext highlighter-rouge">-Os</code>: Optimize for size, the same as above but the compiler will try to make as small executable as possible.</li>
  <li><code class="language-plaintext highlighter-rouge">-Wall -Wextra -pedantic</code>: The compiler will write more warnings and will be more strict. This can help spot many bugs.</li>
  <li><code class="language-plaintext highlighter-rouge">-c</code>: Compile only (generate object files, do not link).</li>
  <li><code class="language-plaintext highlighter-rouge">-g</code>: Include debug symbols, this will be important for <a href="debugging.md">debugging</a>.</li>
</ul>

<h2 id="advanced-data-types-and-variables-structs-arrays-strings">Advanced Data Types And Variables (Structs, Arrays, Strings)</h2>

<p>Until now we’ve encountered simple data types such as <code class="language-plaintext highlighter-rouge">int</code>, <code class="language-plaintext highlighter-rouge">char</code> or <code class="language-plaintext highlighter-rouge">float</code>. These identify values which can take single atomic values (e.g. numbers or text characters). Such data types are called <strong><a href="primitive_type.md">primitive 
types</a></strong>.</p>

<p>Above these there exist <strong><a href="compound_type.md">compound data types</a></strong> (also <em>complex</em> or <em>structured</em>) which are composed of multiple primitive types. They are necessary for any advanced program.</p>

<p>The first compound type is a structure, or <strong><a href="struct.md">struct</a></strong>. It is a collection of several values of potentially different data types (primitive or compound). The following code shows how a struc can be 
created and used.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;

typedef struct
{
  char initial; // initial of name
  int weightKg;
  int heightCm;
} Human;

int bmi(Human human)
{
  return (human.weightKg * 10000) / (human.heightCm * human.heightCm);
}

int main(void)
{
  Human carl;
  
  carl.initial = 'C';
  carl.weightKg = 100;
  carl.heightCm = 180;
  
  if (bmi(carl) &gt; 25)
    puts("Carl is fat.");
    
  return 0;
}
</code></pre></div></div>

<p>The part of the code starting with <code class="language-plaintext highlighter-rouge">typedef struct</code> creates a new data type that we call <code class="language-plaintext highlighter-rouge">Human</code> (one convention for data type names is to start them with an uppercase character). This data type is a structure 
consisting of three members, one of type <code class="language-plaintext highlighter-rouge">char</code> and two of type <code class="language-plaintext highlighter-rouge">int</code>. Inside the <code class="language-plaintext highlighter-rouge">main</code> function we create a variable <code class="language-plaintext highlighter-rouge">carl</code> which is of <code class="language-plaintext highlighter-rouge">Human</code> data type. Then we set the specific values – we see that each 
member of the struct can be accessed using the dot character (<code class="language-plaintext highlighter-rouge">.</code>), e.g. <code class="language-plaintext highlighter-rouge">carl.weightKg</code>; this can be used just as any other variable. Then we see the type <code class="language-plaintext highlighter-rouge">Human</code> being used in the parameter list of the function 
<code class="language-plaintext highlighter-rouge">bmi</code>, just as any other type would be used.</p>

<p>What is this good for? Why don’t we just create global variables such as <code class="language-plaintext highlighter-rouge">carl_initial</code>, <code class="language-plaintext highlighter-rouge">carl_weightKg</code> and <code class="language-plaintext highlighter-rouge">carl_heightCm</code>? In this simple case it might work just as well, but in a more complex code this would be 
burdening – imagine we wanted to create 10 variables of type <code class="language-plaintext highlighter-rouge">Human</code> (<code class="language-plaintext highlighter-rouge">john</code>, <code class="language-plaintext highlighter-rouge">becky</code>, <code class="language-plaintext highlighter-rouge">arnold</code>, …). We would have to painstakingly create 30 variables (3 for each person), the function <code class="language-plaintext highlighter-rouge">bmi</code> would have to take 
two parameters (<code class="language-plaintext highlighter-rouge">height</code> and <code class="language-plaintext highlighter-rouge">weight</code>) instead of one (<code class="language-plaintext highlighter-rouge">human</code>) and if we wanted to e.g. add more information about every human (such as <code class="language-plaintext highlighter-rouge">hairLength</code>), we would have to manually create another 10 variables and add 
one parameter to the function <code class="language-plaintext highlighter-rouge">bmi</code>, while with a struct we only add one member to the struct definition and create more variables of type <code class="language-plaintext highlighter-rouge">Human</code>.</p>

<p><strong>Structs can be nested</strong>. So you may see things such as <code class="language-plaintext highlighter-rouge">myHouse.groundFloor.livingRoom.ceilingHeight</code> in C code.</p>

<p>Another extremely important compound type is <strong><a href="array.md">array</a></strong> – a sequence of items, all of which are of the same data type. Each array is specified with its length (number of items) and the data type of the 
items. We can have, for instance, an array of 10 <code class="language-plaintext highlighter-rouge">int</code>s, or an array of 235 <code class="language-plaintext highlighter-rouge">Human</code>s. The important thing is that we can <strong>index</strong> the array, i.e. we access the individual items of the array by their position, and 
this position can be specified with a variable. This allows for <strong>looping over array items</strong> and performing certain operations on each item. Demonstration code follows:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;
#include &lt;math.h&gt; // for sqrt()

int main(void)
{
  float vector[5];
  
  vector[0] = 1;
  vector[1] = 2.5;
  vector[2] = 0;
  vector[3] = 1.1;
  vector[4] = -405.054; 
  
  puts("The vector is:");
  
  for (int i = 0; i &lt; 5; ++i)
    printf("%lf ",vector[i]);
  
  putchar('\n'); // newline
  
  /* compute vector length with
     pythagoren theorem: */
  
  float sum = 0;
  
  for (int i = 0; i &lt; 5; ++i)
    sum += vector[i] * vector[i];
  
  printf("Vector length is: %lf\n",sqrt(sum));
  
  return 0;
}
</code></pre></div></div>

<p>We’ve included a new library called <code class="language-plaintext highlighter-rouge">math.h</code> so that we can use a function for square root (<code class="language-plaintext highlighter-rouge">sqrt</code>). (If you have trouble compiling the code, add <code class="language-plaintext highlighter-rouge">-lm</code> flag to the compile command.)</p>

<p><code class="language-plaintext highlighter-rouge">float vector[5];</code> is a declaration of an array of length 5 whose items are of type <code class="language-plaintext highlighter-rouge">float</code>. When compiler sees this, it creates a continuous area in memory long enough to store 5 numbers of <code class="language-plaintext highlighter-rouge">float</code> type, the 
numbers will reside here one after another.</p>

<p>After doing this, we can <strong>index</strong> the array with square brackets (<code class="language-plaintext highlighter-rouge">[</code> and <code class="language-plaintext highlighter-rouge">]</code>) like this: <code class="language-plaintext highlighter-rouge">ARRAY_NAME[INDEX]</code> where <code class="language-plaintext highlighter-rouge">ARRAY_NAME</code> is the name of the array (here <code class="language-plaintext highlighter-rouge">vector</code>) and <code class="language-plaintext highlighter-rouge">INDEX</code> is an expression that evaluates 
to integer, <strong>starting with 0</strong> and going up to the vector length minus one (remember that <strong>programmers count from zero</strong>). So the first item of the array is at index 0, the second at index 1 etc. The index can be 
a numeric constant like <code class="language-plaintext highlighter-rouge">3</code>, but also a variable or a whole expression such as <code class="language-plaintext highlighter-rouge">x + 3 * myFunction()</code>. Indexed array can be used just like any other variable, you can assign to it, you can use it in expressions 
etc. This is seen in the example. Trying to access an item beyond the array’s bounds (e.g. <code class="language-plaintext highlighter-rouge">vector[100]</code>) will likely crash your program.</p>

<p>Especially important are the parts of code staring with <code class="language-plaintext highlighter-rouge">for (int i = 0; i &lt; 5; ++i)</code>: this is an iteration over the array. It’s a very common pattern that we use whenever we need to perform some action with every 
item of the array.</p>

<p>Arrays can also be multidimensional, but we won’t bothered with that right now.</p>

<p>Why are arrays so important? They allow us to work with great number of data, not just a handful of numeric variables. We can create an array of million structs and easily work with all of them thanks to indexing 
and loops, this would be practically impossible without arrays. Imagine e.g. a game of <a href="chess.md">chess</a>; it would be very silly to have 64 plain variables for each square of the board (<code class="language-plaintext highlighter-rouge">squareA1</code>, <code class="language-plaintext highlighter-rouge">squareA2</code>, …, 
<code class="language-plaintext highlighter-rouge">squareH8</code>), it would be extremely difficult to work with such code. With an array we can represent the square as a single array, we can iterate over all the squares easily etc.</p>

<p>One more thing to mention about arrays is how they can be passed to functions. A function can have as a parameter an array of fixed or unknown length. There is also one exception with arrays as opposed to other 
types: <strong>if a function has an array as parameter and the function modifies this array, the array passed to the function (the argument) will be modified as well</strong> (we say that arrays are <em>passed by reference</em> while 
other types are <em>passed by value</em>). We know this wasn’t the case with other parameters such as <code class="language-plaintext highlighter-rouge">int</code> – for these the function makes a local copy that doesn’t affect the argument passed to the function. The 
following example shows what’s been said:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;

// prints an int array of lengt 10
void printArray10(int array[10])
{
  for (int i = 0; i &lt; 10; ++i)
    printf("%d ",array[i]);
}

// prints an int array of arbitrary lengt
void printArrayN(int array[], int n)
{
  for (int i = 0; i &lt; n; ++i)
    printf("%d ",array[i]);
}

// fills an array with numbers 0, 1, 2, ...
void fillArrayN(int array[], int n)
{
  for (int i = 0; i &lt; n; ++i)
    array[i] = i;
}

int main(void)
{
  int array10[10];
  int array20[20];
  
  fillArrayN(array10,10);
  fillArrayN(array20,20);
    
  printArray10(array10);
  putchar('\n');
  printArrayN(array20,20);
    
  return 0;
}
</code></pre></div></div>

<p>The function <code class="language-plaintext highlighter-rouge">printArray10</code> has a fixed length array as a parameter (<code class="language-plaintext highlighter-rouge">int array[10]</code>) while <code class="language-plaintext highlighter-rouge">printArrayN</code> takes as a parameter an array of unknown length (<code class="language-plaintext highlighter-rouge">int array[]</code>) plus one additional parameter to specify 
this length (so that the function knows how many items of the array it should print). The function <code class="language-plaintext highlighter-rouge">printArray10</code> is important because it shows how a function can modify an array: when we call <code class="language-plaintext highlighter-rouge">fillArrayN(array10,
10);</code> in the <code class="language-plaintext highlighter-rouge">main</code> function, the array <code class="language-plaintext highlighter-rouge">array10</code> will be actually modified after when the function finishes (it will be filled with numbers 0, 1, 2, …). This can’t be done with other data types (though there is 
a trick involving <a href="pointer.md">pointers</a> which we will learn later).</p>

<p>Now let’s finally talk about <strong>text <a href="string.md">strings</a></strong>. We’ve already seen strings (such as <code class="language-plaintext highlighter-rouge">"hello"</code>), we know we can print them, but what are they really? A string is a data type, and from C’s point of view 
strings are nothing but <strong>arrays of <code class="language-plaintext highlighter-rouge">char</code>s</strong> (text characters), i.e. sequences of <code class="language-plaintext highlighter-rouge">char</code>s in memory. <strong>In C every string has to end with a 0 <code class="language-plaintext highlighter-rouge">char</code></strong> – this is NOT <code class="language-plaintext highlighter-rouge">'0'</code> (whose <a href="ascii.md">ASCII</a> value is 48) but 
the direct value 0 (remember that <code class="language-plaintext highlighter-rouge">char</code>s are really just numbers). The 0 <code class="language-plaintext highlighter-rouge">char</code> cannot be printed out, it is just a helper value to terminate strings. So to store a string <code class="language-plaintext highlighter-rouge">"hello"</code> in memory we need an array of 
length at least 6 – one for each character plus one for the terminating 0. These types of string are called <strong>zero terminated strings</strong> (or <em>C strings</em>).</p>

<p>When we write a string such as <code class="language-plaintext highlighter-rouge">"hello"</code> in our source, the C compiler creates an array in memory for us and fills it with characters <code class="language-plaintext highlighter-rouge">'h'</code>, <code class="language-plaintext highlighter-rouge">'e'</code>, <code class="language-plaintext highlighter-rouge">'l'</code>, <code class="language-plaintext highlighter-rouge">'l'</code>, <code class="language-plaintext highlighter-rouge">'o'</code>, 0. In memory this may look like a sequence of 
numbers 104, 101, 108, 108 111, 0.</p>

<p>Why do we terminate strings with 0? Because functions that work with strings (such as <code class="language-plaintext highlighter-rouge">puts</code> or <code class="language-plaintext highlighter-rouge">printf</code>) don’t know what length the string is. We can call <code class="language-plaintext highlighter-rouge">puts("abc");</code> or <code class="language-plaintext highlighter-rouge">puts("abcdefghijk");</code> – the string 
passed to <code class="language-plaintext highlighter-rouge">puts</code> has different length in each case, and the function doesn’t know this length. But thanks to these strings ending with 0, the function can compute the length, simply by counting characters from the 
beginning until it finds 0 (or more efficiently it simply prints characters until it finds 0).</p>

<p>The <a href="syntax.md">syntax</a> that allows us to create strings with double quotes (<code class="language-plaintext highlighter-rouge">"</code>) is just a helper (<em>syntactic sugar</em>); we can create strings just as any other array, and we can work with them the same. Let’s see 
an example:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;

int main(void)
{
  char alphabet[27]; // 26 places for letters + 1 for temrinating 0
  
  for (int i = 0; i &lt; 26; ++i)
    alphabet[i] = 'A' + i;
  
  alphabet[26] = 0; // terminate the string
  
  puts(alphabet);
  
  return 0;
}
</code></pre></div></div>

<p><code class="language-plaintext highlighter-rouge">alphabet</code> is an array of <code class="language-plaintext highlighter-rouge">char</code>s, i.e. a string. Its length is 27 because we need 26 places for letters and one extra space for the terminating 0. Here it’s important to remind ourselves that we count from 0, so 
the alphabet can be indexed from 0 to 26, i.e. 26 is the last index we can use, doing <code class="language-plaintext highlighter-rouge">alphabet[27]</code> would be an error! Next we fill the array with letters (see how we can treat <code class="language-plaintext highlighter-rouge">char</code>s as numbers and do <code class="language-plaintext highlighter-rouge">'A' + 
i</code>). We iterate while <code class="language-plaintext highlighter-rouge">i &lt; 26</code>, i.e. we will fill all the places in the array up to the index 25 (including) and leave the last place (with index 26) empty for the terminating 0. That we subsequently assign. And 
finally we print the string with <code class="language-plaintext highlighter-rouge">puts(alphabet)</code> – here note that there are no double quotes around <code class="language-plaintext highlighter-rouge">alphabet</code> because its a variable name. Doing <code class="language-plaintext highlighter-rouge">puts("alphabet")</code> would cause the program to literally print out 
<code class="language-plaintext highlighter-rouge">alphabet</code>. Now the program outputs:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>ABCDEFGHIJKLMNOPQRSTUVWXYZ
</code></pre></div></div>

<p>In C there is a standard library for working with strings called <em>string</em> (<code class="language-plaintext highlighter-rouge">#include &lt;string.h&gt;</code>), it contains such function as <code class="language-plaintext highlighter-rouge">strlen</code> for computing string length or <code class="language-plaintext highlighter-rouge">strcmp</code> for comparing strings.</p>

<p>One final example – a creature generator – will show all the three new data types in action:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt; // for rand()

typedef struct
{
  char name[4]; // 3 letter name + 1 place for 0
  int weightKg;
  int legCount;
} Creature; // some weird creature

Creature creatures[100]; // global array of Creatures

void printCreature(Creature c)
{
  printf("Creature named %s ",c.name); // %s prints a string
  printf("(%d kg, ",c.weightKg);
  printf("%d legs)\n",c.legCount);
}

int main(void)
{
  // generate random creatures:
  
  for (int i = 0; i &lt; 100; ++i)
  {
    Creature c;
    
    c.name[0] = 'A' + (rand() % 26);
    c.name[1] = 'a' + (rand() % 26);
    c.name[2] = 'a' + (rand() % 26);
    c.name[3] = 0; // terminate the string

    c.weightKg = 1 + (rand() % 1000); 
    c.legCount = 1 + (rand() % 10); // 1 to 10 legs

    creatures[i] = c;
  }
    
  // print the creatures:
  
  for (int i = 0; i &lt; 100; ++i)
    printCreature(creatures[i]);
  
  return 0;
}
</code></pre></div></div>

<p>When run you will see a list of 100 randomly generated creatures which may start e.g. as:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Creature named Nwl (916 kg, 4 legs)
Creature named Bmq (650 kg, 2 legs)
Creature named Cda (60 kg, 4 legs)
Creature named Owk (173 kg, 7 legs)
Creature named Hid (430 kg, 3 legs)
...
</code></pre></div></div>

<h2 id="macrospreprocessor">Macros/Preprocessor</h2>

<p>The C language comes with a feature called <em>preprocessor</em> which is necessary for some advanced things. It allows automatized modification of the source code before it is compiled.</p>

<p>Remember how we said that compiler compiles C programs in several steps such as generating object files and linking? There is one more step we didn’t mention: <strong><a href="preprocessing.md">preprocessing</a></strong>. It is the very 
first step – the source code you give to the compiler first goes to the preprocessor which modifies it according to special commands in the source code called <strong>preprocessor directives</strong>. The result of 
preprocessing is a pure C code without any more preprocessing directives, and this is handed over to the actual compilation.</p>

<p>The preprocessor is like a <strong>mini language on top of the C language</strong>, it has its own commands and rules, but it’s much more simple than C itself, for example it has no data types or loops.</p>

<p>Each directive begins with <code class="language-plaintext highlighter-rouge">#</code>, is followed by the directive name and continues until the end of the line (<code class="language-plaintext highlighter-rouge">\</code> can be used to extend the directive to the next line).</p>

<p>We have already encountered one preprocessor directive: the <code class="language-plaintext highlighter-rouge">#include</code> directive when we included library header files. This directive pastes a text of the file whose name it is handed to the place of the directive.</p>

<p>Another directive is <code class="language-plaintext highlighter-rouge">#define</code> which creates so called <a href="macro.md">macro</a> – in its basic form a macro is nothing else than an alias, a nickname for some text. This is used to create constants. Consider the 
following code:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;

#define ARRAY_SIZE 10

int array[ARRAY_SIZE];

void fillArray(void)
{
  for (int i = 0; i &lt; ARRAY_SIZE; ++i)
    array[i] = i;
}

void printArray(void)
{
  for (int i = 0; i &lt; ARRAY_SIZE; ++i)
    printf("%d ",array[i]);
}

int main()
{
  fillArray();
  printArray();
  return 0;
}
</code></pre></div></div>

<p><code class="language-plaintext highlighter-rouge">#define ARRAY_SIZE 10</code> creates a macro that can be seen as a constant named <code class="language-plaintext highlighter-rouge">ARRAY_SIZE</code> which stands for <code class="language-plaintext highlighter-rouge">10</code>. From this line on any occurence of <code class="language-plaintext highlighter-rouge">ARRAY_SIZE</code> that the preprocessor encounters in the code will be 
replaced with <code class="language-plaintext highlighter-rouge">10</code>. The reason for doing this is obvious – we respect the <a href="dry.md">DRY</a> (don’t repeat yourself) principle, if we didn’t use a constant for the array size and used the direct numeric value <code class="language-plaintext highlighter-rouge">10</code> in 
different parts of the code, it would be difficult to change them all later, especially in a very long code, there’s a danger we’d miss some. With a constant it is enough to change one line in the code (e.g. 
<code class="language-plaintext highlighter-rouge">#define ARRAY_SIZE 10</code> to <code class="language-plaintext highlighter-rouge">#define ARRAY_SIZE 20</code>).</p>

<p>The macro substitution is literally a glorified copy-paste text replacement, there is nothing very complex going on. This means you can create a nickname for almost anything (for example you could do <code class="language-plaintext highlighter-rouge">#define when 
if</code> and then also use <code class="language-plaintext highlighter-rouge">when</code> in place of <code class="language-plaintext highlighter-rouge">if</code> – but it’s probably not a very good idea). By convention macro names are to be <code class="language-plaintext highlighter-rouge">ALL_UPPER_CASE</code> (so that whenever you see an all upper case word in the source code, 
you know it’s a macro).</p>

<p>Macros can optionally take parameters similarly to functions. There are no data types, just parameter names. The usage is demonstrated by the following code:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;

#define MEAN3(a,b,c) (((a) + (b) + (c)) / 3) 

int main()
{
  int n = MEAN3(10,20,25);
  
  printf("%d\n",n);
    
  return 0;
}
</code></pre></div></div>

<p><code class="language-plaintext highlighter-rouge">MEAN3</code> computes the mean of 3 values. Again, it’s just text replacement, so the line <code class="language-plaintext highlighter-rouge">int n = MEAN3(10,20,25);</code> becomes <code class="language-plaintext highlighter-rouge">int n = (((10) + (20) + (25)) / 3);</code> before code compilation. Why are there so many brackets 
in the macro? It’s always good to put brackets over a macro and all its parameters because the parameters are again a simple text replacement; consider e.g. a macro <code class="language-plaintext highlighter-rouge">#define HALF(x) x / 2</code> – if it was invoked as 
<code class="language-plaintext highlighter-rouge">HALF(5 + 1)</code>, the substitution would result in the final text <code class="language-plaintext highlighter-rouge">5 + 1 / 2</code>, which gives 5 (instead of the intended value 3).</p>

<p>You may be asking why would we use a macro when we can use a function for computing the mean? Firstly macros don’t just have to work with numbers, they can be used to generate parts of the source code in ways that 
functions can’t. Secondly using a macro may sometimes be simpler, it’s shorter and will be faster to execute because the is no function call (which has a slight overhead) and because the macro expansion may lead to 
the compiler precomputing expressions at compile time. But beware: macros are usually worse than functions and should only be used in very justified cases. For example macros don’t know about data types and cannot 
check them, and they also result in a bigger compiled executable (function code is in the executable only once whereas the macro is expanded in each place where it is used and so the code it generates multiplies).</p>

<p>Another very useful directive is <code class="language-plaintext highlighter-rouge">#if</code> for conditional inclusion or exclusion of parts of the source code. It is similar to the C <code class="language-plaintext highlighter-rouge">if</code> command. The following example shows its use:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;

#define RUDE 0

void printNumber(int x)
{
  puts(
#if RUDE
    "You idiot, the number is:"
#else
    "The number is:"
#endif
  );
  
  printf("%d\n",x);
}

int main()
{
  printNumber(3);
  printNumber(100);
  
#if RUDE
  puts("Bye.");
#endif
    
  return 0;
}
</code></pre></div></div>

<p>When run, we get the output:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>The number is:
3
The number is:
100
</code></pre></div></div>

<p>And if we change <code class="language-plaintext highlighter-rouge">#define RUDE 0</code> to <code class="language-plaintext highlighter-rouge">#define RUDE 1</code>, we get:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>You idiot, the number is:
3
You idiot, the number is:
100
Bye+.
</code></pre></div></div>

<p>We see the <code class="language-plaintext highlighter-rouge">#if</code> directive has to have a corresponding <code class="language-plaintext highlighter-rouge">#endif</code> directive that terminates it, and there can be an optional <code class="language-plaintext highlighter-rouge">#else</code> directive for an <em>else</em> branch. The condition after <code class="language-plaintext highlighter-rouge">#if</code> can use similar operators 
as those in C itself (<code class="language-plaintext highlighter-rouge">+</code>, <code class="language-plaintext highlighter-rouge">==</code>, <code class="language-plaintext highlighter-rouge">&amp;&amp;</code>, <code class="language-plaintext highlighter-rouge">||</code> etc.). There also exists an <code class="language-plaintext highlighter-rouge">#ifdef</code> directive which is used the same and checks if a macro of given name has been defined.</p>

<p><code class="language-plaintext highlighter-rouge">#if</code> directives are very useful for conditional compilation, they allow for creation of various “settings” and parameters that can fine-tune a program – you may turn specific features on and off with this 
directive. It is also helpful for <a href="portability.md">portability</a>; compilers may automatically define specific macros depending on the platform (e.g. <code class="language-plaintext highlighter-rouge">_WIN64</code>, <code class="language-plaintext highlighter-rouge">__APPLE__</code>, …) based on which you can trigger 
different code. E.g.:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#ifdef _WIN64
  puts("Your OS sucks.");
#endif
</code></pre></div></div>

<p>Let us talk about one more thing that doesn’t fall under the preprocessor language but is related to constants: <strong>enumerations</strong>. Enumeration is a data type that can have values that we specify individually, for 
example:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>typedef enum
{
  APPLE,
  PEAR,
  TOMATO
} Fruit;
</code></pre></div></div>

<p>This creates a new data type <code class="language-plaintext highlighter-rouge">Fruit</code>. Variables of this type may have values <code class="language-plaintext highlighter-rouge">APPLE</code>, <code class="language-plaintext highlighter-rouge">PEAR</code> or <code class="language-plaintext highlighter-rouge">TOMATO</code>, so we may for example do <code class="language-plaintext highlighter-rouge">Fruit myFruit = APPLE;</code>. These values are in fact integers and the names we give 
them are just nicknames, so here <code class="language-plaintext highlighter-rouge">APPLE</code> is equal to 0, <code class="language-plaintext highlighter-rouge">PEAR</code> to 1 and <code class="language-plaintext highlighter-rouge">TOMATO</code> to 2.</p>

<h2 id="pointers">Pointers</h2>

<p>Pointers are an advanced topic that many people fear – many complain they’re hard to learn, others complain about memory unsafety and potential dangers of using pointers. These people are stupid, pointers are 
great.</p>

<p>But beware, there may be too much new information in the first read. Don’t get scared, give it some time.</p>

<p>Pointers allow us to do certain advanced things such as allocate dynamic memory, return multiple values from functions, inspect content of memory or use functions in similar ways in which we use variables.</p>

<p>A <strong><a href="pointer.md">pointer</a></strong> is essentially nothing complicated: it is a <strong>data type that can hold a memory address</strong> (plus an information about what data type should be stored at that address). An address is simply 
a number. Why can’t we just use an <code class="language-plaintext highlighter-rouge">int</code> to store a memory address? Because the size of <code class="language-plaintext highlighter-rouge">int</code> and a pointer may differ, the size of pointer depends on each platform’s address width. Besides this, as said, a pointer 
actually holds not only an address but also the information about the type it points to, which is a safety mechanism that will become clear later. It is also good when the compiler knows a certain variable is 
supposed to point to a memory rather than to hold a generic number – this can all prevent bugs. I.e. pointers and generic integers are distinguished for the same reason other data types are distinguished – in 
theory they don’t have to be distinguished, but it’s safer.</p>

<p>It is important to stress again that a pointer is not a pure address but it also knows about the data type it is pointing to, so there are many kinds of pointers: a pointer to <code class="language-plaintext highlighter-rouge">int</code>, a pointer to <code class="language-plaintext highlighter-rouge">char</code>, a pointer 
to a specific struct type etc.</p>

<p>A variable of pointer type is created similarly to a normal variable, we just add <code class="language-plaintext highlighter-rouge">*</code> after the data type, for example <code class="language-plaintext highlighter-rouge">int *x;</code> creates a variable named <code class="language-plaintext highlighter-rouge">x</code> that is a pointer to <code class="language-plaintext highlighter-rouge">int</code> (some people would write this 
as <code class="language-plaintext highlighter-rouge">int* x;</code>).</p>

<p>But how do we assign a value to the pointer? To do this, we need an address of something, e.g. of some variable. To get an address of a variable we use the <code class="language-plaintext highlighter-rouge">&amp;</code> character, i.e. <code class="language-plaintext highlighter-rouge">&amp;a</code> is the address of a variable <code class="language-plaintext highlighter-rouge">a</code>.</p>

<p>The last basic thing we need to know is how to <strong><a href="dereference.md">dereference</a></strong> a pointer. Dereferencing means accessing the value at the address that’s stored in the pointer, i.e. working with the pointed to 
value. This is again done (maybe a bit confusingly) with <code class="language-plaintext highlighter-rouge">*</code> character in front of a pointer, e.g. if <code class="language-plaintext highlighter-rouge">x</code> is a pointer to <code class="language-plaintext highlighter-rouge">int</code>, <code class="language-plaintext highlighter-rouge">*x</code> is the <code class="language-plaintext highlighter-rouge">int</code> value to which the pointer is pointing. An example can perhaps make 
it clearer.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;

int main(void)
{
  int normalVariable = 10;
  int *pointer;
  
  pointer = &amp;normalVariable;
  
  printf("address in pointer: %p\n",pointer);
  printf("value at this address: %d\n",*pointer);
  
  *pointer = *pointer + 10;
  
  printf("normalVariable: %d\n",normalVariable);
  
  return 0;
}
</code></pre></div></div>

<p>This may print e.g.:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>address in pointer: 0x7fff226fe2ec
value at this address: 10
normalVariable: 20
</code></pre></div></div>

<p><code class="language-plaintext highlighter-rouge">int *pointer;</code> creates a pointer to <code class="language-plaintext highlighter-rouge">int</code> with name <code class="language-plaintext highlighter-rouge">pointer</code>. Next we make the pointer point to the variable <code class="language-plaintext highlighter-rouge">normalVariable</code>, i.e. we get the address of the variable with <code class="language-plaintext highlighter-rouge">&amp;normalVariable</code> and assign it normally 
to <code class="language-plaintext highlighter-rouge">pointer</code>. Next we print firstly the address in the pointer (accessed with <code class="language-plaintext highlighter-rouge">pointer</code>) and the value at this address, for which we use dereference as <code class="language-plaintext highlighter-rouge">*pointer</code>. At the next line we see that we can also use 
dereference for writing to the pointed address, i.e. doing <code class="language-plaintext highlighter-rouge">*pointer = *pointer + 10;</code> here is the same as doing <code class="language-plaintext highlighter-rouge">normalVariable = normalVariable + 10;</code>. The last line shows that the value in <code class="language-plaintext highlighter-rouge">normalVariable</code> has 
indeed changed.</p>

<p>IMPORTANT NOTE: <strong>You generally cannot read and write from/to random addresses</strong>! This will crash your program. To be able to write to a certain address it must be <em><a href="allocation.md">allocated</a></em>, i.e. reserved for 
use. Addresses of variables are allocated by the compiler and can be safely operated with.</p>

<p>There’s a special value called <code class="language-plaintext highlighter-rouge">NULL</code> (a macro defined in the standard library) that is meant to be assigned to pointer that points to “nothing”. So when we have a pointer <code class="language-plaintext highlighter-rouge">p</code> that’s currently not supposed to point 
to anything, we do <code class="language-plaintext highlighter-rouge">p = NULL;</code>. In a safe code we should always check (with <code class="language-plaintext highlighter-rouge">if</code>) whether a pointer is not <code class="language-plaintext highlighter-rouge">NULL</code> before dereferencing it, and if it is, then NOT dereference it. This isn’t required but is 
considered a “good practice” in safe code, storing <code class="language-plaintext highlighter-rouge">NULL</code> in pointers that point nowhere prevents dereferencing random or unallocated addresses which would crash the program.</p>

<p>But what can pointers be good for? Many things, for example we can kind of “store variables in variables”, i.e. a pointer is a variable which says which variable we are now using, and we can switch between variable 
any time. E.g.:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;

int bankAccountMonica = 1000;
int bankAccountBob = -550;
int bankAccountJose = 700;

int *payingAccount; // pointer to who's currently paying

void payBills(void)
{
  *payingAccount -= 200;
}

void buyFood(void)
{
  *payingAccount -= 50;
}

void buyGas(void)
{
  *payingAccount -= 20;
}

int main(void)
{
  // let Jose pay first
  
  payingAccount = &amp;bankAccountJose;
  
  payBills();
  buyFood();
  buyGas();
    
  // that's enough, now let Monica pay 

  payingAccount = &amp;bankAccountMonica;

  buyFood();
  buyGas();
  buyFood();
  buyFood();
    
  // now it's Bob's turn
  
  payingAccount = &amp;bankAccountBob;
  
  payBills();
  buyFood();
  buyFood();
  buyGas();
    
  printf("Monika has $%d left.\n",bankAccountMonica);
  printf("Jose has $%d left.\n",bankAccountJose);
  printf("Bob has $%d left.\n",bankAccountBob);
    
  return 0;
}
</code></pre></div></div>

<p>Well, this could be similarly achieved with arrays, but pointers have more uses. For example they allow us to <strong>return multiple values by a function</strong>. Again, remember that we said that (with the exception of 
arrays) a function cannot modify a variable passed to it because it always makes its own local copy of it? We can bypass this by, instead of giving the function the value of the variable, giving it the address of 
the variable. The function can read the value of that variable (with dereference) but it can also CHANGE the value, it simply writes a new value to that address (again, using dereference). This example shows it:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;
#include &lt;math.h&gt;

#define PI 3.141592

// returns 2D coordinates of a point on a unit circle
void getUnitCirclePoint(float angle, float *x, float *y)
{
  *x = sin(angle);
  *y = cos(angle);
}

int main(void)
{
  for (int i = 0; i &lt; 8; ++i)
  {
    float pointX, pointY;
    
    getUnitCirclePoint(i * 0.125 * 2 * PI,&amp;pointX,&amp;pointY);
    
    printf("%lf %lf\n",pointX,pointY);
  }
    
  return 0;
}
</code></pre></div></div>

<p>Function <code class="language-plaintext highlighter-rouge">getUnitCirclePoint</code> doesn’t return any value in the strict sense, but thank to pointers it effectively returns two <code class="language-plaintext highlighter-rouge">float</code> values via its parameters <code class="language-plaintext highlighter-rouge">x</code> and <code class="language-plaintext highlighter-rouge">y</code>. These parameters are of the data type 
pointer to <code class="language-plaintext highlighter-rouge">float</code> (as there’s <code class="language-plaintext highlighter-rouge">*</code> in front of them). When we call the function with <code class="language-plaintext highlighter-rouge">getUnitCirclePoint(i * 0.125 * 2 * PI,&amp;pointX,&amp;pointY);</code>, we hand over the addresses of the variables <code class="language-plaintext highlighter-rouge">pointX</code> and <code class="language-plaintext highlighter-rouge">pointY</code> 
(which belong to the <code class="language-plaintext highlighter-rouge">main</code> function and couldn’t normally be accessed in <code class="language-plaintext highlighter-rouge">getUnitCirclePoint</code>). The function can then compute values and write them to these addresses (with dereference, <code class="language-plaintext highlighter-rouge">*x</code> and <code class="language-plaintext highlighter-rouge">*y</code>), changing 
the values in <code class="language-plaintext highlighter-rouge">pointX</code> and <code class="language-plaintext highlighter-rouge">pointY</code>, effectively returning two values.</p>

<p>Now let’s take a look at pointers to structs. Everything basically works the same here, but there’s one thing to know about, a <a href="sugar.md">syntactic sugar</a> known as an arrow (<code class="language-plaintext highlighter-rouge">-&gt;</code>). Example:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;

typedef struct
{
  int a;
  int b;
} SomeStruct;

SomeStruct s;
SomeStruct *sPointer;

int main(void)
{
  sPointer = &amp;s;
  
  (*sPointer).a = 10; // without arrow
  sPointer-&gt;b = 20;   // same as (*sPointer).b = 20
    
  printf("%d\n",s.a);
  printf("%d\n",s.b);
    
  return 0;
}
</code></pre></div></div>

<p>Here we are trying to write values to a struct through pointers. Without using the arrow we can simply dereference the pointer with <code class="language-plaintext highlighter-rouge">*</code>, put brackets around and access the member of the struct normally. This shows 
the line <code class="language-plaintext highlighter-rouge">(*sPointer).a = 10;</code>. Using an arrow achieves the same thing but is perhaps a bit more readable, as seen in the line <code class="language-plaintext highlighter-rouge">sPointer-&gt;b = 20;</code>. The arrow is simply a special shorthand and doesn’t need any 
brackets.</p>

<p>Now let’s talk about arrays – these are a bit special. The important thing is that <strong>an array is itself basically a pointer</strong>. What does this mean? If we create an array, let’s say <code class="language-plaintext highlighter-rouge">int myArray[10];</code>, then 
<code class="language-plaintext highlighter-rouge">myArray</code> is basically a pointer to <code class="language-plaintext highlighter-rouge">int</code> in which the address of the first array item is stored. When we index the array, e.g. like <code class="language-plaintext highlighter-rouge">myArray[3] = 1;</code>, behind the scenes there is basically a dereference because the 
index 3 means: 3 places after the address pointed to by <code class="language-plaintext highlighter-rouge">myArray</code>. So when we index an array, the compiler takes the address stored in <code class="language-plaintext highlighter-rouge">myArray</code> (the address of the array start) and adds 3 to it (well, kind of) by 
which it gets the address of the item we want to access, and then dereferences this address.</p>

<p>Arrays and pointer are kind of a duality – we can also use array indexing with pointers. For example if we have a pointer declared as <code class="language-plaintext highlighter-rouge">int *x;</code>, we can access the value <code class="language-plaintext highlighter-rouge">x</code> points to with a dereference (<code class="language-plaintext highlighter-rouge">*x</code>), but 
ALSO with indexing like this: <code class="language-plaintext highlighter-rouge">x[0]</code>. Accessing index 0 simply means: take the value stored in the variable and add 0 to it, then dereference it. So it achieves the same thing. We can also use higher indices (e.g. 
<code class="language-plaintext highlighter-rouge">x[10]</code>), BUT ONLY if <code class="language-plaintext highlighter-rouge">x</code> actually points to a memory that has at least 11 allocated places.</p>

<p>This leads to a concept called <strong><a href="pointer_arithmetic.md">pointer arithmetic</a></strong>. Pointer arithmetic simply means we can add or subtract numbers to pointer values. If we continue with the same pointer as above (<code class="language-plaintext highlighter-rouge">int 
*x;</code>), we can actually add numbers to it like <code class="language-plaintext highlighter-rouge">*(x + 1) = 10;</code>. What does this mean?! It means exactly the same thing as <code class="language-plaintext highlighter-rouge">x[1]</code>. Adding a number to a pointer shifts that pointer given number of <em>places</em> forward. We 
use the word <em>places</em> because each data type takes a different space in memory, for example <code class="language-plaintext highlighter-rouge">char</code> takes one byte of memory while <code class="language-plaintext highlighter-rouge">int</code> takes usually 4 (but not always), so shifting a pointer by <em>N</em> places means 
adding <em>N</em> times the size of the pointed to data type to the address stored in the pointer.</p>

<p>This may be a lot information to digest. Let’s provide an example to show all this in practice:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;

// our own string print function
void printString(char *s)
{
  int position = 0;
  
  while (s[position] != 0)
  {
    putchar(s[position]);
    position += 1;
  }
}

// returns the length of string s
int stringLength(char *s)
{
  int length = 0;
    
  while (*s != 0) // count until terminating 0
  {
    length += 1;
    s += 1; // shift the pointer one character to right
  }
  
  return length;
}

int main(void)
{
  char testString[] = "catdog";
  
  printString("The string '");
  printString(testString);
  printString("' has length ");
  
  int l = stringLength(testString);
  
  printf("%d.",l);

  return 0;
}
</code></pre></div></div>

<p>The output is:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>The string 'catdog' has length 6.
</code></pre></div></div>

<p>We’ve created a function for printing strings (<code class="language-plaintext highlighter-rouge">printString</code>) similar to <code class="language-plaintext highlighter-rouge">puts</code> and a function for computing the length of a string (<code class="language-plaintext highlighter-rouge">stringLength</code>). They both take as an argument a pointer to <code class="language-plaintext highlighter-rouge">char</code>, i.e. a 
string. In <code class="language-plaintext highlighter-rouge">printString</code> we use indexing (<code class="language-plaintext highlighter-rouge">[</code> and <code class="language-plaintext highlighter-rouge">]</code>) just as if <code class="language-plaintext highlighter-rouge">s</code> was an array, and indeed we see it works! In <code class="language-plaintext highlighter-rouge">stringLength</code> we similarly iterate over all characters in the string but we use dereference (<code class="language-plaintext highlighter-rouge">*s</code>) 
and pointer arithmetic (<code class="language-plaintext highlighter-rouge">s += 1;</code>). It doesn’t matter which of the two styles we choose – here we’ve shown both, for educational purposes. Finally notice that the string we actually work with is created in <code class="language-plaintext highlighter-rouge">main</code> 
as an array  with <code class="language-plaintext highlighter-rouge">char testString[] = "catdog";</code> – here we don’t need to specify the array size between <code class="language-plaintext highlighter-rouge">[</code> and <code class="language-plaintext highlighter-rouge">]</code> because we immediately assign a string literal to it (<code class="language-plaintext highlighter-rouge">"catdog"</code>) and in such a case the 
compiler knows how big the array needs to be and automatically fills in the correct size.</p>

<p>Now that know about pointers, we can finally completely explain the functions from <code class="language-plaintext highlighter-rouge">stdio</code> we’ve been using:</p>

<ul>
  <li><code class="language-plaintext highlighter-rouge">int puts(char *s)</code>: A simple and fast function for printing a string (adds the newline character <code class="language-plaintext highlighter-rouge">\n</code> at the end).</li>
  <li><code class="language-plaintext highlighter-rouge">int printf(char *format, ...)</code>: A little bit more complex function that can print not only strings but also other data types. It takes a variable number of parameters. The first one is always a string that 
specifies the print format – this string can contain special sequences that will be replaced by textual representations of values we additionally provide as extra parameters after <code class="language-plaintext highlighter-rouge">format</code>. E.g. the sequence “%d” 
is replaced with a number obtained from the value of a corresponding <code class="language-plaintext highlighter-rouge">int</code> parameter. Similarly <code class="language-plaintext highlighter-rouge">%c</code> is for <code class="language-plaintext highlighter-rouge">char</code>, <code class="language-plaintext highlighter-rouge">%s</code> for strings, <code class="language-plaintext highlighter-rouge">%p</code> for pointers. Example: <code class="language-plaintext highlighter-rouge">printf("MyInt = %d, myChar = %c, MyStr = %s\n",
myInt,myChar,myStr);</code>.</li>
  <li><code class="language-plaintext highlighter-rouge">int getchar(void)</code>: Reads a single text character from the input and returns it. Why does the function return <code class="language-plaintext highlighter-rouge">int</code> and not <code class="language-plaintext highlighter-rouge">char</code>? Because the function can return additional special values such as <code class="language-plaintext highlighter-rouge">EOF</code> (end of 
file) which couldn’t be stored in plain <code class="language-plaintext highlighter-rouge">char</code>.</li>
  <li><code class="language-plaintext highlighter-rouge">int scanf(char *format, ...)</code>: Function for reading various data types from the input. Like <code class="language-plaintext highlighter-rouge">printf</code> it takes a variable number of parameters. The first one is a string that specifies which data type(s) to read 
– this is a bit complicated but “%d” reads an <code class="language-plaintext highlighter-rouge">int</code>, “%f” <code class="language-plaintext highlighter-rouge">float</code>, “%c” <code class="language-plaintext highlighter-rouge">char</code> and  “%s” string. The following arguments are <strong>pointers</strong> to expected data types, so e.g. if we’ve provided the format string “%d”, a 
pointer to <code class="language-plaintext highlighter-rouge">int</code> has to follow. Through this parameter the value that’s been read will be returned (in the same way we’ve seen in one example above).</li>
</ul>

<h2 id="files">Files</h2>

<p>Now we’ll take a look at how we can read and write from/to files on the computer disk which enables us to store information permanently or potentially process data such as images or audio. Files aren’t so 
difficult.</p>

<p>We work with files through functions provided in the <em>stdio</em> library (so it has to be included). We distinguish two types of files:</p>

<ul>
  <li><strong>text files</strong>: Contain text, are human readable.</li>
  <li><strong>binary files</strong>: Contain binary data, aren’t human readable, are more efficient but also more prone to corruption.</li>
</ul>

<p>From programmer’s point of view there’s actually not a huge difference between the two, they’re both just sequences of characters or bytes (which are kind of almost the same). Text files are a little more abstract, 
they handle potentially different format of newlines etc. The main thing for us is that we’ll use slightly different functions for each type.</p>

<p>There is a special data type for file called <code class="language-plaintext highlighter-rouge">FILE</code> (we’ll be using a pointer to it). Whatever file we work with, we need to firstly open it with the function <code class="language-plaintext highlighter-rouge">fopen</code> and when we’re done with it, we need to close 
it with a function <code class="language-plaintext highlighter-rouge">fclose</code>.</p>

<p>First we’ll write something to a text file:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;

int main(void)
{
  FILE *textFile = fopen("test.txt","w"); // "w" for write

  if (textFile != NULL) // if opened successfully
    fprintf(textFile,"Hello file.");
  else
    puts("ERROR: Couldn't open file.");

  fclose(textFile);

  return 0;
}
</code></pre></div></div>

<p>When run, the program should create a new file named <em>test.txt</em> in the same directory we’re in and in it you should find the text <code class="language-plaintext highlighter-rouge">Hello file.</code>. <code class="language-plaintext highlighter-rouge">FILE *textFile</code> creates a new variable <code class="language-plaintext highlighter-rouge">textFile</code> which is a pointer 
to the <code class="language-plaintext highlighter-rouge">FILE</code> data type. We are using a pointer simply because the standard library is designed this way, its functions work with pointers (it can be more efficient). <code class="language-plaintext highlighter-rouge">fopen("test.txt","w");</code> attempts to open the 
file <em>test.txt</em> in text mode for writing – it returns a pointer that represents the opened file. The mode, i.e. text/binary, read/write etc., is specified by the second argument: <code class="language-plaintext highlighter-rouge">"w"</code>; <em>w</em> simply specifies 
<em>write</em> and the text mode is implicit (it doesn’t have to be specified). <code class="language-plaintext highlighter-rouge">if (textFile != NULL)</code> checks if the file has been successfully opened; the function <code class="language-plaintext highlighter-rouge">fopen</code> returns <code class="language-plaintext highlighter-rouge">NULL</code> (the value of “point to nothing” 
pointers) if there was an error with opening the file (such as that the file doesn’t exist). On success we write text to the file with a function <code class="language-plaintext highlighter-rouge">fprintf</code> – it’s basically the same as <code class="language-plaintext highlighter-rouge">printf</code> but works on files, 
so it’s first parameter is always a pointer to a file to which it should write. You can of course also print numbers and anything that <code class="language-plaintext highlighter-rouge">printf</code> can with this function. Finally we mustn’t forget to close the file at 
the end with <code class="language-plaintext highlighter-rouge">fclose</code>!</p>

<p>Now let’s write another program that reads the file we’ve just created and writes its content out in the command line:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;

int main(void)
{
  FILE *textFile = fopen("test.txt","r"); // "r" for read

  if (textFile != NULL) // if opened successfully
  {
    char c;

    while (fscanf(textFile,"%c",&amp;c) != EOF) // while not end of file
      putchar(c);
  }
  else
    puts("ERROR: Couldn't open file.");

  fclose(textFile);

  return 0;
}
</code></pre></div></div>

<p>Notice that in <code class="language-plaintext highlighter-rouge">fopen</code> we now specify <code class="language-plaintext highlighter-rouge">"r"</code> (read) as a mode. Again, we check if the file has been opened successfully (<code class="language-plaintext highlighter-rouge">if (textFile != NULL)</code>). If so, we use a <code class="language-plaintext highlighter-rouge">while</code> loop to read and print all characters from 
the file until we encounter the end of file. The reading of file characters is done with the <code class="language-plaintext highlighter-rouge">fscanf</code> function inside the loop’s condition – there’s nothing preventing us from doing this. <code class="language-plaintext highlighter-rouge">fscanf</code> again works the 
same as <code class="language-plaintext highlighter-rouge">scanf</code> (so it can read other types than only <code class="language-plaintext highlighter-rouge">char</code>s), just on files (its first argument is the file to read from). On encountering end of file <code class="language-plaintext highlighter-rouge">fscanf</code> returns a special value <code class="language-plaintext highlighter-rouge">EOF</code> (which is macro 
constant defined in the standard library). Again, we must close the file at the end with <code class="language-plaintext highlighter-rouge">fclose</code>.</p>

<p>We will now write to a binary file:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;

int main(void)
{
  unsigned char image[] = // image in ppm format
  { 
    80, 54, 32, 53, 32, 53, 32, 50, 53, 53, 32,
    255,255,255, 255,255,255, 255,255,255, 255,255,255, 255,255,255,
    255,255,255,    0, 0,  0, 255,255,255,   0,  0,  0, 255,255,255,
    255,255,255, 255,255,255, 255,255,255, 255,255,255, 255,255,255,
      0,  0,  0, 255,255,255, 255,255,255, 255,255,255,   0,  0,  0,
    255,255,255,   0,  0,  0,   0,  0,  0,   0,  0,  0, 255,255,255  
  };

  FILE *binFile = fopen("image.ppm","wb");

  if (binFile != NULL) // if opened successfully
    fwrite(image,1,sizeof(image),binFile);
  else
    puts("ERROR: Couldn't open file.");

  fclose(binFile);

  return 0;
}
</code></pre></div></div>

<p>Okay, don’t get scared, this example looks complex because it is trying to do a cool thing: it creates an image file! When run, it should produce a file named <em>image.ppm</em> which is a tiny 5x5 smiley face image in 
<a href="ppm.md">ppm</a> format. You should be able to open the image in any good viewer (I wouldn’t bet on <a href="windows.md">Windows</a> programs though). The image data was made manually and are stored in the <code class="language-plaintext highlighter-rouge">image</code> array. We 
don’t need to understand the data, we just know we have some data we want to write to a file. Notice how we can manually initialize the array with values using <code class="language-plaintext highlighter-rouge">{</code> and <code class="language-plaintext highlighter-rouge">}</code> brackets. We open the file for writing and 
in binary mode, i.e. with a mode <code class="language-plaintext highlighter-rouge">"wb"</code>, we check the success of the action and then write the whole array into the file with one function call. The function is name <code class="language-plaintext highlighter-rouge">fwrite</code> and is used for writing to binary files 
(as opposed to <code class="language-plaintext highlighter-rouge">fprintf</code> for text files). <code class="language-plaintext highlighter-rouge">fwrite</code> takes these parameters: pointer to the data to be written to the file, size of one data element (in bytes), number of data elements and a pointer to the file to 
write to. Our data is the <code class="language-plaintext highlighter-rouge">image</code> array and since “arrays are basically pointers”, we provide it as the first argument. Next argument is 1 (<code class="language-plaintext highlighter-rouge">unsigned char</code> always takes 1 byte), then length of our array (<code class="language-plaintext highlighter-rouge">sizeof</code> 
is a special operator that substitutes the size of a variable in bytes – since each item in our array takes 1 byte, <code class="language-plaintext highlighter-rouge">sizeof(image)</code> provides the number of items in the array), and the file pointer. At the end we 
close the file.</p>

<p>And finally we’ll finish with reading this binary file back:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;

int main(void)
{
  FILE *binFile = fopen("image.ppm","rb");

  if (binFile != NULL) // if opened successfully
  {
    unsigned char byte;

    while (fread(&amp;byte,1,1,binFile))
      printf("%d ",byte);

    putchar('\n');
  }
  else
    puts("ERROR: Couldn't open file.");

  fclose(binFile);

  return 0;
}
</code></pre></div></div>

<p>The file mode is now <code class="language-plaintext highlighter-rouge">"rb"</code> (read binary). For reading from binary files we use the <code class="language-plaintext highlighter-rouge">fread</code> function, similarly to how we used <code class="language-plaintext highlighter-rouge">fscanf</code> for reading from a text file. <code class="language-plaintext highlighter-rouge">fread</code> has these parameters: pointer where to 
store the read data (the memory must have sufficient space allocated!), size of one data item, number of items to read and the pointer to the file which to read from. As the first argument we pass <code class="language-plaintext highlighter-rouge">&amp;byte</code>, i.e. the 
address of the variable <code class="language-plaintext highlighter-rouge">byte</code>, next 1 (we want to read a single byte whose size in bytes is 1), 1 (we want to read one byte) and the file pointer. <code class="language-plaintext highlighter-rouge">fread</code> returns the number of items read, so the <code class="language-plaintext highlighter-rouge">while</code> condition 
holds as long as <code class="language-plaintext highlighter-rouge">fread</code> reads bytes; once we reach end of file, <code class="language-plaintext highlighter-rouge">fread</code> can no longer read anything and returns 0 (which in C is interpreted as a false value) and the loop ends. Again, we must close the file at 
the end.</p>

<h2 id="more-on-functions-recursion-function-pointers">More On Functions (Recursion, Function Pointers)</h2>

<p>There’s more to be known about functions.</p>

<p>An important concept in programming is <a href="recursion.md">recursion</a> – the situation in which a function calls itself. Yes, it is possible, but some rules have to be followed.</p>

<p>When a function calls itself, we have to ensure that we won’t end up in infinite recursion (i.e. the function calls itself which subsequently calls itself and so on until infinity). This crashes our program. There 
always has to be a <strong>terminating condition</strong> in a recursive function, i.e. an <code class="language-plaintext highlighter-rouge">if</code> branch that will eventually stop the function from calling itself again.</p>

<p>But what is this even good for? Recursion is actually very common in math and programming, many problems are recursive in nature. Many things are beautifully described with recursion (e.g. <a href="fractal.md">fractals</a>). 
But remember: anything a recursion can achieve can also be achieved by iteration (loop) and vice versa. It’s just that sometimes one is more elegant or more computationally efficient.</p>

<p>Let’s see this on a typical example of the mathematical function called <a href="factorial.md">factorial</a>. Factorial of <em>N</em> is defined as <em>N</em> x <em>(N - 1)</em> x <em>(N - 2)</em> x … x 1. It can also be defined recursively as: 
factorial of <em>N</em> is 1 if <em>N</em> is 0, otherwise <em>N</em> times factorial of <em>N - 1</em>. Here is some code:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;

unsigned int factorialRecursive(unsigned int x)
{
  if (x == 0) // terminating condition
    return 1;
  else
    return x * factorialRecursive(x - 1);
}

unsigned int factorialIterative(unsigned int x)
{
  unsigned int result = 1;
    
  while (x &gt; 1)
  {
    result *= x;
    x--;  
  }
  
  return result;
}

int main(void)
{
  printf("%d %d\n",factorialRecursive(5),factorialIterative(5));
  return 0;
}
</code></pre></div></div>

<p><code class="language-plaintext highlighter-rouge">factorialIterative</code> computes the factorial by iteration. <code class="language-plaintext highlighter-rouge">factorialRecursive</code> uses recursion – it calls itself. The important thing is the recursion is guaranteed to end because every time the function calls 
itself, it passes a decremented argument so at one point the function will receive 0 in which case the terminating condition (<code class="language-plaintext highlighter-rouge">if (x == 0)</code>) will be triggered which will avoid the further recursive call.</p>

<p>It should be mentioned that performance-wise recursion is almost always worse than iteration (function calls have certain overhead), so in practice it is used sparingly. But in some cases it is very well justified 
(e.g. when it makes code much simpler while creating unnoticeable performance loss).</p>

<p>Another thing to mention is that we can have <strong>pointers to functions</strong>; this is an advanced topic so we’ll stay at it just briefly. Function pointers are pretty powerful, they allow us to create so called *
<a href="callback.md">callbacks</a>*: imagine we are using some <a href="gui.md">GUI</a> framework and we want to tell it what should happen when a user clicks on a specific button – this is usually done by giving the framework a 
pointer to our custom function that it will be called by the framework whenever the button is clicked.</p>

<h2 id="dynamic-allocation-malloc">Dynamic Allocation (Malloc)</h2>

<p>Dynamic memory allocation means the possibility of reserving additional memory (<a href="ram.md">RAM</a>) for our program at run time, whenever we need it. This is opposed to static memory allocation, i.e. reserving memory 
for use at compile time (when compiling, before the program runs). We’ve already been doing static allocation whenever we created a variable – compiler automatically reserves as much memory for our variables as is 
needed. But what if we’re writing a program but don’t yet know how much memory it will need? Maybe the program will be reading a file but we don’t know how big that file is going to be – how much memory should we 
reserve? Dynamic allocation allows us to reserve this memory with functions when the program is actually runing and already knows how much of it should be reserved.</p>

<p>It must be known that dynamic allocation comes with a new kind of bug known as a <strong><a href="memory_leak.md">memory leak</a></strong>. It happens when we reserve a memory and forget to free it after we no longer need it. If this 
happens e.g. in a loop, the program will continue to “grow”, eat more and more RAM until operating system has no more to give. For this reason, as well as others such as simplicity, it may sometimes be better to go 
with only static allocation.</p>

<p>Anyway, let’s see how we can allocate memory if we need to. We use mostly just two functions that are provided by the <em>stdlib</em> library. One is <code class="language-plaintext highlighter-rouge">malloc</code> which takes as an argument size of the memory we want to 
allocate (reserve) in bytes and returns a pointer to this allocated memory if successful or <code class="language-plaintext highlighter-rouge">NULL</code> if the memory couldn’t be allocated (which in serious programs we should always check). The other function is 
<code class="language-plaintext highlighter-rouge">free</code> which frees the memory when we no longer need it (every allocated memory should be freed at some point) – it takes as the only parameter a pointer to the memory we’ve previously allocated. There is also 
another function called <code class="language-plaintext highlighter-rouge">realloc</code> which serves to change the size of an already allocated memory: it takes a pointer the the allocated memory and the new size in byte, and returns the pointer to the resized memory.</p>

<p>Here is an example:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

#define ALLOCATION_CHUNK 32 // by how many bytes to resize

int main(void)
{
  int charsRead = 0;
  int resized = 0; // how many times we called realloc
  char *inputChars = malloc(ALLOCATION_CHUNK * sizeof(char)); // first allocation

  while (1) // read input characters
  {
    char c = getchar();
 
    charsRead++;

    if ((charsRead % ALLOCATION_CHUNK) == 0)
    {
      inputChars = // we need more space, resize the array
        realloc(inputChars,(charsRead / ALLOCATION_CHUNK + 1) * ALLOCATION_CHUNK * sizeof(char));
        
      resized++;
    }

    inputChars[charsRead - 1] = c;
   
    if (c == '\n')
    {
      charsRead--; // don't count the last newline character
      break;
    }
  }
  
  puts("The string you entered backwards:");
  
  while (charsRead &gt; 0)
  {
    putchar(inputChars[charsRead - 1]);
    charsRead--;
  }

  free(inputChars); // important!
  
  putchar('\n');
  printf("I had to resize the input buffer %d times.",resized);
    
  return 0;
}
</code></pre></div></div>

<p>This code reads characters from the input and stores them in an array (<code class="language-plaintext highlighter-rouge">inputChars</code>) – the array is dynamically resized if more characters are needed. (We restrain from calling the array <code class="language-plaintext highlighter-rouge">inputChars</code> a string 
because we never terminate it with 0, we couldn’t print it with standard functions like <code class="language-plaintext highlighter-rouge">puts</code>.) At the end the entered characters are printed backwards (to prove we really stored all of them), and we print out how 
many times we needed to resize the array.</p>

<p>We define a constant (macro) <code class="language-plaintext highlighter-rouge">ALLOCATION_CHUNK</code> that says by how many characters we’ll be resizing our character buffer. I.e. at the beginning we create a character buffer of size <code class="language-plaintext highlighter-rouge">ALLOCATION_CHUNK</code> and start 
reading input character into it. Once it fills up we resize the buffer by another <code class="language-plaintext highlighter-rouge">ALLOCATION_CHUNK</code> characters and so on. We could be resizing the buffer by single characters but that’s usually inefficient (the 
function <code class="language-plaintext highlighter-rouge">malloc</code> may be quite complex and take some time to execute).</p>

<p>The line starting with <code class="language-plaintext highlighter-rouge">char *inputChars = malloc(...</code> creates a pointer to <code class="language-plaintext highlighter-rouge">char</code> – our character buffer – to which we assign a chunk of memory allocated with <code class="language-plaintext highlighter-rouge">malloc</code>. Its size is <code class="language-plaintext highlighter-rouge">ALLOCATION_CHUNK * sizeof
(char)</code>. Note that for simplicity we don’t check if <code class="language-plaintext highlighter-rouge">inputChars</code> is not <code class="language-plaintext highlighter-rouge">NULL</code>, i.e. whether the allocation succeeded – but in your program you should do it :) Then we enter the character reading loop inside which 
we check if the buffer has filled up (<code class="language-plaintext highlighter-rouge">if ((charsRead % ALLOCATION_CHUNK) == 0)</code>). If so, we used the <code class="language-plaintext highlighter-rouge">realloc</code> function to increase the size of the character buffer. The important thing is that once we exit the 
loop and print the characters stored in the buffer, we free the memory with <code class="language-plaintext highlighter-rouge">free(inputChars);</code> as we no longer need it.</p>

<h2 id="debugging-optimization">Debugging, Optimization</h2>

<p><a href="debugging.md">Debugging</a> means localizing and fixing <a href="bug.md">bugs</a> (errors) in your program. In practice there are always bugs, even in very short programs (you’ve probably already figured that out yourself), 
some small and insignificant and some pretty bad ones that make your program unusable, vulnerable or even dangerous.</p>

<p>There are two kinds of bugs: <strong><a href="syntax.md">syntactic</a> errors</strong> and <strong><a href="semantics.md">semantic</a> errors</strong>. A syntactic error is when you write something not obeying the C grammar, it’s like a typo or grammatical error 
in a normal language – these errors are very easy to detect and fix, a compiler won’t be able to understand your program and will point you to the exact place where the error occurs. A semantic error can be much 
worse – it’s a logical error in the program; the program will compile and run but the program will behave differently than intended. The program may crash, leak memory, give wrong results, run slowly, corrupt 
files etc. These errors may be hard to spot and fix, especially when they happen in rare situations. We’ll be only considering semantic errors from now on.</p>

<p>If we spot a bug, how do we fix it? The first thing is to find a way to <strong>replicate</strong> it, i.e. find the exact steps we need to make with the program to make the bug appear (e.g. “in the menu press keys A and B 
simultaneously”, …). Next we need to trace and locate which exact line or piece of code is causing the bug. This can either be done with the help of specialized <a href="debugger.md">debuggers</a> such as <a href="gdb.md">gdb</a> or 
<a href="valgrind.md">valgrind</a>, but there’s usually a much easier way of using printing functions such as <code class="language-plaintext highlighter-rouge">printf</code>. (Still do check out the above mentioned debuggers, they’re very helpful.)</p>

<p>Let’s say your program crashes and you don’t know at which line. You simply put prints such as <code class="language-plaintext highlighter-rouge">printf("A\n");</code> and <code class="language-plaintext highlighter-rouge">printf("B\n);</code> at the beginning and end of a code you suspect might be causing the crash. Then 
you run the program: if <code class="language-plaintext highlighter-rouge">A</code> is printed but <code class="language-plaintext highlighter-rouge">B</code> isn’t, you know the crash happened somewhere between the two prints, so you shift the <code class="language-plaintext highlighter-rouge">B</code> print a little bit up and so on until you find exactly after which line <code class="language-plaintext highlighter-rouge">B</code> 
stops printing – this is the line that crashes the program. IMPORTANT NOTE: the prints have to have newline (<code class="language-plaintext highlighter-rouge">\n</code>) at the end, otherwise this method may not work because of output buffering.</p>

<p>Of course, you may use the prints in other ways, for example to detect at which place a value of variable changes to a wrong value. (<a href="assert.md">Asserts</a> are also good for keeping an eye on correct values of 
variables.)</p>

<p>What if the program isn’t exactly crashing but is giving wrong results? Then you need to trace the program step by step (not exactly line by line, but maybe function by function) and check which step has a problem 
in it. If for example your game AI is behaving stupid, you firstly check (with prints) if it correctly detects its circumstances, then you check whether it makes the correct decision based on the circumstances, 
then you check whether the pathfinding algorithm finds the correct path etc. At each step you need to know what the correct behavior should be and you try to find where the behavior is broken.</p>

<p>Knowing how to fix a bug isn’t everything, we also need to find the bugs in the first place. <strong><a href="testing.md">Testing</a></strong> is the process of trying to find bugs by simply running and using the program. Remember, 
testing can’t prove there are no bugs in the program, it can only prove bugs exist. You can do testing manually or automate the tests. Automated tests are very important for preventing so called <strong>[regressions]
(regression.md)</strong> (so the tests are called regression tests). Regression happens when during further development you break some of its already working features (it is very common, don’t think it won’t be happening 
to you). Regression test (which can simply be just a normal C program) simply automatically checks whether the already implemented functions still give the same results as before (e.g. if <em>sin(0) = 0</em> etc.). These 
tests should be run and pass before releasing any new version of the program (or even before any commit of new code).</p>

<p><a href="optimization.md">Optimization</a> is also a process of improving an already working program, but here we try to make the program more efficient – the most common goal is to make the program faster, smaller or 
consume less <a href="ram.md">RAM</a>. This can be a very complex task, so we’ll only mention it briefly.</p>

<p>The very basic thing we can do is to turn on automatic optimization with a compiler flag: <code class="language-plaintext highlighter-rouge">-O3</code> for speed, <code class="language-plaintext highlighter-rouge">-Os</code> for program size (<code class="language-plaintext highlighter-rouge">-O2</code> and <code class="language-plaintext highlighter-rouge">-O1</code> are less aggressive speed optimizations). Yes, it’s that simple, 
you simply add <code class="language-plaintext highlighter-rouge">-O3</code> and your program gets magically faster. Remember that <strong>optimizations against different resources are often antagonistic</strong>, i.e. speeding up your program typically makes it consume more memory 
and vice versa. You need to choose. Optimizing manually is a great art. Let’s suppose you are optimizing for speed – the first, most important thing is to locate the part of code that’s slowing down you program 
the most, so called <strong><a href="bottleneck.md">bottleneck</a></strong>. That is the code you want to make faster. Trying to optimize non-bottlenecks doesn’t speed up your program as a whole much; imagine you optimize a part of the 
code that takes 1% of total execution time by 200% – your program will only get 0.5% faster. Bottlenecks can be found using <a href="profiling.md">profiling</a> – measuring the execution time of different parts of the 
program (e.g. each function). This can be done manually or with tools such a <a href="gprof.md">gprof</a>. Once you know where to optimize, you try to apply different techniques: using algorithms with better [time complexity]
(time_complexity.md), using <a href="lut.md">look up tables</a>, optimizing <a href="cache.md">cache</a> behavior and so on. This is beyond the scope of this tutorial.</p>

<h2 id="final-program">Final Program</h2>

<p>Now is the time to write a final program that showcases what we’ve learned, so let’s write a quite simple but possibly useful <a href="hex_editor.md">hex viewer</a>. The program will allow us to interactively inspect bytes in 
any file, drawing their hexadecimal values along with their addresses, supporting one character commands to move within the file. If you want, you can take it and improve it as an exercise, for example by adding 
more viewing modes (showing decimal octal or ASCII values), maybe even allowing editing and saving the file. Here is the source code:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>/* Simple interactive hex file viewer. */

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

#define ROWS 10 // how many rows and columns to print at one screen
#define COLS 16

unsigned char *fileContent = NULL;   // this will contain the loaded file
unsigned long long fileSize = 0;     // size of fileContent in bytes
unsigned long long fileOffset = 0;   // current offset within the file
const char *fileName = NULL;

// Loads file with given name into fileContent, returns 1 on success, 0 on fail.
int loadFile(const char *fileToOpen)
{
  FILE *file = fopen(fileToOpen,"rb");

  if (file == NULL)
    return 0;

  fseek(file,0L,SEEK_END); // get to the end of the file
  fileSize = ftell(file);  // our position now says the size of the file
  rewind(file);            // get back to start of the file

  fileContent = malloc(fileSize);  // allocate memory to load the file into

  if (fileContent == NULL)
  {
    fclose(file); // don't forget to close the file
    return 0;
  }

  if (fread(fileContent,1,fileSize,file) != fileSize)
  {
    fclose(file);
    free(fileContent);
    return 0;
  }

  fclose(file);

  return 1;
}

// Call when loaded file is no longer needed.
void unloadFile(void)
{
  free(fileContent); // free the allocated memory
}

// Draws the current screen, i.e. hex view of the file at current offset.
void drawScreen(void)
{
  for (int i = 0; i &lt; 80; ++i) // scroll the old screen our of the view
    putchar('\n');

  printf("%s: %llu / %llu\n\n",fileName,fileOffset,fileSize);

  unsigned long long offset = fileOffset;

  for (int i = 0; i &lt; ROWS * COLS; ++i)
  {
    if (offset % COLS == 0)
      printf("%04X    ",(int) offset);

    if (offset &lt; fileSize)
      printf("%02X ",fileContent[offset]);
    else
      printf(".. ");

    offset++;

    if (offset % COLS == 0) // break line after each COLS values
      putchar('\n');
  }
}

int main(int argc, char **argv)
{
  if (argc &lt; 2)
  {
    puts("ERROR: please pass a file to open");
    return 1;
  }

  fileName = argv[1]; // (argv[0] is the name of our program, we want argv[1])

  if (!loadFile(fileName))
  {
    printf("ERROR: couldn't open the file \"%s\"\n",fileName);
    return 1;
  }

  int goOn = 1;

  while (goOn) // the main interactive loop
  {
    drawScreen();

    puts("\ntype command (w = end, s = start, a = back, d = next, q = quit)");

    char userInput = getchar();

    switch (userInput)
    {
      case 'q':
        goOn = 0;
        break;

      case 's':
        if (fileOffset + COLS &lt; fileSize)
          fileOffset += COLS;

          break;

      case 'w': 
        if (fileOffset &gt;= COLS)
          fileOffset -= COLS;
  
        break;

      case 'a': 
        fileOffset = 0;
        break;

      case 'd':
        fileOffset = ((fileSize - COLS) / COLS) * COLS; // aligns the offset
        break;

      default: 
        puts("unknown command, sorry");
        break;
    }
  }

  unloadFile();

  return 0;
}
</code></pre></div></div>

<p>To add a few comments: the program opens a file whose name it gets passed as a command line argument, so it is used as: <code class="language-plaintext highlighter-rouge">./hexview myfile</code>. We try to correctly perform all safety checks, e.g. if we actually get 
passed the file name, if we manage to open it and so on. Our program (a bit inefficiently) loads the whole file into memory (advanced programs only load parts of the file) – for this it first checks the file size, 
allocates sufficient memory for it with <code class="language-plaintext highlighter-rouge">malloc</code> (also checking for errors) and loads it there. Then we have a function to draw the current file view and inside the main program body we have an interactive loop 
that loads and handles user commands and issues the view drawing. That is basically it!</p>

<h2 id="bonus-introduction-to-graphics-ascii-ppm-sdl2-">Bonus: Introduction To Graphics (ASCII, PPM, SDL2, …)</h2>

<p>Let’s stress you should only get into graphics after you’ve written several purely command-line programs and are comfortable with the language. Don’t try graphics programming until you can easily work with 2D 
arrays, structs and so on. <a href="graphics.md">Graphics</a> is a huge topic of its own, there is so much we can’t cover here, remember this is just a quick, basic starting point for making pictures with C.</p>

<p>For start please note that:</p>

<ul>
  <li><strong>C itself doesn’t know anything about graphics</strong>. C is just trying to be a good programming language, it leaves the vast area of graphics for others to solve, therefore though you can try to avoid it (see 
below), typically you will use a third party <a href="library.md">library</a> to draw some real pixels to the screen, there isn’t a universal way of doing it, you have to choose specific solution based on what you want to 
achieve, what’s available etc.</li>
  <li><strong>By graphics we really just mean drawing <a href="pixel.md">pixels</a></strong>. Things like keyboard and mouse <a href="io.md">input</a> (which you need for anything <a href="interactivity.md">interactive</a> like <a href="game.md">games</a>), loading <a href="png.
md">PNG</a> pictures, playing sounds, loading and displaying 3D models, detecting <a href="collision.md">collisions</a> of virtual objects and so on won’t necessarily be covered here, it’s all too much to learn at once. We will ONLY 
be trying to show basic shapes on the screen.</li>
  <li>We’ll be doing things in simplified ways here, omitting common <a href="optimization.md">optimizations</a>, safety checks and so on. Just know that in practice things will be yet a bit more complex.</li>
</ul>

<p>So, how to actually do graphics? As said, graphics is a super wide topic, there is no <a href="silver_bullet.md">silver bullet</a>, all depends on what we really need. Consider the following:</p>

<ul>
  <li><strong>Need to quickly draw something quite basic (e.g. a graph)? <a href="kiss.md">Keep it simple</a> and just use <a href="ascii_art.md">ASCII art</a>.</strong> You can draw simple pictures to the console with ASCII art, i.e. you emulate real 
screen pixels with text characters. This is a nice, natural transition from text to graphics when studying programming, so you may start with this. The disadvantage is you can only draw very simple, rough and low 
resolution pictures, usually without colors, but you can animate them and make your program a little bit interactive. By doing things yourself you’ll also get an idea what graphics is about and will better 
understand why libraries you’ll use later work the way they do. A big advantage is that ASCII art graphics can be done without any library (there are libraries like <a href="ncurses.md">ncurses</a>, but you probably won’t 
need them) and will keep your program quite simple, nice and <a href="portability.md">portable</a>. You can use this for simple visualization, animations, games and so on.</li>
  <li><strong>Need to just produce one or two static pictures (e.g. function plot)? Output a picture file</strong>. You can make a C program that will simply save a picture to a file which you can open in any image viewer. For this 
you can use quite simple libraries but it is also possible to load and save simple formats without any libraries at all! You can very easily export bitmap images (e.g. <a href="ppm.md">PPM</a>, <a href="farbfeld.md">farbfeld</a>, …) 
as well as beautiful <a href="vector.md">vector</a> images (e.g. by exporting <a href="svg.md">SVG</a>) with curves, <a href="antialiasing.md">antialiasing</a>, fancy fonts and so on, you can auto-convert them with other tools to other formats and 
so on. This will suffice for many things like data visualizations, function plots, photo processing, even 3D rendering, while keeping your program highly <a href="portability.md">portable</a>, i.e. it will be usable 
everywhere, even on computers without any GUI or screen, it will be much less <a href="bloat.md">bloated</a>.</li>
  <li><strong>Need a fast, real time interactive program (e.g. a game)? Use a <a href="library.md">library</a> for that</strong>. If you want the “real deal”, i.e. interactive, fully colorful high-res graphics, e.g. for a serious game, you’ll 
typically have to use a library – in C this library is traditionally <a href="sdl.md">SDL2</a> (but there are many alternatives, e.g. <a href="sfml.md">SFML</a>, <a href="allegro.md">Allegro</a>, <a href="saf.md">SAF</a>, …). This is a bit more complex, 
so only go this way if you really must – you have to install the library, learn to use it and your program will become more bloated, less portable, bigger in size, harder to compile and so on.</li>
</ul>

<p>We will show an example of each of these approaches further on.</p>

<p>But first let’s quickly mention what graphics programming at this level is essentially about, i.e. the kind of “workflow” we’ll always try to implement:</p>

<p>The most essential thing is basically to be able to draw a pixel, i.e. set a color of one point in the picture. Once you can draw a single pixel, you can draw anything, just like to build any kind of house you have
 to be able to lay bricks – every shape is just some formation of pixels that you can construct with C code: a line is just a series of pixels one next to another, circle is a curved line, rectangle is just area 
 filled with pixels of some color and so on. So at the beginning we’ll just have some way of drawing a single pixel. Typically this can be e.g. a function drawPixel(x,y,color) – graphic libraries will normally 
 offer you a function like this, letting you draw pixels without actually caring about what magic is going on inside the function. (Sometimes you will also encounter a lower level way in which the library maps a 
 screen to memory and you will draw pixels by literally writing values to memory, i.e. with pointers or arrays.)
With the basic pixel drawing function we’ll draw our picture however we want – if we’re using a library, there may be helper functions and of course we can write our own functions too, for example drawLine(fromX,
fromY,toX,toY,color), drawText(x,y,text,size,color) and so on. The picture itself is just a virtual canvas, a computer memory holding numbers, typically a two dimensional array whose values are manipulated by the 
drawPixel function. At this point we are doing nothing else than changing values in memory.
At the end, once drawing is complete, we have to show (present) the picture. This is to say that when we’re drawing, the picture isn’t actually seen, it is only changing in memory, it is shown to the user only when
 it’s completed, i.e. when we issue a special command such as drawingDone(). Why can’t the picture just be shown at all times? In theory it can, but you encounter problems, imagine e.g. a game that quickly redraws
  the picture on the screen – here the user would see flickering, he might even see enemies show briefly behind a wall before the wall is actually drawn and so on. So a way to solve this is to do the drawing off 
 screen and only at the end say “now we’re done drawing, show the image” (for more details see double buffering).
Also note that usually there is some kind of management around graphic code, i.e. some initialization of the program’s window, setting its resolution, allocation of memory for the screen pixels, setting the pixel 
formats, callbacks and so on. Similarly at the end you often have to clean things up and as many graphic systems are based on events, you have to periodically check events like key presses, window resizes etc. 
Interactive programs will furthermore have an infinite loop (so called game loop) in which they check events, redraw the screen, wait for a while (to keep the right FPS) and so on. Libraries try to do many thing 
for you but you have to at least tell them some very basic things. So be prepared for a lot extra code.</p>

<p>Now let’s finally do this. We’ll set up some basic code for drawing a rectangle and try to draw it with different approaches.</p>

<p>The ASCII approach:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;

#define SCREEN_WIDTH 60
#define SCREEN_HEIGHT 25

char screen[SCREEN_WIDTH * SCREEN_HEIGHT]; // our virtual screen

// sets a single pixel at given coordinates
void drawPixel(int x, int y, char pixel)
{
  int index = y * SCREEN_WIDTH + x;

  if (index &gt;= 0 &amp;&amp; index &lt; SCREEN_WIDTH * SCREEN_HEIGHT)
    screen[index] = pixel;
}

// presents the drawn picture on the screen
void drawScreen(void)
{
  for (int i = 0; i &lt; 30; ++i) // shift old picture out of view
    putchar('\n');

  const char *p = screen;

  for (int y = 0; y &lt; SCREEN_HEIGHT; ++y)
  {
    for (int x = 0; x &lt; SCREEN_WIDTH; ++x)
    {
      putchar(*p);
      p++;
    }

    putchar('\n');
  }
}

// fills rectangle with given pixel value
void drawRectangle(int x, int y, int width, int height, char pixel)
{
  for (int j = 0; j &lt; height; ++j)
    for (int i = 0; i &lt; width; ++i)
      drawPixel(x + i,y + j,pixel);
}

int main(void)
{
  int quit = 0;
  int playerX = SCREEN_WIDTH / 2, playerY = SCREEN_HEIGHT / 2;

  while (!quit) // main game loop
  {
    drawRectangle(0,0,SCREEN_WIDTH,SCREEN_HEIGHT,'.'); // clear screen with dots
    drawRectangle(playerX - 2,playerY - 1,5,3,'X');    // draw player
    drawScreen();                                      // present the picture

    puts("enter command (w/s/a/d/q):");
    char input = getchar();

    switch (input)
    {
      case 'w': playerY--; break;
      case 's': playerY++; break;
      case 'a': playerX--; break;
      case 'd': playerX++; break;
      case 'q': quit = 1; break;
    }
  }

  return 0;
}
</code></pre></div></div>

<p>With this we have a simple interactive program that draws a dotted screen with rectangle that represents the player, you can compile it like any other program, it uses no external libraries. User can move the 
rectangle around by typing commands. There is a main infinite loop (this is the above mentioned <em>game loop</em>, a typical thing in interactive applications) in which we read the user commands and redraw the picture on 
the screen. Notice we have our basic <code class="language-plaintext highlighter-rouge">drawPixel</code> function as well as the <code class="language-plaintext highlighter-rouge">drawScreen</code> function for presenting the finished picture, we also have a helper <code class="language-plaintext highlighter-rouge">drawRectangle</code> function. The <code class="language-plaintext highlighter-rouge">screen</code> array represents our
 virtual picture (it is declared as one dimensional array but in reality it is treated as two dimensional by the <code class="language-plaintext highlighter-rouge">setPixel</code> function). As an exercise you can try to draw other simple shapes, for example horizontal 
and vertical lines, non-filled rectangles – if you’re brave enough you can also try a filled circle (hint: points inside a circle mustn’t be further away from the center than the circle radius).</p>

<p>Now let’s try to do something similar, but this time creating a “real picture” made of true pixels, exported to a file:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt;

#define SCREEN_WIDTH 640   // picture resolution
#define SCREEN_HEIGHT 480

unsigned char screen[SCREEN_WIDTH * SCREEN_HEIGHT * 3]; // screen, 3 is for RGB

// sets a single pixel at given coordinates
void drawPixel(int x, int y, int red, int green, int blue)
{
  int index = y * SCREEN_WIDTH + x;

  if (index &gt;= 0 &amp;&amp; index &lt; SCREEN_WIDTH * SCREEN_HEIGHT)
  {
    index *= 3;
    screen[index] = red;
    screen[index + 1] = green;
    screen[index + 2] = blue;
  }
}

// outputs the image in PPM format 
void outputPPM(void)
{
  printf("P6 %d %d 255\n",SCREEN_WIDTH,SCREEN_HEIGHT); // PPM file header

  for (int i = 0; i &lt; SCREEN_WIDTH * SCREEN_HEIGHT * 3; ++i)
    putchar(screen[i]);
}

// fills rectangle with given pixel
void drawRectangle(int x, int y, int width, int height, int red, int green,
  int blue)
{
  for (int j = 0; j &lt; height; ++j)
    for (int i = 0; i &lt; width; ++i)
      drawPixel(x + i,y + j,red,green,blue);
}

int main(void)
{
  drawRectangle(0,0,SCREEN_WIDTH,SCREEN_HEIGHT,128,128,128); // clear with grey
  drawRectangle(SCREEN_WIDTH / 2 - 32, SCREEN_HEIGHT / 2 - 32,64,64,255,0,0);
  outputPPM();
  return 0;
}
</code></pre></div></div>

<p>Wow, this is yet simpler! Although we have no interactivity now, we get a nice picture of a red rectangle on grey background, and don’t even need any library (not even the file library!). We just compile this and save the program output to a file, e.g. with <code class="language-plaintext highlighter-rouge">./program &gt; picture.ppm</code>. The picture we get is stored in <a href="ppm.md">PPM</a> format – a very simple format that basically just stores raw <a href="rgb.md">RGB</a> values and can be opened in many viewers and editors (e.g. <a href="gimp.md">GIMP</a>). Notice the similar functions like <code class="language-plaintext highlighter-rouge">drawPixel</code> – we only have a different parameter for the pixel (in ASCII example it was a single ASCII character, now we have 3 color values: red, green and blue). In the main program we also don’t have any infinite loop, the program is non-interactive.</p>

<p>And now finally to the more complex example of a fully interactive graphic using SDL2:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;SDL2/SDL.h&gt; // include SDL library

#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480

#define COLOR_WHITE 0xff // some colors in RGB332 format
#define COLOR_RED 0xe0

unsigned char _SDL_screen[SCREEN_WIDTH * SCREEN_HEIGHT]; 
SDL_Window *_SDL_window;
SDL_Renderer *_SDL_renderer;
SDL_Texture *_SDL_texture;
const unsigned char *_SDL_keyboardState;
int sdlEnd;

static inline void drawPixel(unsigned int x, unsigned int y, unsigned char color)
{
  if (x &lt; SCREEN_WIDTH &amp;&amp; y &lt; SCREEN_HEIGHT)
    _SDL_screen[y * SCREEN_WIDTH + x] = color;
}

void sdlStep(void)
{
  SDL_Event event;

  SDL_UpdateTexture(_SDL_texture,NULL,_SDL_screen,SCREEN_WIDTH);
  SDL_RenderClear(_SDL_renderer);
  SDL_RenderCopy(_SDL_renderer,_SDL_texture,NULL,NULL);
  SDL_RenderPresent(_SDL_renderer);

  while (SDL_PollEvent(&amp;event))
    if (event.type == SDL_QUIT)
      sdlEnd = 1;

  SDL_Delay(10); // relieve CPU for 10 ms
}

void sdlInit(void)
{
  SDL_Init(0);
  _SDL_window = SDL_CreateWindow("program",SDL_WINDOWPOS_UNDEFINED,
    SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH,SCREEN_HEIGHT,SDL_WINDOW_SHOWN);
  _SDL_renderer = SDL_CreateRenderer(_SDL_window,-1,0);
  _SDL_texture = SDL_CreateTexture(_SDL_renderer,SDL_PIXELFORMAT_RGB332,
    SDL_TEXTUREACCESS_STATIC,SCREEN_WIDTH,SCREEN_HEIGHT);
  _SDL_keyboardState = SDL_GetKeyboardState(NULL);
  SDL_PumpEvents();
}

void sdlDestroy(void)
{
  SDL_DestroyTexture(_SDL_texture);
  SDL_DestroyRenderer(_SDL_renderer); 
  SDL_DestroyWindow(_SDL_window); 
}

int sdlKeyPressed(int key)
{
  return _SDL_keyboardState[key];
}

void drawRectangle(int x, int y, int width, int height, unsigned char color)
{
  for (int j = 0; j &lt; height; ++j)
    for (int i = 0; i &lt; width; ++i)
      drawPixel(x + i,y + j,color);
}

int main(void)
{
  int playerX = SCREEN_WIDTH / 2, playerY = SCREEN_HEIGHT / 2;

  sdlInit();

  while (!sdlEnd) // main loop
  {
    sdlStep(); // redraws screen, refreshes keyboard etc.

    drawRectangle(0,0,SCREEN_WIDTH,SCREEN_HEIGHT,COLOR_WHITE);
    drawRectangle(playerX - 10,playerY - 10,20,20,COLOR_RED); // draw player

    // update player position:
    if (sdlKeyPressed(SDL_SCANCODE_D))
      playerX++;
    else if (sdlKeyPressed(SDL_SCANCODE_A))
      playerX--;
    else if (sdlKeyPressed(SDL_SCANCODE_W))
      playerY--;
    else if (sdlKeyPressed(SDL_SCANCODE_S))
      playerY++;
  }

  sdlDestroy();

  return 0;
}
</code></pre></div></div>

<p>This program creates a window with a rectangle that can be moved with the WSAD keys. To compile it you first need to install the SDL2 library – how to do this depends on your system (just look it up somewhere; on Debian like systems this will typically be done with <code class="language-plaintext highlighter-rouge">sudo apt-get install libsdl2-dev</code>), and then you also need to link SDL2 during compilation, e.g. like this: <code class="language-plaintext highlighter-rouge">gcc -O3 -o graphics_sdl2 -lSDL2 graphics_sdl2.c</code>.</p>

<p>This code is almost a bare minimum template for SDL that doesn’t even perform any safety checks such as validating creation of each SDL object (which in real code SHOULD be present, here we left it out for better 
clarity). Despite this the code is quite long with a lot of <a href="boilerplate.md">boilerplate</a>; that’s because we need to initialize a lot of stuff, we have to create a graphical window, a texture to which we will draw, 
we have to tell SDL the format in which we’ll represent our pixels, we also have to handle operating system events so that we get the key presses, to know when the window is closed and so on. Still in the end we 
are working with essentially the same functions, i.e. we have <code class="language-plaintext highlighter-rouge">drawPixel</code> (this time with pixel as a single char value because we are using the simple <a href="rgb332.md">332</a> format) and <code class="language-plaintext highlighter-rouge">drawRectangle</code>. This time 
<code class="language-plaintext highlighter-rouge">sdlStep</code> is the function that presents the drawn image on screen (it also does other things like handling the SDL events and pausing for a while to not heat up the CPU).</p>]]></content><author><name></name></author><summary type="html"><![CDATA[C]]></summary></entry><entry><title type="html">Binary</title><link href="https://promathx.github.io/Binary/" rel="alternate" type="text/html" title="Binary" /><published>2024-02-01T00:00:00+00:00</published><updated>2024-02-01T00:00:00+00:00</updated><id>https://promathx.github.io/Binary</id><content type="html" xml:base="https://promathx.github.io/Binary/"><![CDATA[<h1 id="binary">Binary</h1>

<p>The word binary in general refers to having two choices or “two of a thing”; in computer science binary refers to the base 2 numeral system, i.e. a system of writing numbers with only two symbols, usually 1s and 0s, 
which are commonly interpreted <em>true</em> vs <em>false</em>. We can write any number in binary just as we can with our everyday decimal system (which uses ten digits, as opposed to two), but binary is more convenient for computers 
because this system is easy to implement in electronics (a switch can be on or off, i.e. 1 or 0; systems with more digits were tried but unsuccessful, they failed miserably in reliability – see e.g. ternary computers). 
The word <em>binary</em> is also by extension used for non-textual computer files such as native executable programs or asset files for games.</p>

<p>One binary digit (a “place” for binary value in computer memory) can be used to store exactly 1 bit of information. We mostly use binary digits in two ways:</p>

<ol>
  <li>With single bits we represent basic <strong>logic</strong> values, i.e. <em>true</em> and <em>false</em>, and perform logic operations (e.g. AND, OR etc.) with so called <strong>Boolean algebra</strong>.</li>
  <li>By grouping multiple bits together we create a <strong>base-2 numeral system</strong> that behaves in the same way as our decimal system and can be used to record numbers. We can build this numeral system with the above mentioned
Boolean algebra, i.e. we extend our simple one bit system to multi bit system allowing to work not just with two values (<em>true</em> and <em>false</em>) but with many distinct values (whole numbers, from which we may later
construct fractions etc.). Thanks to this we can implement algebraic operations such as addition, multiplication, square roots etc.</li>
</ol>

<p>Of course the binary system didn’t appear from nowhere, people in ancient times used similar systems, e.g. the poet Pingala (200 BC) created a system that used two syllables for counting, old Egyptians used so called Eye 
of Horus, a unit based on power of two fractions etc. Thomas Harriot used something very similar to today’s binary in 1600s. It’s just that until computers appeared there wasn’t much practical use for it, so no one cared.</p>

<h2 id="boolean-algebra-truefalse-logic">Boolean Algebra (“True/False Logic”)</h2>

<p>In binary we start by working with single bits – each bit can hold two values, 1 and 0. We may see bits now like “simple numbers”, we’ll want to do operations with them, but they can only ever be one of the two values. 
Though we can interpret these values in any way – e.g. in electronics we see them as high vs low voltage – in mathematics we traditionally turn to using logic and interpret them as meaning <em>true</em> (1) and <em>false</em> (0). 
This will further allow us to apply all the knowledge and theory we have gathered about logic, such as formulas that allow us to simplify binary expressions etc.</p>

<p>Next we want to define “operations” we can perform on single bits – for this we use so called <strong>Boolean algebra</strong>, which is originally a type of abstract algebra that works with set and their operations such as 
conjunction, disjunction etc. Boolean algebra can be seen as a sort of simplified version of what we do in “normal” elementary school algebra – just as we can add or multiply numbers, we can do similar things with 
individual bits, we just have a bit different operations such as logic AND, logic OR and so on. Generally Boolean algebra can operate with more than just two values, however that’s more interesting to mathematicians; for 
us all we need now is a binary Boolean algebra – that’s what programmers have adopted for their field. It is the case that in context of computers and programming we implicitly understand Boolean algebra to be the one 
working with 1s and 0s, i.e. the binary version, so the word <strong>“boolean”</strong> is essentially used synonymously with “binary” around computers. Many programming languages have a data type called <code class="language-plaintext highlighter-rouge">boolean</code> or <code class="language-plaintext highlighter-rouge">bool</code> that 
allows represents just two values (<em>true</em> and <em>false</em>).</p>

<p>The very basic operations, or logic functions, of Boolean algebra are:</p>

<ul>
  <li><strong>NOT</strong> (negation, <code class="language-plaintext highlighter-rouge">!</code>): Done with single bit, turns 1 into 0 and vice versa.</li>
  <li><strong>AND</strong> (conjunction, <code class="language-plaintext highlighter-rouge">/\</code>): Done with two bits, yields 1 only if both input bits are 1, otherwise yields 0. This is similar to multiplication (1 * 1 = 1, 1 * 0 = 0, 0 * 1 = 0, 0 * 0 = 0) .</li>
  <li><strong>OR</strong> (disjunction, <code class="language-plaintext highlighter-rouge">\/</code>): Done with two bits, yields 1 if at least one of the input bits is 1, otherwise yields 0. This is similar to addition (1 + 1 = 1, 1 + 0 = 1, 0 + 1 = 1, 0 + 0 = 0).</li>
</ul>

<p>There are also other function such as XOR (exclusive OR, is 1 exactly when the inputs differ) and negated versions of AND and OR (NAND and NOR, give opposite outputs of the respective non-negated function). The functions 
are summed up in the following table (we all these kinds of tables <strong>truth tables</strong>):</p>

<table>
  <thead>
    <tr>
      <th>x</th>
      <th>y</th>
      <th>NOT x</th>
      <th>x AND y</th>
      <th>x OR y</th>
      <th>x XOR y</th>
      <th>x NAND y</th>
      <th>x NOR y</th>
      <th>x NXOR y</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>0</td>
      <td>0</td>
      <td>1</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>1</td>
      <td>1</td>
      <td>1</td>
    </tr>
    <tr>
      <td>0</td>
      <td>1</td>
      <td>1</td>
      <td>0</td>
      <td>1</td>
      <td>1</td>
      <td>1</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <td>1</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>1</td>
      <td>1</td>
      <td>1</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <td>1</td>
      <td>1</td>
      <td>0</td>
      <td>1</td>
      <td>1</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>1</td>
    </tr>
  </tbody>
</table>

<p>In fact there exists more functions with two inputs and one output (16 in total, computing this is left as exercise :]). However not all are named – we only use special names for the commonly used ones, mostly the ones 
in the table above.</p>

<p>An interesting thing is that we may only need one or two of these functions to be able to create all other function (this is called <em>functional completeness</em>); for example it is enough to only have <em>AND</em> and <em>NOT</em> 
functions together to be able to construct all other functions. Functions <em>NAND</em> and <em>NOR</em> are each enough by themselves to make all the other functions! For example <em>NOT x = x NAND x</em>, <em>x AND y = NOT (x NAND y) = (x 
NAND y) NAND (x NAND y)</em>, <em>x OR y = (x NAND x) NAND (y NAND y)</em> etc.</p>

<p>Boolean algebra further tells us some basic laws we can use to simplify our expressions with these functions, for example:</p>

<ul>
  <li>trivial laws:
    <ul>
      <li><em>x AND 0 = 0</em></li>
      <li><em>x OR 1 = 1</em></li>
      <li><em>x AND 1 = x</em></li>
      <li><em>x OR 0 = x</em></li>
      <li><em>x AND x = x</em></li>
      <li><em>x OR x = x</em></li>
      <li>commutativity of OR: <em>x OR y = y OR x</em></li>
      <li>commutativity of OR: <em>x AND y = y AND x</em></li>
      <li>associativity of AND: <em>x OR (x OR x) = (x OR x) OR x</em></li>
      <li>associativity of AND: <em>x AND (x AND x) = (x AND x) AND x</em></li>
      <li>…</li>
    </ul>
  </li>
  <li>distributive laws:
    <ul>
      <li><em>x AND (y OR z) = (x AND y) OR (x AND z)</em></li>
      <li><em>x OR (y AND z) = (x OR y) AND (x OR z)</em></li>
    </ul>
  </li>
  <li>De Morgan’s laws:
    <ul>
      <li>NOT (x AND y) = NOT(x) OR NOT(y)</li>
      <li>NOT (x OR y) = NOT(x) AND NOT(y)</li>
    </ul>
  </li>
  <li>…</li>
</ul>

<p>By combining all of these simple functions it is possible to construct not only operations with whole numbers and traditional algebra, but also a whole computer that renders 3D graphics and sends multimedia over the 
Internet.</p>

<h2 id="base-2-numeral-system">Base-2 Numeral System</h2>

<p>While we may use a single bit to represent two values, we can group more bits together and become able to represent more values; the more bits we group together, the more values we’ll be able to represent as possible 
combinations of the values of individual bits. The number of bits, or “places” we have for writing a binary number is called a number of bits or <strong>bit width</strong>. A bit width <em>N</em> allows for storing 2^<em>N</em> values – e.g. with 
2 bits we can store 2^2 = 4 values: 0, 1, 2 and 3, in binary 00, 01, 10 and 11. With 3 bits we can store 2^3 = 8 values: 0 to 7, in binary 000, 001, 010, 011, 100, 101, 110, 111. And so on.</p>

<p>At the basic level binary works just like the decimal (base 10) system we’re used to. While the decimal system uses powers of 10, binary uses powers of 2. Here is a table showing a few numbers in decimal and binary:</p>

<table>
  <thead>
    <tr>
      <th>decimal</th>
      <th>binary</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <td>1</td>
      <td>1</td>
    </tr>
    <tr>
      <td>2</td>
      <td>10</td>
    </tr>
    <tr>
      <td>3</td>
      <td>11</td>
    </tr>
    <tr>
      <td>4</td>
      <td>100</td>
    </tr>
    <tr>
      <td>5</td>
      <td>101</td>
    </tr>
    <tr>
      <td>6</td>
      <td>110</td>
    </tr>
    <tr>
      <td>7</td>
      <td>111</td>
    </tr>
    <tr>
      <td>8</td>
      <td>1000</td>
    </tr>
    <tr>
      <td>…</td>
      <td>…</td>
    </tr>
  </tbody>
</table>

<p><strong>Conversion to decimal</strong>: let’s see an example that utilizes the facts mentioned above. Let’s have a number that’s written as 10135 in decimal. The first digit from the right (5) says the number of 10^(0)s (1s) in the 
number, the second digit (3) says the number of 10^(1)s (10s), the third digit (1) says the number of 10^(2)s (100s) etc. Similarly if we now have a number <strong>100101</strong> in binary, the first digit from the right (1) says 
the number of 2^(0)s (1s), the second digit (0) says the number of 2^(1)s (2s), the third digit (1) says the number of 2^(2)s (4s) etc. Therefore this binary number can be converted to decimal by simply computing 1 * 2^0</p>
<ul>
  <li>0 * 2^1 + 1 * 2^2 + 0 * 2^3 + 0 * 2^4 + 1 * 2^5 = 1 + 4 + 32 = <strong>37</strong>.</li>
</ul>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>100101 = 1 + 4 + 32 = 37
||||||
\\\\\\__number of 2^0s (= 1s)
 \\\\\__number of 2^1s (= 2s)
  \\\\__number of 2^2s (= 4s)
   \\\__number of 2^3s (= 8s)
    \\__number of 2^4s (= 16s)
     \__number of 2^5s (= 32s)
</code></pre></div></div>

<p>To <strong>convert from decimal</strong> to binary we can use a simple algorithm that’s again derived from the above. Let’s say we have a number <em>X</em> we want to write in binary. We will write digits from right to left. The first 
(rightmost) digit is the remainder after integer division of <em>X</em> by 2. Then we divide the number by 2. The second digit is again the remainder after division by 2. Then we divide the number by 2 again. This continues 
until the number is 0. For example let’s convert the number 22 to binary: first digit = 22 % 2 = <strong>0</strong>; 22 / 2 = 11, second digit = 11 % 2 = <strong>1</strong>; 11 / 2 = 5; third digit = 5 % 2 = <strong>1</strong>; 5 / 2 = 2; 2 % 2 = <strong>0</strong>; 2 / 2 
= 1; 1 % 2 = <strong>1</strong>; 1 / 2 = 0. The result is <strong>10110</strong>.</p>

<p>NOTE: once we start grouping bits to create numbers, we typically still also keep the possibility to apply the basic Boolean operations to these bits. You will sometimes encounter the term <strong>bitwise</strong> operation which 
signifies an operation that works on the level of single bits by applying given function to bits that correspond by their position; for example a bitwise AND of values <code class="language-plaintext highlighter-rouge">1010</code> and <code class="language-plaintext highlighter-rouge">1100</code> will give <code class="language-plaintext highlighter-rouge">1000</code>.</p>

<p><strong>Operations with binary numbers</strong>: again, just as we can do arithmetic with decimal numbers, we can do the same with binary numbers, even the algorithms we use to perform these operations with pen and paper work 
basically the same. For example the following shows multiplication of <code class="language-plaintext highlighter-rouge">110</code> (6) by <code class="language-plaintext highlighter-rouge">11</code> (3) to get <code class="language-plaintext highlighter-rouge">10010</code> (18):</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>  110 *
   11
  ___
  110
 110
 ____
10010
</code></pre></div></div>

<p>All of these operations can be implemented just using the basic boolean functions – see logic circuits and CPUs.</p>

<p>In binary it is very simple and fast to divide and multiply by powers of 2 (1, 2, 4, 8, 16, …), just as it is simply to divide and multiple by powers of 10 (1, 10, 100, 1000, …) in decimal (we just shift the radix 
point, e.g. the binary number 1011 multiplied by 4 is 101100, we just added two zeros at the end). This is why as a programmer <strong>you should prefer working with powers of two</strong> (your programs can be faster if the computer 
can perform basic operations faster).</p>

<p><strong>Binary can be very easily converted to and from **hexadecimal and octal</strong> because 1 hexadecimal (octal) digit always maps to exactly 4 (3) binary digits. E.g. the hexadeciaml number F0 is 11110000 in binary (1111 is 
always equaivalent to F, 0000 is always equivalent to 0). This doesn’t hold for the decimal base, hence programmers often tend to avoid base 10.</p>

<p>We can work with the binary representation the same way as with decimal, i.e. we can e.g. write negative numbers such as -110101 or rational numbers (or even real numbers) such as 1011.001101. However in a computer 
memory there are no other symbols than 1 and 0, so we can’t use extra symbols such as <em>-</em> or <em>.</em> to represent such values. So if we want to represent more numbers than non-negative integers, we literally have to only use 
1s and 0s and choose a specific <strong>representation</strong>/<strong>format</strong>/encoding of numbers – there are several formats for representing e.g. signed (potentially negative) or rational (fractional) numbers, each with pros and 
cons. The following are the most common number representations:</p>

<ul>
  <li><strong>two’s complement</strong>: Allows storing integers, both positive, negative and zero. It is <strong>probably the most common representation</strong> of integers because of its great advantages: basic operations (+, -, *) are performed
exactly the same as with “normal” binary numbers, and there is no negative zero (which would be an inconvenience and waste of memory). Inverting a number (from negative to positive and vice versa) is done simply by inverting all the bits and adding 1. The leftmost bit signifies the number’s sign (0 = +, 1 = -).</li>
  <li><strong>sign-magnitude</strong>: Allows storing integers, both positive, negative and zero. It’s pretty straightforward: the leftmost bit in a number serves as a sign (0 means +, 1 means -) and the rest of the number is the distance from zero in “normal” representation. So e.g. a 4 bit number 0011 is 3 while 1011 is -3 (note that we have to know the bit width of the number here, e.g. on 8 bits -3 would be 10000011). The disadvantage is there are two values for zero (positive, 0000 and negative, 1000) which wastes a value and presents a computational inconvenience, and operations with these numbers are more complicated and slower (checking the sign requires extra code).</li>
  <li><strong>one’s complement</strong>: Allows storing integers, both positive, negative and zero. The leftmost bit signifies a sign, in the same way as with sign-magnitude, but numbers are inverted differently: a positive number is turned into negative (and vice versa) by inverting all bits. So e.g. 0011 is 3 while 1100 is -3 (again, bit width matters). The disadvantage is there are two values for zero (positive, 0000 and [negative], 1111) which wastes a value and presents a computational inconvenience, and some operations with these numbers may be more complex.</li>
  <li><strong>fixed point</strong>: Allows storing rational numbers (fractions), i.e. numbers with a radix point (such as 1101.011), which can also be positive, negative or zero. It works by imagining a radix point at some fixed position in the binary representation, e.g. if we have an 8 bit number, we may consider 5 leftmost bits to represent the whole part and 3 rightmost bits to be the fractional part (so e.g the number 11010110 represents 11010.110). The advantage here is extreme simplicity (we can use normal integer numbers as fixed point simply by imagining a radix point). The disadvantage may be low precision and small range of representable values.</li>
  <li><strong>floating point</strong>: Allows storing rational numbers in great ranges, both positive, negative and zero, plus some additional values such as infinity and <em>not a number</em>. It allows the radix point to be shifted which gives a potential for storing extremely big and extremely small numbers at the same time. The disadvantage is that float is extremely complex, bloated, wastes some values and for fast execution requires a special hardware unit (which most “normal” computers nowadays have, but are missing e.g. in some embedded systems).</li>
  <li>…</li>
</ul>

<p>As anything can be represented with numbers, binary can be used to store any kind of information such as text, images, sounds and videos. See data structures and file formats.</p>

<p><strong>Binary numbers can nicely encode sets</strong>: one binary number can be seen as representing a set, with each bit saying whether an object is or is not present. For example an 8 bit number can represent a set of whole 
numbers 0 to 7. Consider e.g. a value <em>S1 = 10000101</em> and <em>S2 = 01001110</em>; <em>S1</em> represents a set { 0, 2, 7 }, <em>S2</em> represents a set { 1, 2, 3, 6 }. This is natural and convenient, no bits are wasted on encoding order of 
numbers, only their presence or absence is encoded, and many set operations are trivial and very fast. For example the basic operations on sets, i.e. union, intersection, complement are simply performed with boolean 
operators OR, AND and NOT. Also checking membership, adding or removing numbers to the set etc. are very simple (left as an exercise for the reader lol; also another exercise – in a similar fashion, how would you encode 
a multiset. This is actually very useful and commonly used, for example chess engines often use 64 bit numbers to represent sets of squares on a chessboard.</p>]]></content><author><name></name></author><summary type="html"><![CDATA[Binary]]></summary></entry></feed>