mirror of
https://github.com/LeCoupa/awesome-cheatsheets.git
synced 2026-01-29 14:48:01 -08:00
Compare commits
9 Commits
ece2cd59e0
...
5587651f7f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5587651f7f | ||
|
|
559d03ecf3 | ||
|
|
bea751612a | ||
|
|
984b1b0282 | ||
|
|
8e3299d2df | ||
|
|
1c94e54c7c | ||
|
|
d7fdfb6745 | ||
|
|
ca5e153c6d | ||
|
|
82a8d0e5b4 |
@@ -25,7 +25,7 @@ Feel free to take a look. You might learn new things. They have been designed to
|
||||
|
||||
#### Imperative
|
||||
|
||||
- [C](languages/C.txt)
|
||||
- [C](languages/C.md)
|
||||
- [C#](languages/C%23.txt)
|
||||
- [Go](languages/golang.md)
|
||||
- [Java](languages/java.md)
|
||||
|
||||
232
languages/C.md
Normal file
232
languages/C.md
Normal file
@@ -0,0 +1,232 @@
|
||||
#### main() Function
|
||||
|
||||
* The main() function is the starting point of the program: `int main (int argc, char *argv[])`
|
||||
* The return type of the main() function is an integer (type int) and it is known as the `return value` of the program.
|
||||
* As a rule of thumb, `value 0 means success while non-zero means an error conditions.`
|
||||
|
||||
---
|
||||
|
||||
### Include Files
|
||||
|
||||
* The purpose of these files is to tell the compiler about the `existence of external functions which the source code will make use of.`
|
||||
|
||||
---
|
||||
|
||||
### Preprocessor directives:
|
||||
|
||||
| Directive | Description |
|
||||
|-----------------------------|----------------------------------------------------|
|
||||
| `#include "mine.h"` | Search current working directory first. |
|
||||
| `#include <stdio.h>` | Search command line directory, then the system. |
|
||||
| `#define TRUE 1` | Macro substitution, usually use capitals. |
|
||||
| `#define min(a,b)` | Macro substitution with parameters. |
|
||||
| `#define abs(a)` | Macro substitution. |
|
||||
| `#define note /* comment */` | This comment gets inserted every time `note` appears. |
|
||||
| `backslash \ at end of a line` | Means continue the line. |
|
||||
| `#undef TRUE` | Undefines a previously defined macro name. |
|
||||
| `#error` | Stop compiling at this point. |
|
||||
| `#if expression` | Conditional compilation, starts an `if` structure. |
|
||||
| `#elif expression` | Else if expression != 0, compile the following code. |
|
||||
| `#else` | Else, compile the following code. |
|
||||
| `#endif` | End of conditional compiling. |
|
||||
| `#ifdef macroname` | Like `#if`, compiles if `macroname` is defined. |
|
||||
| `#ifndef macroname` | Like `#if`, compiles if `macroname` is undefined. |
|
||||
| `#line number [filename]` | Set the origin for `__LINE__` and `__FILE__`. |
|
||||
| `#pragma` | Gives the compiler commands. |
|
||||
|
||||
---
|
||||
|
||||
### Create and execute a program
|
||||
|
||||
In Linux systems:
|
||||
|
||||
1. Open up a terminal (Ctrl + Alt + T)
|
||||
|
||||
2. Create the program: nano nameProgram.c
|
||||
|
||||
3. Write the program and save it
|
||||
|
||||
4. gcc -o nameExecutable nameProgram.c (eg: gcc -o nameProgram_output nameProgram.c)
|
||||
|
||||
---
|
||||
|
||||
### Reserved words (32)
|
||||
|
||||
| Term | Description |
|
||||
|------------|------------------------------------------------------------|
|
||||
| `auto` | Optional local declaration |
|
||||
| `break` | Used to exit a loop and used to exit a `switch` statement |
|
||||
| `case` | Choice in a `switch` statement |
|
||||
| `char` | Basic declaration of a type character |
|
||||
| `const` | Prefix declaration meaning the variable cannot be changed |
|
||||
| `continue` | Go to the bottom of a loop in `for`, `while`, and `do` loops |
|
||||
| `default` | Optional last case of a `switch` statement |
|
||||
| `do` | Executable statement, `do-while` loop |
|
||||
| `double` | Basic declaration of double precision floating point |
|
||||
| `else` | Executable statement, part of an "if" structure |
|
||||
| `enum` | Basic declaration of enumeration type |
|
||||
| `extern` | Prefix declaration meaning the variable is defined externally |
|
||||
| `float` | Basic declaration of floating point |
|
||||
| `for` | Executable statement, `for` loop |
|
||||
| `goto` | Jump within a function to a label |
|
||||
| `if` | Executable statement |
|
||||
| `int` | Basic declaration of an integer |
|
||||
| `long` | Prefix declaration applying to many types |
|
||||
| `register` | Prefix declaration meaning to keep a variable in a register |
|
||||
| `return` | Executable statement with or without a value |
|
||||
| `short` | Prefix declaration applying to many types |
|
||||
| `signed` | Prefix declaration applying to some types |
|
||||
| `sizeof` | Operator applying to variables and types, gives size in bytes |
|
||||
| `static` | Prefix declaration to make a local variable static |
|
||||
| `struct` | Declaration of a structure, like a record |
|
||||
| `switch` | Executable statement for cases |
|
||||
| `typedef` | Creates a new type name for an existing type |
|
||||
| `union` | Declaration of variables that share the same memory locations |
|
||||
| `unsigned` | Prefix declaration applying to some types |
|
||||
| `void` | Declaration of a typeless variable |
|
||||
| `volatile` | Prefix declaration meaning the variable can be changed at any time |
|
||||
| `while` | Executable statement, `while` loop or `do-while` loop |
|
||||
|
||||
---
|
||||
|
||||
### Basic types
|
||||
|
||||
| Type | Description |
|
||||
|---------|--------------------------------------------------------|
|
||||
| `char` | Character type, usually one byte (a string is an array of `char`) |
|
||||
| `int` | Integer type, usually 2 or 4 bytes (default) |
|
||||
| `float` | Floating-point type, usually 4 bytes |
|
||||
| `double`| Floating-point type, usually 8 bytes |
|
||||
| `void` | No type, typeless |
|
||||
| `enum` | Enumeration type (user defines the type name) |
|
||||
|
||||
---
|
||||
|
||||
### Type modifiers, prefix for basic types
|
||||
|
||||
| Modifier | Description |
|
||||
|------------|--------------------------------------------------------|
|
||||
| `signed` | Has a sign (default) |
|
||||
| `unsigned` | No sign bit in the variable |
|
||||
| `long` | Longer version of a type (e.g., `long int`) |
|
||||
| `short` | Shorter version of a type (e.g., `short int`) |
|
||||
| `const` | Variable cannot be modified or stored into |
|
||||
|
||||
---
|
||||
|
||||
### Storage Types
|
||||
|
||||
| Prefix | Description |
|
||||
|-----------|-------------------------------------------------------|
|
||||
| `auto` | Local variable (default) |
|
||||
| `static` | Permanent, exists beyond function scope (not `auto`) |
|
||||
| `volatile`| Can change from external influences |
|
||||
| `extern` | Variables are defined elsewhere, externally |
|
||||
| `register`| Suggests that the variable should be stored in a register if possible |
|
||||
|
||||
---
|
||||
### Operators
|
||||
|
||||
| Operator | Description |
|
||||
|----------|----------------------------------------------------|
|
||||
| `( )` | Grouping parenthesis, function call |
|
||||
| `[ ]` | Array indexing, also `[ ][ ]` etc. |
|
||||
| `->` | Selector, structure pointer |
|
||||
| `.` | Select structure element |
|
||||
| `!` | Relational not, complement, `!a` yields true or false |
|
||||
| `~` | Bitwise not, ones complement, `~a` |
|
||||
| `++` | Increment, pre or post to a variable |
|
||||
| `--` | Decrement, pre or post to a variable |
|
||||
| `-` | Unary minus, `-a` |
|
||||
| `+` | Unary plus, `+a` |
|
||||
| `*` | Indirect, the value of a pointer, `*p` is the value at pointer `p` address |
|
||||
| `&` | The memory address, `&b` is the memory address of variable `b` |
|
||||
| `sizeof` | Size in bytes, `sizeof a` or `sizeof (int)` |
|
||||
| `(type)` | Cast, explicit type conversion, `(float)i`, `(*fun)(a, b)`, `(int*)x` |
|
||||
| `*` | Multiply, `a * b` |
|
||||
| `/` | Divide, `a / b` |
|
||||
| `%` | Modulo, `a % b` |
|
||||
| `+` | Add, `a + b` |
|
||||
| `-` | Subtract, `a - b` |
|
||||
| `<<` | Shift left, left operand is shifted left by right operand bits |
|
||||
| `>>` | Shift right, left operand is shifted right by right operand bits |
|
||||
| `<` | Less than, result is true or false, `a < b` |
|
||||
| `<=` | Less than or equal, result is true or false, `a <= b` |
|
||||
| `>` | Greater than, result is true or false, `a > b` |
|
||||
| `>=` | Greater than or equal, result is true or false, `a >= b` |
|
||||
| `==` | Equal, result is true or false, `a == b` |
|
||||
| `!=` | Not equal, result is true or false, `a != b` |
|
||||
| `&` | Bitwise and, `a & b` |
|
||||
| `^` | Bitwise exclusive or, `a ^ b` |
|
||||
| | | Bitwise or, `a` | `b` |
|
||||
| `&&` | Relational and, result is true or false, `a < b && c >= d` |
|
||||
| | | | Relational or, result is true or false, `a < b` | | `c >= d` |
|
||||
| `?` | Ternary conditional, `exp1 ? exp2 : exp3`, result is `exp2` if `exp1` is not 0, else result is `exp3` |
|
||||
| `=` | Store |
|
||||
| `+=` | Add and store |
|
||||
| `-=` | Subtract and store |
|
||||
| `*=` | Multiply and store |
|
||||
| `/=` | Divide and store |
|
||||
| `%=` | Modulo and store |
|
||||
| `<<=` | Shift left and store |
|
||||
| `>>=` | Shift right and store |
|
||||
| `&=` | Bitwise and and store |
|
||||
| `^=` | Bitwise exclusive or and store |
|
||||
| | = | Bitwise or and store |
|
||||
| `,` | Separator, as in `(y = x, z = ++x)` |
|
||||
|
||||
---
|
||||
### Operator precedence
|
||||
|
||||
#### More precedence
|
||||
|
||||
| Associativity | Operators |
|
||||
|---------------|---------------------------------------------------|
|
||||
| LR | `( )`, `[ ]`, `->`, `.`, `x++`, `x--` |
|
||||
| RL | `!`, `~`, `-`, `+`, `++x`, `--x`, `*`, `&`, `sizeof (type)` |
|
||||
| LR | `*`, `/`, `%` |
|
||||
| LR | `+`, `-` |
|
||||
| LR | `<<`, `>>` |
|
||||
| LR | `<`, `<=`, `>`, `>=` |
|
||||
| LR | `==`, `!=` |
|
||||
| LR | `&` |
|
||||
| LR | `^` |
|
||||
| LR | | |
|
||||
| LR | `&&` |
|
||||
| LR | | | |
|
||||
| RL | `? :` |
|
||||
| RL | `=`, `+=`, `-=` ,`*=`, `/=`, `%=`, `>>=`, `<<=`, `&=`, `^=`, |= |
|
||||
| LR | `,` |
|
||||
|
||||
|
||||
#### Less precedence
|
||||
|
||||
|
||||
---
|
||||
### Conditional Branching
|
||||
|
||||
if ( condition ) statement ;
|
||||
else statement_2 ; /* optional else clause */
|
||||
|
||||
---
|
||||
|
||||
### Switch statement
|
||||
|
||||
switch ( expression ) /* constants must be unique */
|
||||
{
|
||||
case constant_1: /* do nothing for this case */
|
||||
break;
|
||||
case constant_2: /* drop through and do same constant_3 */
|
||||
case constant_3:
|
||||
statement_sequence /* can have but does not need { } */
|
||||
break;
|
||||
case constant_4:
|
||||
statement_sequence /* does this and next statement_sequence also*/
|
||||
case constant_5:
|
||||
statement_sequence
|
||||
break;
|
||||
default: /* default executes if no constant equals*/
|
||||
statement_sequence /* the expression. This is optional */
|
||||
}
|
||||
|
||||
---
|
||||
241
languages/C.txt
241
languages/C.txt
@@ -1,241 +0,0 @@
|
||||
|
||||
|
||||
main() Function
|
||||
|
||||
* The main() function is the starting point of the program: int main (int argc, char *argv[])
|
||||
* The return type of the main() function is an integer (type int) and it is known as the return value of the program.
|
||||
* As a rule of thumb, value 0 means success while non-zero means an error conditions.
|
||||
|
||||
Include Files
|
||||
|
||||
* The purpose of these files is to tell the compiler about the existence of external functions which the source code will make use of.
|
||||
|
||||
Preprocessor directives:
|
||||
#include "mine.h" search current working directory first
|
||||
#include <stdio.h> search command line directory then system
|
||||
#define TRUE 1 macro substitution, usually use capitals
|
||||
#define min(a,b) (a<b)?(a):(b) macro substitution with parameters
|
||||
#define abs(a) (a<0)?(-(a)):(a) macro substitution
|
||||
#define note /* comment */ this comment gets inserted every time note appears */
|
||||
backslash \ at end of a line means continue
|
||||
#undef TRUE undefines a previously defined macroname
|
||||
#error stop compiling at this point
|
||||
#if expression conditional compilation, start if structure
|
||||
#elif expression else if expression != 0 compile following code
|
||||
#else else compile following code
|
||||
#endif end of conditional compiling
|
||||
#ifdef macroname like #if, compiles if macroname defined
|
||||
#ifndef like #if, compiles if macroname undefined
|
||||
#line number [filename] set origin for __LINE__ and __FILE__
|
||||
#pragma gives the compiler commands
|
||||
|
||||
Create and execute a program
|
||||
|
||||
In Linux systems:
|
||||
1. Open up a terminal
|
||||
2. Create the program: nano nameProgram.c
|
||||
3. Write the program and save it
|
||||
4. gcc -o nameExecutable nameProgram.c
|
||||
|
||||
32 Reserved words
|
||||
|
||||
Term Description
|
||||
auto optional local declaration
|
||||
break used to exit loop and used to exit switch
|
||||
case choice in a switch
|
||||
char basic declaration of a type character
|
||||
const prefix declaration meaning variable can not be changed
|
||||
continue go to bottom of loop in for, while and do loops
|
||||
default optional last case of a switch
|
||||
do executable statement, do-while loop
|
||||
double basic declaration double precision floating point
|
||||
else executable statement, part of "if" structure
|
||||
enum basic declaration of enumeration type
|
||||
extern prefix declaration meaning variable is defined externally
|
||||
float basic declaration of floating point
|
||||
for executable statement, for loop
|
||||
goto jump within function to a label
|
||||
if executable statement
|
||||
int basic declaration of integer
|
||||
long prefix declaration applying to many types
|
||||
register prefix declaration meaning keep variable in register
|
||||
return executable statement with or without a value
|
||||
short prefix declaration applying to many types
|
||||
signed prefix declaration applying to some types
|
||||
sizeof operator applying to variables and types, gives size in bytes
|
||||
static prefix declaration to make local variable static
|
||||
struct declaration of a structure, like a record
|
||||
switch executable statement for cases
|
||||
typedef creates a new type name for an existing type
|
||||
union declaration of variables that are in the same memory locations
|
||||
unsigned prefix declaration applying to some types
|
||||
void declaration of a typeless variable
|
||||
volatile prefix declaration meaning the variable can be changed at any time
|
||||
while executable statement, while loop or do-while loop
|
||||
|
||||
Basic types
|
||||
|
||||
Type Description
|
||||
char character type, usually one byte ( a string is array of char )
|
||||
int integer type, usually 2 or 4 bytes ( default )
|
||||
float floating point type, usually 4 bytes
|
||||
double floating point type, usually 8 bytes
|
||||
void no type, typeless
|
||||
enum enumeration type ( user defines the type name )
|
||||
|
||||
Type modifiers, prefix for basic types
|
||||
|
||||
Modifiers Description
|
||||
signed has a sign ( default )
|
||||
unsigned no sign bit in variable
|
||||
long longer version of type (short or long alone means short int or
|
||||
short shorter version of type long int because int is the default)
|
||||
const variable can not be stored into
|
||||
|
||||
|
||||
Storage Types
|
||||
|
||||
Prefix Description
|
||||
auto local variable ( default )
|
||||
static permanent when function exits, not auto
|
||||
volatile can change from outside influence
|
||||
extern variables are defined elsewhere, externally
|
||||
register assign variable to register
|
||||
|
||||
Operators
|
||||
|
||||
( ) grouping parenthesis, function call
|
||||
[ ] array indexing, also [ ][ ] etc.
|
||||
-> selector, structure pointer
|
||||
. select structure element
|
||||
! relational not, complement, ! a yields true or false
|
||||
~ bitwise not, ones complement, ~ a
|
||||
++ increment, pre or post to a variable
|
||||
-- decrement, pre or post to a variable
|
||||
- unary minus, - a
|
||||
+ unary plus, + a
|
||||
* indirect, the value of a pointer, * p is value at pointer p address
|
||||
& the memory address, & b is the memory address of variable b
|
||||
sizeof size in bytes, sizeof a or sizeof (int)
|
||||
(type) a cast, explicit type conversion, (float) i, (*fun)(a,b), (int*)x
|
||||
* multiply, a * b
|
||||
/ divide, a / b
|
||||
% modulo, a % b
|
||||
+ add, a + b
|
||||
- subtract, a - b
|
||||
<< shift left, left operand is shifted left by right operand bits
|
||||
>> shift right, left operand is shifted right by right operand bits
|
||||
< less than, result is true or false, a %lt; b
|
||||
<= less than or equal, result is true or false, a <= b
|
||||
> greater than, result is true or false, a > b
|
||||
>= greater than or equal, result is true or false, a >= b
|
||||
== equal, result is true or false, a == b
|
||||
!= not equal, result is true or false, a != b
|
||||
& bitwise and, a & b
|
||||
^ bitwise exclusive or, a ^ b
|
||||
| bitwise or, a | b
|
||||
&& relational and, result is true or false, a < b && c >= d
|
||||
|| relational or, result is true or false, a < b || c >= d
|
||||
? exp1 ? exp2 : exp3 result is exp2 if exp1 != 0, else result is exp3
|
||||
= store
|
||||
+= add and store
|
||||
-= subtract and store
|
||||
*= multiply and store
|
||||
/= divide and store
|
||||
%= modulo and store
|
||||
<<= shift left and store
|
||||
>>= shift right and store
|
||||
&= bitwise and and store
|
||||
^= bitwise exclusive or and store
|
||||
|= bitwise or and store
|
||||
, separator as in ( y=x,z=++x )
|
||||
|
||||
|
||||
Operator precedence
|
||||
|
||||
More precedence
|
||||
|
||||
LR ( ) [ ] -> . x++ x--
|
||||
RL ! ~ - + ++x --x * & sizeof (type)
|
||||
LR * / %
|
||||
LR + -
|
||||
LR << >>
|
||||
LR < <= > >=
|
||||
LR == !=
|
||||
LR &
|
||||
LR ^
|
||||
LR |
|
||||
LR &&
|
||||
LR ||
|
||||
RL ? :
|
||||
RL = += -= *= /= %= >>= <<= &= ^= |=
|
||||
LR ,
|
||||
|
||||
Less precedence
|
||||
|
||||
Conditional branching
|
||||
|
||||
if ( condition ) statement ;
|
||||
else statement_2 ; /* optional else clause */
|
||||
|
||||
Switch statement
|
||||
|
||||
switch ( expression ) /* constants must be unique */
|
||||
{
|
||||
case constant_1: /* do nothing for this case */
|
||||
break;
|
||||
case constant_2: /* drop through and do same as constant_3*/
|
||||
case constant_3:
|
||||
statement_sequence /* can have but does not need { } */
|
||||
break;
|
||||
case constant_4:
|
||||
statement_sequence /* does this and next */
|
||||
/* statement_sequence also*/
|
||||
case constant_5:
|
||||
statement_sequence
|
||||
break;
|
||||
default: /* default executes if no constant equals*/
|
||||
statement_sequence /* the expression. This is optional */
|
||||
}
|
||||
|
||||
Function definition
|
||||
|
||||
type function_name(int a, float b, const char * ch,...) { function_body }
|
||||
|
||||
/* only parameters passed by address can are modified*/
|
||||
|
||||
/* in the calling function, local copy can be modified*/
|
||||
|
||||
char * strcpy( char * s1, const char * s2 ) { statements }
|
||||
|
||||
Declarations forms
|
||||
|
||||
basic_type variable;
|
||||
|
||||
type variable[val][val]...[val]={data,data,...}; /*multidimensional array*/
|
||||
|
||||
struct struct_name { /* struct_name is optional */
|
||||
type variable_1; /* any declaration */
|
||||
… /* all variable names must be unique*/
|
||||
} variable_1, ... ; /* variables are optional */
|
||||
|
||||
struct struct_name { /* struct_name is optional */
|
||||
type variable_1: length; /* any declaration : length in bits */
|
||||
... /* type is int, unsigned or signed */
|
||||
} variable_1, ... ; /* variables are optional, they can also be arrays and pointers */
|
||||
|
||||
|
||||
union union_name { /* union_name is optional */
|
||||
type variable_1; /* variable_1 overlays variable_2 */
|
||||
type variable_2;
|
||||
...
|
||||
} variable_a, ...; /* variables are optional */
|
||||
|
||||
enum enum_type /* enum_name is optional */
|
||||
{ enumeration_name_1, /* establishes enumeration literals */
|
||||
enumeration_name_2=number,/* optional number, */
|
||||
... /* default is 0, 1, 2, ... */
|
||||
} variable, ...; /* variables are optional */
|
||||
|
||||
/* use dot notation to select a component of a struct or union */
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
|
||||
#### General
|
||||
|
||||
- `Ctrl`+`Shift`+`P`, `F1`: Show Command Palette
|
||||
- `Ctrl`+`Shift`+`P`, `F1`: Show Command Palette
|
||||
- `Ctrl`+`Shift`+`T`: Open last closed tab
|
||||
- `Ctrl`+`P`: Quick Open, Go to File
|
||||
- `Ctrl`+`Shift`+`N`: New window/instance
|
||||
- `Ctrl`+`W`: Close window/instance
|
||||
@@ -19,7 +20,7 @@
|
||||
- `Ctrl`+`C`: Copy line (empty selection)
|
||||
- `Ctrl`+`↓/↑`: Move line down / up
|
||||
- `Ctrl`+`Shift`+`K`: Delete line
|
||||
- `Ctrl`+`Enter` / `Ctrl`+`Shift`+`Enter`: Insert line below / above
|
||||
- `Ctrl`+`Enter` / `Ctrl`+`Shift`+`Enter`: Insert line below / above
|
||||
- `Ctrl`+`Shift`+`\`: Jump to matching bracket
|
||||
- `Ctrl`+`]` / `Ctrl`+`[`: Indent / Outdent line
|
||||
- `Ctrl`+`Home` / `End`: Go to beginning / end of file
|
||||
@@ -136,7 +137,7 @@
|
||||
|
||||
- [`Wrap Console Log`](https://marketplace.visualstudio.com/items?itemName=midnightsyntax.vscode-wrap-console-log): Wrap to console.log by word or selection.
|
||||
|
||||
- [`Bracket Pair Colorizer`](https://marketplace.visualstudio.com/items?itemName=CoenraadS.bracket-pair-colorizer): Allows matching brackets to be identified with colours.
|
||||
- [`Bracket Pair Colorizer`](https://marketplace.visualstudio.com/items?itemName=CoenraadS.bracket-pair-colorizer): Allows matching brackets to be identified with colours.
|
||||
|
||||
## My Settings
|
||||
|
||||
|
||||
Reference in New Issue
Block a user