This page looks best with JavaScript enabled

C Programming

 ·  ☕ 2 min read

C Programming

These notes are my working document to C programming. It includes basic concepts
and documentation on items of interest to me.

Style

This nice embedded C coding
standard.
It really helps to use tools such as clangformat
to help with automatic formatting.

Libraries Vs Executables

They are essentially the same thing. However a library is meant to
be run by another program and a exectuable runs on its own.
For example the library <stdio.h> is a dynamic shared library. And
a simple hello world executable that just is meant to be run manually
would be an executable example.

Here are some helpful resources for this

Static Libraries

Linking Libraries

This stackoverflow guide shows how to
link multiple static libraries together.

The command looks like this

1
ar -rcT libaz.a libabc.a libxyz.a

And luckily, it works just fine with the arm5 compiler

1
armar -rcT libaz.a libabc.a libxyz.a

Cmake Build System

There is so much to Cmake that I will create a new page just for cmake.

Shared Libraries

Macros!!

Stringization And Double Stringization

Single stringization can be done with a macro like this. It can be used to get
the name of variables.

1
2
3
4
5
6
7
8
#define S(x) #x

int world = 50;

// Print the name of the variable
printf("Hello " S(world)");

// output: "Hello world"

Double stringization can be done with created another macro additional to the
single stringization method. This can be used to stringize the value of the
variable instead of the name. Useful for integers.

1
2
3
4
5
6
7
8
9
#define S(x) #x
#define xS(x) S(x)

int world = 50;

// Print the value of the variable
printf("Hello " Sx(world)");

// output: "Hello 50"

Here is an example where I used this to help create MQTT paths. I wanted to have
a macro for the QoS as a number and as the string form.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#define BASE_PATH "ESP32_MQTT_TESTING/qos"
// Double "stringizing" trick to help append numeric macros with strings
#define S(x) #x
#define xS(x) S(x)

#define QoS0 0
#define QoS1 1
#define QoS2 2
#define TOPIC_PATH_QoS0 BASE_PATH xS(QoS0)
#define TOPIC_PATH_QoS1 BASE_PATH xS(QoS1)
#define TOPIC_PATH_QoS2 BASE_PATH xS(QoS2)

// this throws an error, because it is only single stringized
// #define TOPIC_PATH_QoS2 BASE_PATH S(QoS2)

Embed Lua In C

Check out the full page Using_C_With_Lua