Update
This commit is contained in:
parent
095dda21dd
commit
71b1b8c0d5
3 changed files with 136 additions and 15 deletions
137
c_tutorial.md
137
c_tutorial.md
|
@ -2,19 +2,19 @@
|
||||||
|
|
||||||
This is a quick [C](c.md) tutorial.
|
This is a quick [C](c.md) tutorial.
|
||||||
|
|
||||||
You should probably know at least the completely basic ideas of programming before reading this (what's a [programming language](programming_language.md), [source code](source_code.md), [command line](cli.md) etc.). If you already know another language you should be just fine.
|
You should probably know at least the completely basic ideas of programming before reading this (what's a [programming language](programming_language.md), [source code](source_code.md), [command line](cli.md) etc.). If you're as far as already knowing another language, this should be pretty easy to understand.
|
||||||
|
|
||||||
## About C and Programming
|
## About C and Programming
|
||||||
|
|
||||||
[C](c.md) is
|
[C](c.md) is
|
||||||
|
|
||||||
- A [programming language](programming_language.md), i.e. a language that lets you express [algorithms](algorithm.md).
|
- A **[programming language](programming_language.md)**, i.e. a language that lets you express [algorithms](algorithm.md).
|
||||||
- [Compiled](compiled.md) language (as opposed to [interpreted](interpreted.md)), i.e. you have to compile the code you write (with [compiler](compiler.md)) in order to obtain a [native](native.md) executable program (a binary file that you can run directly).
|
- [Compiled](compiled.md) language (as opposed to [interpreted](interpreted.md)), i.e. you have to compile the code you write (with [compiler](compiler.md)) in order to obtain a [native](native.md) executable program (a binary file that you can run directly).
|
||||||
- Extremely fast and efficient.
|
- Extremely **fast and efficient**.
|
||||||
- Very widely supported and portable to almost anything.
|
- Very **widely supported and portable** to almost anything.
|
||||||
- [Low level](low_level.md), i.e. there is relatively little [abstraction](abstraction.md) and not many comfortable built-in functionality such as [garbage collection](garbage_collection.md), you have to write many things yourself, you will deal with [pointers](pointer.md), [endianness](endianness.md) etc.
|
- **[Low level](low_level.md)**, i.e. there is relatively little [abstraction](abstraction.md) and not many comfortable built-in functionality such as [garbage collection](garbage_collection.md), you have to write many things yourself, you will deal with [pointers](pointer.md), [endianness](endianness.md) etc.
|
||||||
- Considered hard, but in certain ways it's simple, it lacks [bloat](bloat.md) and [bullshit](bullshit.md) of "[modern](modern.md)" languages which is an essential thing. It will take long to learn but it's the most basic thing you should know if you want to create good software. You won't regret.
|
- Considered **hard**, but in certain ways it's simple, it lacks [bloat](bloat.md) and [bullshit](bullshit.md) of "[modern](modern.md)" languages which is an essential thing. It will take long to learn but it's the most basic thing you should know if you want to create good software. You won't regret.
|
||||||
- Not holding your hand, i.e. you may very easily "shoot yourself in your foot" and crash your program. This is the price for the language's power.
|
- **Not holding your hand**, i.e. you may very easily "shoot yourself in your foot" and crash your program. This is the price for the language's power.
|
||||||
- Very old, well established and tested by time.
|
- Very old, well established and tested by time.
|
||||||
- Recommended by us for serious programs.
|
- Recommended by us for serious programs.
|
||||||
|
|
||||||
|
@ -73,13 +73,15 @@ written in the command line.
|
||||||
|
|
||||||
Now let's see what the source code means:
|
Now let's see what the source code means:
|
||||||
|
|
||||||
- `/* simple C program! */` is a comment, it does nothing, it's here only for the humans that will read the source code. Such comments can be almost anywhere in the code. The comment starts at `/*` and ends with `*/`.
|
- `/* simple C program! */` is so called *comment*, it does nothing, it's here only for the humans that will read the source code. Such comments can be almost anywhere in the code. The comment starts at `/*` and ends with `*/`.
|
||||||
- `// include IO library` is another comment, but this is a line comment, it starts with `//` and ends with the end of line.
|
- `// include IO library` is another comment, but this is a line comment, it starts with `//` and ends with the end of line.
|
||||||
- `#include <stdio.h>` tells the compiler we want to include a library named *stdio*. This is a standard library with input output functions, we need it to be able to use the function `puts` later on. We can include more libraries if we want to. These includes are almost always at the very top of the source code.
|
- `#include <stdio.h>` tells the compiler we want to include a library named *stdio*. This is a standard library with input output functions, we need it to be able to use the function `puts` later on. We can include more libraries if we want to. These includes are almost always at the very top of the source code.
|
||||||
- `int main(void)` is the start of the main program. What exactly this means will be explained later, for now just remember there has to be this function named `main` in basically every program -- inside it are commands that will be executed when the program is run. Note that the curly brackets that follow (`{` and `}`) denote the block of code that belongs to this function, so we need to write our commands between these brackets.
|
- `int main(void)` is the start of the main program. What exactly this means will be explained later, for now just remember there has to be this function named `main` in basically every program -- inside it are commands that will be executed when the program is run. Note that the curly brackets that follow (`{` and `}`) denote the block of code that belongs to this function, so we need to write our commands between these brackets.
|
||||||
- `puts("It works.");` is a "command" for printing text strings to the command line (it's a command from the `stdio` library included above). Why exactly this is written like this will be explained later, but for now notice the following. The command starts with its name (`puts`, for *put string*), then there are left and right brackets (`(` and `)`) between which there are arguments to the command, in our case there is one, the text string `"It works."`. Text strings have to be put between quotes (`"`), otherwise the compiler would think the words are other commands (the quotes are not part of the string itself, they won't be printed out). The command is terminated by `;` -- all "normal" commands in C have to end with a semicolon.
|
- `puts("It works.");` is a "command" for printing text strings to the command line (it's a command from the `stdio` library included above). Why exactly this is written like this will be explained later, but for now notice the following. The command starts with its name (`puts`, for *put string*), then there are left and right brackets (`(` and `)`) between which there are arguments to the command, in our case there is one, the text string `"It works."`. Text strings have to be put between quotes (`"`), otherwise the compiler would think the words are other commands (the quotes are not part of the string itself, they won't be printed out). The command is terminated by `;` -- all "normal" commands in C have to end with a semicolon.
|
||||||
- `return 0;` is another "command", it basically tells the operating system that everything was terminated successfully (`0` is a code for success). This command is an exception in that it doesn't have to have brackets (`(` and `)`). This doesn't have to bother us too much now, let's just remember this will always be the last command in our program.
|
- `return 0;` is another "command", it basically tells the operating system that everything was terminated successfully (`0` is a code for success). This command is an exception in that it doesn't have to have brackets (`(` and `)`). This doesn't have to bother us too much now, let's just remember this will always be the last command in our program.
|
||||||
|
|
||||||
|
Also notice how the source code is formatted, e.g. the indentation of code withing the `{` and `}` brackets. White characters (spaces, new lines, tabs) are ignored by the compiler so we can theoretically write our program on a single line, but that would be unreadable. We use indentation, spaces and empty lines to format the code to be well readable.
|
||||||
|
|
||||||
To sum up let's see a general structure of a typical C program. You can just copy paste this for any new program and then just start writing commands in the `main` function.
|
To sum up let's see a general structure of a typical C program. You can just copy paste this for any new program and then just start writing commands in the `main` function.
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -96,7 +98,124 @@ int main(void)
|
||||||
|
|
||||||
## TO BE CONTINUED
|
## TO BE CONTINUED
|
||||||
|
|
||||||
## Variables, Data Types
|
## Variables, Arithmetic, Data Types
|
||||||
|
|
||||||
|
Programming is a lot like mathematics, we compute equations and transform numerical values into other values. You probably know in mathematics we use *variables* such as *x* or *y* to denote numerical values that can change (hence variables). In programming we also use variables -- here **[variable](variable.md) is a place in memory which has a name**.
|
||||||
|
|
||||||
|
We can create variables named `x`, `y`, `myVariable` or `score` and then store specific values (for now let's only consider numbers) into them. We can read from and write to these variables at any time. These variables physically reside in [RAM](ram.md), but we don't really care where exactly (at which address) they are located -- this is e.g. similar to houses, in common talk we normally say things like *John's house* or *the pet store* instead of *house with address 3225*.
|
||||||
|
|
||||||
|
Variable names can't start with a digit. By convention they also shouldn't be all uppercase or start with uppercase (these are normally used for other things). Normally we name variables like this: `myVariable` or `my_variable` (pick one style, don't mix them).
|
||||||
|
|
||||||
|
In C as in other languages each variable has a certain **[data type](data_type.md)**; that is each variable has associated an information of what kind of data is stored in it. This can be e.g. a *whole number*, *fraction*, a *text character*, *text string* etc. Data types are a more complex topic that will be discussed later, for now we'll start with the most basic one, the **integer type**, in C called `int`. An `int` variable can store whole numbers in the range of at least -32768 to 32767 (but usually much more).
|
||||||
|
|
||||||
|
Let's see an example.
|
||||||
|
|
||||||
|
```
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int myVariable;
|
||||||
|
|
||||||
|
myVariable = 5;
|
||||||
|
|
||||||
|
printf("%d\n",myVariable);
|
||||||
|
|
||||||
|
myVariable = 8;
|
||||||
|
|
||||||
|
printf("%d\n",myVariable);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- `int myVariable;` is so called **variable declaration**, it tells the compiler we are creating a new variable with the name `myVariable` and data type `int`. Variables can be created almost anywhere in the code (even outside the `main` function) but that's a topic for later.
|
||||||
|
- `myVariable = 5;` is so called **variable assignment**, it stores a value 5 into variable named `myVariable`. IMPORTANT NOTE: the `=` does NOT signify mathematical equality but an assignment (equality in C is written as `==`); when compiler encounters `=`, it simply takes the value on the right of it and writes it to the variable on the left side of it. Sometimes people confuse assignment with an equation that the compiler solves -- this is NOT the case, assignment is much more simple, it simply writes a value into variable.
|
||||||
|
- `printf("%d\n",myVariable);` prints out the value currently stored in `myVariable`. Don't get scared by this complicated command, it will be explained later. For now only know this prints the variable content.
|
||||||
|
- `myVariable = 8;` assigns a new value to `myVariable`, overwriting the old.
|
||||||
|
- `printf("%d\n",myVariable);` again prints the value in `myVariable`.
|
||||||
|
|
||||||
|
After compiling and running of the program you should see:
|
||||||
|
|
||||||
|
```
|
||||||
|
5
|
||||||
|
8
|
||||||
|
```
|
||||||
|
|
||||||
|
Last thing to learn is **arithmetic operators**. They're just normal math operators such as +, - and /. You can use these along with brackets (`(` and `)`) to create **[expressions](expression.md)**. Expressions can contain variables and can themselves be used in many places where variables can be used (but not everywhere, e.g. on the left side of variable assignment, that would make no sense). E.g.:
|
||||||
|
|
||||||
|
```
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int heightCm = 175;
|
||||||
|
int weightKg = 75;
|
||||||
|
int bmi = (weightKg * 10000) / (heightCm * heightCm);
|
||||||
|
|
||||||
|
printf("%d\n",bmi);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
calculates and prints your BMI (body mass index).
|
||||||
|
|
||||||
|
## Branches and Loops
|
||||||
|
|
||||||
|
## Simple Game: Guess a Number
|
||||||
|
|
||||||
|
With what we've learned so far we can already make a simple [game](game.md): guess a number. The computer thinks a random number in range 0 to 9 and the user has to guess it. The source code is following.
|
||||||
|
|
||||||
|
```
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
srand(clock()); // random seed
|
||||||
|
|
||||||
|
while (1) // infinite loop
|
||||||
|
{
|
||||||
|
int randomNumber = rand() % 10;
|
||||||
|
|
||||||
|
puts("I think a number. What is it?");
|
||||||
|
|
||||||
|
int guess;
|
||||||
|
|
||||||
|
scanf("%d",&guess); // read the guess
|
||||||
|
|
||||||
|
getchar();
|
||||||
|
|
||||||
|
if (guess == randomNumber)
|
||||||
|
puts("You guessed it!");
|
||||||
|
else
|
||||||
|
printf("Wrong. The number was %d.\n",randomNumber);
|
||||||
|
|
||||||
|
puts("Play on? [y/n]");
|
||||||
|
|
||||||
|
char answer;
|
||||||
|
|
||||||
|
scanf("%c",&answer); // read the answer
|
||||||
|
|
||||||
|
if (answer == 'n')
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
puts("Bye.");
|
||||||
|
|
||||||
|
return 0; // return success, always here
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- `#include <stdlib.h>`, `#include <time.h>`: we're including additional libraries because we need some specific functions from them (`rand`, `srand`, `clock`).
|
||||||
|
- `srand(clock());`: don't mind this line too much, its purpose is to [seed](seed.md) a pseudorandom number generator. Without doing this the game would always generate the same sequence of random numbers when run again.
|
||||||
|
- `while (1)` is an infinite game loop -- it runs over and over, in each cycle we perform one game round. The loop can be exited with the `break` statement later on (if the user answers he doesn't want to continue playing).
|
||||||
|
- `int randomNumber = rand() % 10;`: this line declares a variable named `randomNumber` and immediately assigns a value to it. The value is a random number from 0 to 9. This is achieved with a function `rand` (from the above included `stdlib` library) which returns a random number, and with the modulo (remainder after division) arithmetic operator (`%`) which ensures the number is in the correct range (less than 10).
|
||||||
|
- `int guess;` creates another variable in which we'll store the user's guessed number.
|
||||||
|
- `scanf("%d",&guess);` reads a number from the input to the variable named `guess`. Again, don't be bothered by the complicated structure of this command, for now just accept that this is how it's done.
|
||||||
|
- `getchar();`: don't mind this line, it just discards a newline character read from the input.
|
||||||
|
- `if (guess == randomNumber) ...`: this is a branch which checks if the user guess is equal to the generated random number. If so, a success message is printed out. If not, a fail message is printed out along with the secret number. Note we use the `puts` function for the first message as it only prints a text sting, while for the latter message we have to use `printf`, a more complex function, because that requires inserting a number into the printed string. More on these functions later.
|
||||||
|
- `char answer;` declares a variable to store user's answer to a question of whether to play on. It is of `char` data type which can store a single text character.
|
||||||
|
- `scanf("%c",&answer);` reads a single character from input to the `answer` variable.
|
||||||
|
- `if (answer == 'n') break;` is a branch that exits the infinite loop with `break` statement if the answer entered was `n` (*no*).
|
||||||
|
|
||||||
## Functions
|
## Functions
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
# Collapse
|
# Collapse
|
||||||
|
|
||||||
Collapse of our civilization is something that's very likely coming very soon: we are especially focusing on a very probable **technological collapse** (caused by badly designed technology as well as its wrong application and extreme overuse causing a dangerous dependence) but of course clues point to it coming from many directions (ecological, economical, political, natural disasters such as a coronal mass ejection etc.).
|
Collapse of our civilization is something that's very likely coming very soon: we are especially focusing on a very probable **technological collapse** (caused by badly designed technology as well as its wrong application and extreme overuse causing a dangerous dependence) but of course clues point to it coming from many directions (ecological, economical, political, natural disasters such as a coronal mass ejection etc.). Recently there has even appeared a specific term *collapsology* referring to the study of the potential collapse.
|
||||||
|
|
||||||
There is a [reddit](reddit.md) community for discussing the collapse at https://reddit.net/r/collapse.
|
There is a [reddit](reddit.md) community for discussing the collapse at https://reddit.net/r/collapse.
|
||||||
|
|
||||||
In technological world a lot of people are concerned with the collapse, notable the [collapse OS](collapse_os.md), an operating system meant to run on simple [hardware](hw.md) after the technological supply chain collapses and renders development of modern computers impossible. They believe the collapse will happen before 2030. The chip shortage and energy crisis of 2020s are one of the first warnings and shows how fragile the systems really is. People like [Luke Smith](luke_smith.md) advocate (and practice) simple, independent off-grid living to be ready for a life after the collapse. Even [proprietary](proprietary.md) normies such as [Jonathan Blow](jonathan_blow.md) warn of a coming disaster (in his talk *Preventing the Collapse of Civilization*). [Viznut](viznut.md) is another programmer warning of the collapse.
|
In technological world a lot of people are concerned with the collapse, notable the [collapse OS](collapse_os.md), an operating system meant to run on simple [hardware](hw.md) after the technological supply chain collapses and renders development of modern computers impossible. They believe the collapse will happen before 2030. The chip shortage and energy crisis of 2020s are one of the first warnings and shows how fragile the systems really is.
|
||||||
|
|
||||||
|
[Ted Kaczynski](ted_kaczynski.md), a famous primitivist murderer, has seen the collapse as a possible option. People like [Luke Smith](luke_smith.md) advocate (and practice) simple, independent off-grid living, besides other reasons in order to be prepared for such an eventuality as a collapse. Even [proprietary](proprietary.md) normies such as [Jonathan Blow](jonathan_blow.md) warn of a coming disaster (in his talk *Preventing the Collapse of Civilization*). [Viznut](viznut.md) is another programmer warning about the collapse.
|
||||||
|
|
||||||
The details of the collapse cannot of course be predicted exactly -- it may come is an quick, violent form (e.g. in case of a disaster causing a blackout) or as a more agonizing slow death. CollapseOS site talks about two stages of the slow collapse: the first one after the collapse of the supply chain. i.e. when the production of modern computers halts, and the second (decades after) when the last modern computer stops working.
|
The details of the collapse cannot of course be predicted exactly -- it may come is an quick, violent form (e.g. in case of a disaster causing a blackout) or as a more agonizing slow death. CollapseOS site talks about two stages of the slow collapse: the first one after the collapse of the supply chain. i.e. when the production of modern computers halts, and the second (decades after) when the last modern computer stops working.
|
|
@ -9,14 +9,14 @@ Firstly let us welcome you, no matter who you are, no matter your political opin
|
||||||
If you don't know how to start, here are some basic steps:
|
If you don't know how to start, here are some basic steps:
|
||||||
|
|
||||||
1. **Learn about the most essential topics and concepts**, mainly [free software](free_software.md), [open-source](open_source.md), [bloat](bloat.md), [kiss](kiss.md), [capitalist_software](capitalist_software.md), [suckless](suckless.md) and [LRS](lrs.md).
|
1. **Learn about the most essential topics and concepts**, mainly [free software](free_software.md), [open-source](open_source.md), [bloat](bloat.md), [kiss](kiss.md), [capitalist_software](capitalist_software.md), [suckless](suckless.md) and [LRS](lrs.md).
|
||||||
2. **Install [GNU](gnu.md)/[Linux](linux.md)** operating system to free yourself from shit like [Windows](windows.md) and [Mac](mac.md) (you can also consider [BSD](bsd.md) but you're probably too noob for that). Do NOT try to switch to "Linux" right away if it's your first time, it's almost impossible, you want to just install "Linux" as [dual boot](dual_boot.md) (alongside your main OS) or on another computer. This way you'll be using both operating systems, slowly getting more comfortable with "Linux" and eventually you'll find yourself uninstalling Windows altogether. You can also just try "Linux" in a [virtual machine](vm.md), from a live CD or you can buy something with "Linux" preinstalled like [Raspberry Pi](raspberry.md). **Which "Linux" to install?** There are many options and as a noob you don't have to go hardcore right away, just install any [distro](distro.md) that "works" (don't listen to people who tell you to install [Gentoo](gentoo.md) tho). You can try these:
|
2. **Install [GNU](gnu.md)/[Linux](linux.md)** operating system to free yourself from shit like [Windows](windows.md) and [Mac](mac.md) (you can also consider [BSD](bsd.md) but you're probably too noob for that). Do NOT try to switch to "Linux" right away if it's your first time, it's almost impossible, you want to just install "Linux" as [dual boot](dual_boot.md) (alongside your main OS) or on another computer (easier). This way you'll be using both operating systems, slowly getting more comfortable with "Linux" and eventually you'll find yourself uninstalling Windows altogether. You can also just try "Linux" in a [virtual machine](vm.md), from a live CD/flash drive or you can buy something with "Linux" preinstalled like [Raspberry Pi](raspberry.md). **Which "Linux" to install?** There are many options and as a noob you don't have to go hardcore right away, just install any [distro](distro.md) that [just werks](just_werks.md) (don't listen to people who tell you to install [Gentoo](gentoo.md) tho). You can try these:
|
||||||
- [Devuan](devuan.md): Nice, [LRS](lrs.md) approved distro that respects your [freedom](free_software.md) that just works, is easy to install and is actually nice. Good for any skill level.
|
- [Devuan](devuan.md): Nice, [LRS](lrs.md) approved distro that respects your [freedom](free_software.md) that just works, is easy to install and is actually nice. Good for any skill level.
|
||||||
- [Debian](debian.md): Like Devuan but uses the evil [systemd](systemd.md) which doesn't have to bother you at this point. Try Debian if Devuan doesn't work for any reason.
|
- [Debian](debian.md): Like Devuan but uses the evil [systemd](systemd.md) which doesn't have to bother you at this point. Try Debian if Devuan doesn't work for any reason.
|
||||||
- [Mint](mint.md): More noob, [bloated](bloat.md) and mainstream distro that only mildly cares about freedom, but is extremely easy and works almost everywhere. Try this if Debian didn't work for you.
|
- [Mint](mint.md): More noob, [bloated](bloat.md) and mainstream distro that only mildly cares about freedom, but is extremely easy and works almost everywhere. Try this if Debian didn't work for you.
|
||||||
- [Ubuntu](ubuntu.md): Kind of like Mint, try it if Mint didn't work.
|
- [Ubuntu](ubuntu.md): Kind of like Mint, try it if Mint didn't work.
|
||||||
2. **Learn a bit of [command line](cli.md)** and **start using [FOSS](foss.md) alternatives** to you [proprietary](proprietary.md) programs, e.g. [GIMP](gimp.md) instead of Photoshop.
|
2. **Learn a bit of [command line](cli.md)** and **start using [FOSS](foss.md) alternatives** to you [proprietary](proprietary.md) programs, e.g. [GIMP](gimp.md) instead of Photoshop, [LibreOffice](libreoffice.md) instead of MS Office etc.
|
||||||
3. If you want to program [LRS](lrs.md), learn [C](c.md) (TODO: resources). Also learn a bit of POSIX shell and maybe some [scripting](script.md) language (can be even a bloated one like [Python](python.md)). Learn about [licensing](license.md).
|
3. If you want to program [LRS](lrs.md), **learn [C](c.md)** (see the [tutorial](c_tutorial.md)). Also learn a bit of [POSIX shell](posix_shell.md) and maybe some mainstream [scripting](script.md) language (can be even a bloated one like [Python](python.md)). Learn about [licensing](license.md) and [version control](vcs.md) ([git](git.md)).
|
||||||
4. Optionally make your own minimal [website](web.md) to help reshare ideas you like (static [HTML](html.md) site without [JavaScript](javascript.md)). This is very easy, and the site can be hosted for free e.g. on [git](git.md) hosting sites like Codeberg or GitLab.
|
4. Optionally make your own minimal [website](web.md) to help reshare ideas you like (static [HTML](html.md) site without [JavaScript](javascript.md)). This is very easy, and the site can be hosted for free e.g. on [git](git.md) hosting sites like Codeberg or GitLab. Get in touch with us.
|
||||||
5. Finally start creating something: either programs or other stuff like [free art](free_culture.md), educational materials, contributions to this Wiki etc.
|
5. Finally start creating something: either programs or other stuff like [free art](free_culture.md), educational materials, contributions to this Wiki etc.
|
||||||
6. profit???
|
6. profit???
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue