You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

7.2 KiB

C Tutorial

This is a quick C tutorial.

You should probably know at least the completely basic ideas of programming before reading this (what's a programming language, source code, command line etc.). If you already know another language you should be just fine.

About C and Programming

C is

  • A programming language, i.e. a language that lets you express algorithms.
  • Compiled language (as opposed to interpreted), i.e. you have to compile the code you write (with compiler) in order to obtain a native executable program (a binary file that you can run directly).
  • Extremely fast and efficient.
  • Very widely supported and portable to almost anything.
  • Low level, i.e. there is relatively little abstraction and not many comfortable built-in functionality such as garbage collection, you have to write many things yourself, you will deal with pointers, endianness etc.
  • Considered hard, but in certain ways it's simple, it lacks bloat and bullshit of "modern" languages which is an essential thing. It will take long to learn but it's the most basic thing you should know if you want to create good software. You won't regret.
  • Not holding your hand, i.e. you may very easily "shoot yourself in your foot" and crash your program. This is the price for the language's power.
  • Very old, well established and tested by time.
  • Recommended by us for serious programs.

Programming in C works like this:

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

So, for writing the source code you'll need a text editor; any plain text editor will do but you should use some that can highlight C syntax -- this helps very much when programming and is practically a necessity. Ideal editor is vim but it's a bit difficult to learn so you can use something as simple as Gedit or Geany. We do NOT recommend using huge programming IDEs such as "VS Code" and whatnot. You definitely can NOT use an advanced document editor that can format text such as LibreOffice or that shit from Micro$oft, this won't work because it's not plain text.

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

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.

Last thing: there are multiple standards of C. Here we will be covering C99, but this likely doesn't have to bother you at this point.

First Program

Let's quickly try to compile a tiny program to test everything and see how everything works in practice.

Open your text editor and paste this code:

/* simple C program! */

#include <stdio.h> // include IO library

int main(void)
{
  puts("It works.");
  
  return 0;
}

Save this file and name it program.c. Then open a terminal emulator (or an equivalent command line interface), locate yourself into the directory where you saved the file (e.g. cd somedirectory) and compile the program with the following command:

gcc -o program program.c

The program should compile and the executable program should appear in the directory. You can run it with

./program

And you should see

It works.

written in the command line.

Now let's see what the source code means:

  • /* simple C program! */ is a comment, 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 /* and ends with */.
  • // include IO library is another comment, but this is a line comment, it starts with // and ends with the end of line.
  • #include <stdio.h> tells the compiler we want to include a library named stdio. This is a standard library with input output functions, we need it to be able to use the function puts later on. We can include more libraries if we want to. These includes are almost always at the very top of the source code.
  • int main(void) 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 main in basically every program -- inside it are commands that will be executed when the program is run. Note that the curly brackets that follow ({ and }) denote the block of code that belongs to this function, so we need to write our commands between these brackets.
  • puts("It works."); is a "command" for printing text strings to the command line (it's a command from the stdio 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 (puts, for put string), then there are left and right brackets (( and )) between which there are arguments to the command, in our case there is one, the text string "It works.". Text strings have to be put between quotes ("), 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 ; -- all "normal" commands in C have to end with a semicolon.
  • return 0; is another "command", it basically tells the operating system that everything was terminated successfully (0 is a code for success). This command is an exception in that it doesn't have to have brackets (( and )). This doesn't have to bother us too much now, let's just remember this will always be the last command in our program.

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 main function.

#include <stdio.h> // include the I/O library
// more libraries can be included here

int main(void)
{
  // write commands here
  
  return 0; // always the last command
}

TO BE CONTINUED

Variables, Data Types

Functions

Header Files, Libraries, Compilation

Advanced Data Types

Macros

Pointers

Recursion

Dynamic Allocation

Debugging

Advanced Stuff

Under The Hood