<p>Hi, my name is Garjola and you are listening to a contribution to
HPR. This is episode 3 of my programming language series and is entitled
“Getting started with the C programming language”.</p>
<p>I am not going to teach you C, but just whet your appetite.</p>
<h2 id="intro">1 Intro</h2>
<ul>
<li>What C is useful for
<ul>
<li>Systems programming</li>
<li>Number crunching</li>
<li>Graphics</li>
<li>Embedded systems
<ul>
<li>Arduino</li>
</ul></li>
</ul></li>
<li>Advantages
<ul>
<li>speed</li>
<li>fine grained control of the memory management</li>
<li>close to the metal</li>
<li>the portable assembly language</li>
</ul></li>
<li>Drawbacks
<ul>
<li>fine grained control of the memory management</li>
<li>close to the metal</li>
<li>the portable assembly language</li>
</ul></li>
</ul>
<h2 id="history-of-the-language">2 History of the language</h2>
<ul>
<li><p>developed by Ken Thompson and Dennis Ritchie between 1969 and
1973 at AT&amp;T Bell Labs.</p></li>
<li><p>The origin of C is closely tied to the development of the Unix
operating system, originally implemented in assembly language on a PDP-7
by Ritchie and Thompson, incorporating several ideas from colleagues.
Eventually they decided to port the operating system to a PDP-11. B’s
inability to take advantage of some of the PDP-11’s features, notably
byte addressability, led to the development of an early version of
C.</p></li>
</ul>
<h2 id="uses">3 Uses</h2>
<p>C is often used for “system programming”, including implementing
operating systems and embedded system applications, due to a combination
of desirable characteristics such as code portability and efficiency,
ability to access specific hardware addresses, ability to pun types to
match externally imposed data access requirements, and low run-time
demand on system resources. C can also be used for website programming
using CGI as a “gateway” for information between the Web application,
the server, and the browser. Some reasons for choosing C over
interpreted languages are its speed, stability, and near-universal
availability.</p>
<p>One consequence of C’s wide availability and efficiency is that
compilers, libraries, and interpreters of other programming languages
are often implemented in C. The primary implementations of Python
(CPython), Perl 5, and PHP are all written in C.</p>
<p>Due to its thin layer of abstraction and low overhead, C allows
efficient implementations of algorithms and data structures, which is
useful for programs that perform a lot of computations.</p>
<p>C is sometimes used as an intermediate language by implementations of
other languages. This approach may be used for portability or
convenience; by using C as an intermediate language, it is not necessary
to develop machine-specific code generators.</p>
<p>C has also been widely used to implement end-user applications, but
much of that development has shifted to newer languages.</p>
<h2 id="characteristics-of-the-language">4 Characteristics of the
language</h2>
<ul>
<li>imperative, compiled, static, weakly typed</li>
<li>structured programming
<ul>
<li>Uses functions but it is not functional, but rather procedural</li>
<li>control flow if/else, for, while</li>
<li>curly braces, semi-colons</li>
</ul></li>
<li>all parameters are passed by value and references are simulated
using pointers</li>
<li>A program is a function called main</li>
</ul>
<h2 id="tooling-and-environment">5 Tooling and environment</h2>
<ul>
<li>editor</li>
<li>compiler
<ul>
<li>makefiles for convenience, although higher level tools such as
autoconf/automake or cmake exist</li>
</ul></li>
<li>debugger</li>
<li>IDEs</li>
<li>gnu tools for everything
<ul>
<li>emacs, pico, gedit, vi</li>
<li>gcc, clang</li>
<li>gnumake</li>
<li>gdb, xgdb, ddd</li>
<li>kdevelop</li>
</ul></li>
</ul>
<h2 id="hello-world">6 Hello World</h2>
<pre><code>#include &lt;stdio.h&gt;

int main(void) {
    printf(&quot;hello, world\n&quot;);
    return 0;
}</code></pre>
<h2 id="how-to-make-a-c-program">7 How to make a C program</h2>
<ul>
<li>Write your main function into a file called myprogram.c</li>
<li>compile your program
<ul>
<li><code>gcc -o myprogram myprogram.c</code></li>
</ul></li>
<li>if you use other libraries than C’s standard library, you will need
to use a linker, like <code>ld</code></li>
</ul>
<p>Examples taken from <a
href="https://www.network-theory.co.uk/docs/gccintro/"><em>An
Introduction to GCC</em></a> by Brian J. Gough, foreword by Richard M.
Stallman</p>
<p>The classic example program for the C language is Hello World. Here
is the source code for our version of the program:</p>
<pre><code>#include &lt;stdio.h&gt;

int main (void) { printf (&quot;Hello, world!\n&quot;); return 0; }</code></pre>
<p>We will assume that the source code is stored in a file called
<code>'hello.c'</code>.</p>
<p>To compile the file <code>'hello.c'</code> with <code>gcc</code>, use
the following command:</p>
<pre><code>$ gcc -Wall hello.c -o hello</code></pre>
<p>To run the program, type the path name of the executable like
this:</p>
<pre><code>$ ./hello
Hello, world!</code></pre>
<h2 id="pointers">8 Pointers!</h2>
<p>C supports the use of pointers, a type of reference that records the
address or location of an object or function in memory. Pointers can be
dereferenced to access data stored at the address pointed to, or to
invoke a pointed-to function. Pointers can be manipulated using
assignment or pointer arithmetic. The run-time representation of a
pointer value is typically a raw memory address (perhaps augmented by an
offset-within-word field), but since a pointer’s type includes the type
of the thing pointed to, expressions including pointers can be
type-checked at compile time.</p>
<h2 id="the-standard-library">9 The standard library</h2>
<ul>
<li>just use <code>man</code>!</li>
</ul>
<p>On Unix-like systems, the authoritative documentation of the actually
implemented API is provided in form of man pages. On most systems, man
pages on standard library functions are in section 3; section 7 may
contain some more generic pages on underlying concepts (e.g. man 7
<code>math_error</code> in Linux).</p>
<pre><code>apropos sqrt | grep \(3\)

man 3 sqrt

man 3 qsort history</code></pre>
<ul>
<li>the not-so-standard libraries
<ul>
<li>gsl</li>
<li>gtk</li>
<li>X</li>
</ul></li>
</ul>
<h2 id="languages-of-the-c-family">10 Languages of the C family</h2>
<ul>
<li>C++, ObjectiveC, Java, C#, Go</li>
</ul>
<h2 id="resources">11 Resources</h2>
<ul>
<li><a href="https://www.nongnu.org/c-prog-book/">Learning GNU C by
Ciaran O’Riordan.</a></li>
</ul>
