From 2482161d49b9110b186afac746be719900a3935f Mon Sep 17 00:00:00 2001 From: Aney Date: Sat, 17 Oct 2020 16:36:27 +0100 Subject: [PATCH] New C references added --- C/10_conversion_casting.md | 8 ++++ C/11_memory.md | 34 +++++++++++++++++ C/12_array.md | 15 ++++++++ C/{functions.md => 13_pointers.md} | 0 C/1_variables.md | 47 ++++++++++++++++++++++++ C/{statements.md => 2_functions.md} | 0 C/4_structures.md | 43 ++++++++++++++++++++++ C/5_unions.md | 17 +++++++++ C/6_enumerators.md | 11 ++++++ C/7_statements.md | 43 ++++++++++++++++++++++ C/8_loops.md | 47 ++++++++++++++++++++++++ C/9_operators.md | 16 ++++++++ C/{preprocessor.md => A_preprocessor.md} | 0 C/{compiler.md => B_compiler.md} | 0 C/{source_files.md => C_source_files.md} | 0 C/variables.md | 0 16 files changed, 281 insertions(+) create mode 100644 C/10_conversion_casting.md create mode 100644 C/11_memory.md create mode 100644 C/12_array.md rename C/{functions.md => 13_pointers.md} (100%) create mode 100644 C/1_variables.md rename C/{statements.md => 2_functions.md} (100%) create mode 100644 C/4_structures.md create mode 100644 C/5_unions.md create mode 100644 C/6_enumerators.md create mode 100644 C/7_statements.md create mode 100644 C/8_loops.md create mode 100644 C/9_operators.md rename C/{preprocessor.md => A_preprocessor.md} (100%) rename C/{compiler.md => B_compiler.md} (100%) rename C/{source_files.md => C_source_files.md} (100%) delete mode 100644 C/variables.md diff --git a/C/10_conversion_casting.md b/C/10_conversion_casting.md new file mode 100644 index 0000000..bfa1e16 --- /dev/null +++ b/C/10_conversion_casting.md @@ -0,0 +1,8 @@ +# Conversion + + +# Casting + + (int) sizeof(x) + +Casts the value of sizeof(x) to an integer diff --git a/C/11_memory.md b/C/11_memory.md new file mode 100644 index 0000000..1e1ebe2 --- /dev/null +++ b/C/11_memory.md @@ -0,0 +1,34 @@ +# Memory + + int i = 121313; + +Tells the computer to put aside some memory for i, as an integer. + +This is stored at a certain memory address. + + %i; + printf("0x%p\n", &i); + +The value will then be stored in that memory and can be referenced with the variable name, or with a pointer. + +# Pointers + +Pointer's can reference a memory address, this is like a varialbe that stores a memory address; + + int * p = &i; + + int * p = 0; + +Pointers set to 0 have no memory address, but should be declared as such instead of with no initialisation, as no init could cause bugs and other issues. + +# Derefferenced Pointers + +Pointers can also be dereffernced to get the value being pointed to, istead of the memory address itself. + + int j = *p; + +j is a referrence of p's memory address, the actual value of the varialbe it's pointed to. + + *p = 34234; + +The derefferenced value of p can be altered, while keeping the same memory address through derefferencing. diff --git a/C/12_array.md b/C/12_array.md new file mode 100644 index 0000000..ae8cf64 --- /dev/null +++ b/C/12_array.md @@ -0,0 +1,15 @@ +# Array + +Arrays can be any data type, including self-defined structs + + int numbers[] = { 100, 200. 55. 91 }; + +Has storage amount equal to the amount of elements/values. + + int numbers[3]; + +Has a storage amount of 3 elements. + + numbers[0] + +Gets the first element stored in the array. As arrays start at 0, so the 2nd element will be `numbers[1]` diff --git a/C/functions.md b/C/13_pointers.md similarity index 100% rename from C/functions.md rename to C/13_pointers.md diff --git a/C/1_variables.md b/C/1_variables.md new file mode 100644 index 0000000..14bd013 --- /dev/null +++ b/C/1_variables.md @@ -0,0 +1,47 @@ +# Variables + +Declare then use variables + + (optional initialiser) e.g. `int hens = 5` + +Variables should be declared seperately. + +If variables aren't initialised it'll try to get some value from the stack, causing massive unprodictibiliy. Always initialse. + +## Types + +These types store a certain amount of data, in bits. + +- int +- float +- double +- char + +- unsigned +- short +- long + +## Signed and Unsigned + +integers are signed by default, allowing negatives and positives. + +Unsigned ints, etc. have all their bits for positive values. + + +## Local Variables + +These are only usable and accessible withing the scope of the current function + +## Static Varialbles + +Static variables retain their value within the function, regardless of how many function calls. + +static int hens; + +Static variables default their initialisation to 0. And are not re-initialised upon function calls. These variables retain their value until the program is restarted, etc. + +### Sharing static varialbes + +By declaring the static variable within the source code, not inside of a function it can be used by many functions. + +Global? diff --git a/C/statements.md b/C/2_functions.md similarity index 100% rename from C/statements.md rename to C/2_functions.md diff --git a/C/4_structures.md b/C/4_structures.md new file mode 100644 index 0000000..904eb97 --- /dev/null +++ b/C/4_structures.md @@ -0,0 +1,43 @@ +# Structures + +A collection of variables that create a new type, like classes/objects, but pure data. + +Keep related variables and values together. + + typedef struct Pixel{ + float X; + float Y; + } + +Declared as any other variable type + + Pixel p; + +With initialisation + + Pixel p = {10.0f, 25.0f}; + +If typedef not before the original struct, it'll need to be added. Structs are in a different namespace. + + struct Pixel p; + +You can assign and access members of the declared structs using a . + + float x_value = p.X; + + p.Y = 66.0f; + +## Layout and Padding + +C and C++ compilers require values in structs to be in order of sizeof, so shorts before ints, etc. +If this isn't the case the struct will take up more memory that needed. eg. + + short x; // 2 bytes, with 2 bytes of padding + int y; // 4 bytes, no padding + short z; // 2 bytes, 2 bytes of padding + // total 12 bytes + + short x; // 2 bytes + short z; // 2 bytes + int y; // 4 bytes + // total 8 bytes diff --git a/C/5_unions.md b/C/5_unions.md new file mode 100644 index 0000000..664a3a8 --- /dev/null +++ b/C/5_unions.md @@ -0,0 +1,17 @@ +# Unions + +Probably won't be used much, eh. + +Like a structure, but all members share the same (starting) memory address + + typedef union testUnion{ + int Int; + float Real; + } + +A union is the size of it's biggest member (in bytes) + +A union will be one of these types, so either an int or float depending on what it's set as. + + +This can be useful for APIs, or if a variable could be a number of types. More likely the former. diff --git a/C/6_enumerators.md b/C/6_enumerators.md new file mode 100644 index 0000000..250b2d1 --- /dev/null +++ b/C/6_enumerators.md @@ -0,0 +1,11 @@ +# Enumerators + +Integer values, starting with 0 unless otherwise defined. + + typedef enum { + Toby, + Bobby + } Staff; + + + int staffmember = Toby; // 0 diff --git a/C/7_statements.md b/C/7_statements.md new file mode 100644 index 0000000..1849174 --- /dev/null +++ b/C/7_statements.md @@ -0,0 +1,43 @@ +# Conditional Statements + +Code that gets executed + +# Conditional/Selection Statements + +## If Statement + +Evaluates a condition and executes different compound statements depending on the evaluation. + + if (!condition) statement; + +Ideally you want to wrap the code blocked statements in braces + + if (condition) { + statement; + } + else{ + statement2; + } + +You can also add more conditions if the first doesn't match, if the other conditiosn also don't match then the else will execute. + + if (condition) { + statment; + } + else if (condition2){ + statement2; + } + +C doesn't have an elseif, elif, etc. Instead it just performs another if statement after the else clause. + +## Switch statement + +Select from a number of different values/case depending on the switch value + + switch (eggs) { + case 0: printf("no eggs"); break; + case 1: printf("one egg"); break; + default: printf("default value if neither case match switch"); break; + } + +In C if the breaks aren't there, it'll execute code from that match. This includes all the cases after the match. diff --git a/C/8_loops.md b/C/8_loops.md new file mode 100644 index 0000000..9b0d205 --- /dev/null +++ b/C/8_loops.md @@ -0,0 +1,47 @@ +# Loops + +Loops, or iteration statements are executed repeatedly until their iteration has completed. + +## While + +While loops check a condition and execute the statement inside the loop if the condition is evaluated as true. + + while (condition) { + statement; + } + +To ensure you don't get infinite loops, the evaluation must eventually become false. + + while (count < 10) { + printf("%d \n", count); + count += 1; + } + +## Do + +Do loops, or Do while loops also execute the statement inside the loop. + +Unlike the while loop the Do loop will always execute the statment at least once, as it does the evaluation of the condition after the statements. + + do { + printf("%d \n", count); + count += 1; + } while (count < 10); + +This is useful in case the statement must be run, but could also be iterated after this first execution. Like a normal statement followed by a while loop, but cleaner and more efficient. + +## For + +Iterates through in a single condition. + +Defines the varialbe, sets the condition, and also the increment that should happen if the condition is met. + + for (int count = 10; count < 10; count +=1) { + statement; + } + +## Break and Continue + +Break can break out of statements, and loops + +Continue skips over the code, and returns to the loop continuing from the next value. diff --git a/C/9_operators.md b/C/9_operators.md new file mode 100644 index 0000000..1a2adb2 --- /dev/null +++ b/C/9_operators.md @@ -0,0 +1,16 @@ +# Operators + + + + - + * + / + () + + ++x + x+=1 + x=x+1 + + == + != + ! + diff --git a/C/preprocessor.md b/C/A_preprocessor.md similarity index 100% rename from C/preprocessor.md rename to C/A_preprocessor.md diff --git a/C/compiler.md b/C/B_compiler.md similarity index 100% rename from C/compiler.md rename to C/B_compiler.md diff --git a/C/source_files.md b/C/C_source_files.md similarity index 100% rename from C/source_files.md rename to C/C_source_files.md diff --git a/C/variables.md b/C/variables.md deleted file mode 100644 index e69de29..0000000