This commit is contained in:
Miloslav Ciz 2022-08-01 20:59:33 +02:00
parent e074c9eb6e
commit 8c38cbc178
7 changed files with 34 additions and 12 deletions

View file

@ -6,7 +6,7 @@ This is a relatively 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'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
@ -168,7 +168,7 @@ Let's quickly mention how you can read and write values in C so that you can beg
- `printf("%d ");`: Same as above but without a newline.
- `scanf("%d",&x);`: Read a number from input to the variable `x`. Note there has to be `&` in front of `x`.
## Branches and Loops (If, While, For)
## Branches And Loops (If, While, For)
When creating [algorithms](algorithm.md), it's not enough to just write linear sequences of commands. Two things (called [control structures](control_structure.md)) are very important to have in addition:
@ -182,7 +182,7 @@ if (x > 10)
puts("X is greater than 10.");
```
The [syntax](syntax.md) is given, we start with `if`, then brackets (`(` and `)`) follow inside which there is a condition, then a command or a block of multiple commands (inside `{` and `}`) follow. If the condition in brackets hold, the command (or block of commands) gets executed, otherwise it is skipped.
The [syntax](syntax.md) is given, we start with `if`, then brackets (`(` and `)`) follow inside which there is a condition, then a command or a block of multiple commands (inside `{` and `}`) follow. If the condition in brackets holds, the command (or block of commands) gets executed, otherwise it is skipped.
Optionally there may be an *else* branch which is gets executed only if the condition does NOT hold. It is denoted with the `else` keyword which is again followed by a command or a block of multiple commands. Branching may also be nested, i.e. branches may be inside other branches. For example:
@ -289,7 +289,7 @@ The code above places a condition in the middle of an infinite loop to prevent d
Again, loops can be nested (we may have loops inside loops) and also loops can contain branches and vice versa.
## Simple Game: Guess a Number
## 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.
@ -797,7 +797,7 @@ As a bonus, let's see a few useful compiler flags:
- `-c`: Compile only (generate object files, do not link).
- `-g`: Include debug symbols, this will be important for [debugging](debugging.md).
## Advanced Data Types and Variables (Structs, Arrays, Strings)
## Advanced Data Types And Variables (Structs, Arrays, Strings)
Until now we've encountered simple data types such as `int`, `char` or `float`. These identify values which can take single atomic values (e.g. numbers or text characters). Such data types are called **[primitive types](primitive_type.md)**.
@ -1538,7 +1538,7 @@ int main(void)
The file mode is now `"rb"` (read binary). For reading from binary files we use the `fread` function, similarly to how we used `fscanf` for reading from a text file. `fread` has these parameters: pointer where to store the read data (the memory must have sufficient space allocated!), size of one data item, number of items to read and the pointer to the file which to read from. As the first argument we pass `&byte`, i.e. the address of the variable `byte`, next 1 (we want to read a single byte whose size in bytes is 1), 1 (we want to read one byte) and the file pointer. `fread` returns the number of items read, so the `while` condition holds as long as `fread` reads bytes; once we reach end of file, `fread` can no longer read anything and returns 0 (which in C is interpreted as a false value) and the loop ends. Again, we must close the file at the end.
## More on Functions (Recursion, Function Pointers)
## More On Functions (Recursion, Function Pointers)
There's more to be known about functions.
@ -1676,7 +1676,7 @@ The very basic thing we can do is to turn on automatic optimization with a compi
TODO
## Where to Go Next
## Where To Go Next
We haven't covered the whole of C, not even close, but you should have pretty solid basics now. Now you just have to go and write a lot of C programs, that's the only way to truly master C. WARNING: Do not start with an ambitious project such as a 3D game. You won't make it and you'll get demotivated. Start very simple (a Tetris clone perhaps?).