Programming in C for pets: Introduction

2026-03-30

One of the best choices to learn programming is plain C.

Why?

Why not C++? For entry level C++ is too complicated. For advanced level it's often unnecessary. In fact, C++ is a niche language. The practice proves that:

Personally I always hate to read tutorials. I want to immediately start working on something cool rather than run stupid examples. But I wrote in assembly quite enough before getting to C, and for the very beginner we have to study the only stupid example. We'll be studying it in four chapters, and then stupidity will end, and only cool stuff will be from then on.

Sources of information

An introduction to C can be found on Wikipedia: https://en.wikipedia.org/wiki/C_(programming_language).

The canonical tutorial is deemed to be a book written by Brian Kernigan and Dennis Ritchie The C Programming Language Well, you can try reading it, but you'll have to get latest issue.

The standard is published by ANSI and ISO/IEC and available only for money. Basically, it's necessary only for those who write compilers. Ordinary programmers do not need it that much. There are free drafts available at https://www.open-std.org/jtc1/sc22/wg14/

For everyday work the best manual is https://cppreference.net/w/c.html The site looks way too old school, but there's everything there.

And there's a manual system man in Linux. Sections of interest are 2 System calls, and 3 Library calls. So you can type in the terminal

man puts

and get comprehensive information.

The section number should be specified to avoid ambiguity. For instance,

man printf

shows a page for printf command from coreutils package. If you want C library function, you should type

man 3 printf

How to invoke a compiler

Thanks to simplicity of the language, there's always been a plenty of compilers for it. You can look at this list if you wish https://en.wikipedia.org/wiki/List_of_compilers#C_compilers

In Linux there are currently two major implementations: GCC и Clang. You can run them from the command line as gcc and clang respectively. You can have both installed in your system, and even more than both twos, but the default one can be invoked with cc command.

In other words, if you have no specific demands, you simply type cc myprogram.c, where myprogram.c is a file with the source code of your program.

Let's create such a file:

#include <stdio.h>

int main(int argc, char* argv[])
{
    puts("Meow, she-master!");
    return 0;
}

We don't get into details for now, but it's pretty clear that the program should print Meow, she-master! on the screen.

Let's compile our first program:

cc myprogram.c

On success it does not print anything, it just creates the file a.out in the current working directory. If we run this file

./a.out

then we see

Meow, she-master!

That's good, but why a.out? What if we already had such file with some necessary and useful data? And the compiler just forcibly overwrote it?

That's why we always should specify target file name. Well, for simple one-off things a.out is fine, but for bigger projects we need to instruct the compiler what we want.

And not only that.

By default compilers give the programmer too much freedom which leads to nasty errors. This is and old and notorious problem of the C language, actually this is its curse. Luckily, modern compilers can warn the programmer about potential problems. Moreover, they can treat warnings as errors. I highly recommend to take advantage of that feature.

I use the following command line options:

Also, I specify the highest level of optimization with -O3

The name of output file is specified with -o.

Putting all together:

cc --std=gnu23 -Wall -Wextra -pedantic -Werror -Wno-unused-parameter -O3 -o myprogram myprogram.c

I should note that compilers can analyze the code much, much deeper and they have options for that:

But I haven't used that this far. You just need to know they can do that.


Meow.

Pet has limited explanatory capabilities and might miss something. Litte kitties may ask questions on Mastodon and pet will improve this article.