New C references added
parent
4e3faca5cf
commit
2482161d49
@ -0,0 +1,8 @@
|
||||
# Conversion
|
||||
|
||||
|
||||
# Casting
|
||||
|
||||
(int) sizeof(x)
|
||||
|
||||
Casts the value of sizeof(x) to an integer
|
||||
@ -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.
|
||||
@ -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]`
|
||||
@ -0,0 +1,47 @@
|
||||
# Variables
|
||||
|
||||
Declare then use variables
|
||||
|
||||
<type> <name> (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?
|
||||
@ -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
|
||||
@ -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.
|
||||
@ -0,0 +1,11 @@
|
||||
# Enumerators
|
||||
|
||||
Integer values, starting with 0 unless otherwise defined.
|
||||
|
||||
typedef enum {
|
||||
Toby,
|
||||
Bobby
|
||||
} Staff;
|
||||
|
||||
|
||||
int staffmember = Toby; // 0
|
||||
@ -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.
|
||||
@ -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.
|
||||
@ -0,0 +1,16 @@
|
||||
# Operators
|
||||
|
||||
+
|
||||
-
|
||||
*
|
||||
/
|
||||
()
|
||||
|
||||
++x
|
||||
x+=1
|
||||
x=x+1
|
||||
|
||||
==
|
||||
!=
|
||||
!
|
||||
|
||||
Loading…
Reference in New Issue