Mente

c_language.htm

Notes on C Programming

Debugging

Run all commands with -Wall and Werror for good alerts.

Flags:

To debug in macOS use lldb with the program output.

To debug sanitizing the output you can use the clang compiler.

clang++ -O1 -g -fsanitize=address -fno-omit-frame-pointer -c example_UseAfterFree.cc

String Functions

Bits

unsigned char x = 42;
42 in base 2 is 101010 -> 2^5 + 2^3 + 2^1

unsigned char uses eight binary digits or bits. Known as a single byte. It has a range of [0,255]. It is stored as: 00101010

unsigned int y = 4 uses 4 bytes or 32 bits. So it is stored as: 00000000000000000000000000101010.

Binary, Octal, and Decimal

Values:

unsigned int a = 0736; // octal value
unsigned int y = 0x5E2; // hexadecimal value
unsigned int p = 0x12; // hexadecimal value
unsigned int q = 0xa8b; // A,B,C,D,E,F can be uppercase or lowercas

Char in C is limited:

Other types:

sizeof(expression) tells us how many bytes the type has. It returns a type size_t and can be specified with %zu

Operations

AND, XOR, OR, work like typical boolean operations

Left Shift <<

Shifts bit to the right and eliminates whatever extra bits we have. Adds N bits to the right.

uint8_t a = 0x02u;   // 0b 0000 0010 = 0x02
uint8_t b = a << 3; // 0b 0001 0000 = 0x10 
printf("After shift: 0x%x\n", b);

// Output
// After shift: 0x10

Right Shift >>

Adds N 0 bits to the left and pushes the ones in the right out.

uint8_t a = 0xAAu;   // 1010 1010 = 0xAA
uint8_t b = a >> 4; // 0000 1010 - 0x0A
printf("After shift: 0x%X\n", b);

// Output
// After shift: 0xA

Pointers

In C every single argument gets copied into parameters and the function uses a copy of the argument.

E.g., we write a home address on a piece of paper; and then copy it onto another piece of paper. We now have two points to that house.

We use pointers because it allows us to use indirection. Allow changes to be made without changing the reference.

Every byte in memory has a unique address.

Pass by value

#include <stdio.h>

void swapA(int x, int y)