notes on C

This commit is contained in:
Thomas Bishop 2025-12-03 17:59:30 +00:00
parent 43a90613e8
commit e0e5cdef0c
5 changed files with 166 additions and 22 deletions

41
zk/Arrays_in_C.md Normal file
View file

@ -0,0 +1,41 @@
---
tags:
- C
---
# Arrays in C
To declare an array in C, you must specify:
- the data type of the variables it will store
- the size (in square brackets)
- the array elements (in curly brackets)
Thus:
```c
int some_numbers[3] = {1, 2, 3};
```
> Array element individuation is the same as JavaScript, as is a standard `for`
> loop.
## Calculating the length of an array
Determining the length of an array is a fairly involved process compared to
higher-level languages. The process:
- Get the size of the array **in bytes**
- Divide this by the size of the first array element
So:
```c
int some_numbers[3] = {1, 2, 3};
int length = sizeof(some_numbers) / sizeof(arr[0]) // 3
```
What is happening under the hood:
The array has three `int` values. The normal byte size for a single `int` is 4
bytes (32 bits). 4 x 3 is 12. 12/4 is 3.

83
zk/Char_data_type_in_C.md Normal file
View file

@ -0,0 +1,83 @@
---
tags:
- C
---
# The `char` data type in C
> A `char` is simply a 1-byte integer type that happens to be commonly used for
> storing characters
Don't get fixated on `char` _designating a character_. While it is mostly used
for this, it is better understood as the _smallest integer type_ C provides (1
byte). As C is so low-level there is a closer connection to data types just
being binary numbers. Hence `h`, `!` and `6` are all stored as 8-bit binary
numbers.
As `char` uses 8 bits, you can represent/encode $2^8$ values (256).
A _signed_ char (which is the default in C) allows for negative values hence it
can be used to [encode](./Binary_encoding.md) the numbers -128 - 127.
An _unsigned_ char gives you a greater positive range, 0 - 255.
Now, when you actually want to store a textual character, the number
corresponding to the `char` is looked up in the
[ASCII](./Binary_encoding_of_text.md) table, e.g. `65` would be `A`.
This is the only difference. In both cases a `char` is an 8-bit number, it's
just with actual characters, you are running it through ASCII.
This can be demonstrated by using the format specifiers for `char` and `int`
against a `char`:
```c
char a_char = 65;
printf("%i\n", aChar) // 65
print("%c\n", aChar) // A
```
## Strings
Unlike higher-level languges like Python, there is no independent string type in
C: **strings are just arrays of `char`**.
In order for the array of `char` to be understood as a string, the convention is
to apply a null terminator ('\0') as the final element.
Thus the string 'Thomas' would be declared:
```c
char name[] = "Thomas"
```
In memory this would actually be:
```
'T' 'h' 'o' 'm' 'a' 's' '\0'
```
With each char corresponding to an ASCII number apart from the null which would
just be `0000 0000`.
### String literals
The easiest way to declare a string as a `char` array is to use the format we've
already used which is string literal notation:
```c
char word[] = "Hello";
```
The null terminator is implied in this form, you do not need to add it manually.
Also the length is fixed to the length of the string (in this case `6` for the
five actual characters plus the null).
### Non-literal string declaration
The more formal way of doing this would be:
```c
char word[6] = {'H', 'e', 'l', 'l', 'o', '\0'}
```

View file

@ -0,0 +1,25 @@
---
tags:
- C
---
# Entry point to C programs
```c
int main(void)
{
// Instructions
}
```
The braces wrap all code that comprises the program.
The return type is `int`. It receives no arguments so the arguments are `void`.
## Why does it return an `int`?
It returns an `int` because the main function returns a success code: `0` for
success and `1` for error.
As C was divised to create Unix, this fits in with other Unix-like OS behaviour
such as bash scripts returning `0`/`1`.

View file

@ -7,15 +7,21 @@ created: Thursday, February 29, 2024 | 17:41
# Format specifiers in C # Format specifiers in C
Format specifiers define the type of data to be printed on standard output. You Format specifiers define the type of data to be printed or interpolated in
need to use format specifiers whenever you are standard output. You need to use format specifiers whenever you are
[printing-values-in-c](printing-values-in-c.md). [printing values in C](./Printing_values_in_C.md).
For example:
```c
int age = 37
printf("My age is %i", age)
```
| Specifier | Usage | | Specifier | Usage |
| --------- | -------------------------- | | --------- | ------- |
| `%c` | single character | | `%c` | char |
| `%s` | string | | `%s` | string |
| `%n` | nothing | | `%n` | nothing |
| `%d` | integer (assuming base 10) | | `%i` | integer |
| `%f` | float | | `%f` | float |
| `%%` | The percent sign itself |

View file

@ -1,11 +0,0 @@
---
id: r4i16p1x
tags: [C]
created: Thursday, February 29, 2024 | 17:20
---
# Variables in C
```c
int age = 25
```