low-level and operating systems programming.

language-overview

C is a general purpose language primarily used in low-level programming such as operating system code. It was originally designed to help create Unix software by bell labs (Dennis Ritchie specifically), but nowadays is found in many operating systems and pieces of software.

Language Properties

  • Typing : Statically Typed, Weakly Typed
  • Paradigm: Procedural
  • Semi-colon based line endings (need to put a ';' after every statement)
  • Created: 1972

Useful Starter Resources

C programming language (book)

Tutorialspoint

learn-c

Comments/Documentation

The commenting system in C is pretty standard, below are examples of single and multiline comments.

// This is a single line comment

/**
This
is
a
multiline
comment
*/

Docstrings are possible (and recommended), but have no officially recognized structure. I have included an example of a function docstring with my preferred style, which is modelled after the python numpy style(there is also the one outlined here that is quite good).

Here is an example:

#include <math.h>

/** Calculates the area of a circle and returns it based on radius
* 
* Parameters
* ----------
* radius: 
*         The radius of the circle you want the area of.
* 
* Returns
* -------
* The area of a circle with the given radius as an int
*/
int area_of_circle(int radius){
  int area = M_PI * (radius*radius); // pi times radius squared
  return area;
  }

Variables & Typing

Since C is a statically typed and Weakly typed language; This means that you must declare variables types (int, float etc.) when you initialize variables, and that variable types can be manipulated more easily than strongly typed languages.

There are two syntactically distinct ways to create variables in C:

  1. Instantiate a variable, then assign a value:
int age;
age = 21;
  1. Instantiate a variable with a value:
int age = 21;

Since I mentioned that C is weakly typed here is an example of converting a float to an int in C:

float age = 21.45;

age = (int)age; // Now equal to the int 21

Functions

Functions in C require an explicit statement of each parameter type, as well as the return type (if there is one).

Generally the format is as follows:

/** Docstring goes here*/
return_type function_name(parameter_type parameter_name){
    ...         // Some code here
    return foo; // Return something of the correct return type
}

For example:

#include <math.h>
/** Calculates the area of a circle and returns it based on radius
* 
* Parameters
* ----------
* radius: 
*         The radius of the circle you want the area of.
* 
* Returns
* -------
* The area of a circle with the given radius as an int
*/
int area_of_circle(int radius){
  int area = M_PI * (radius*radius); // pi times radius squared
  return area;
  }

Running C Code

To run C code you will need a C compiler, here are a few possible options:

gcc (What I use)

clang

Assuming you installed gcc you can compile the code using:

gcc <filename>.c -o <executeable name>

This will then compile the code to a binary (.exe for windows, and regular binary for macOS & Linux) and run it.

Files

c_demo.c
/**Used to demo the C language, demonstrates: functions, variables, imports, string formatting etc. */ #include <stdio.h> // Used to print to stdin and stdout #include <math.h> // Contains the value of PI as M_PI; Used in area_of_circle function /** Calculates the area of a circle and returns it based on radius * * Parameters * ---------- * radius: * The radius of the circle you want the area of. * * Returns * ------- * The area of a circle with the given radius as an int */ int area_of_circle(int radius){ int area = M_PI * (radius*radius); // pi times radius squared return area; } /**Function that runs in the binary after compile.*/ int main(){ int radius = 5; // Setup variable to be used in next format string printf("\nThe area of a circle with radius: %d is %d\n", radius, area_of_circle(radius)); return 0; // Causes the function to exit }