<commit_before>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Variables\n",
    "\n",
    "`C` language uses static typing, which means that you should specify type of variable on its declaration\n",
    "\n",
    "This may look familiar to you\n",
    "```c\n",
    "int a; // Create integer 'a'\n",
    "```\n",
    "\n",
    "You can assign value immediately\n",
    "```c\n",
    "int k = 10;\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Integer data types\n",
    "\n",
    "Types arranged from the smallest to the biggest\n",
    "- char\n",
    "- short\n",
    "- int\n",
    "- long\n",
    "- long long"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Unsigned and signed\n",
    "\n",
    "You can attach prefix `unsigned` to denote that you want to use non-negative integers\n",
    "```\n",
    "unsigned long t = 0;\n",
    "```\n",
    "\n",
    "Prefix `signed` used to have guarantees, that variable can contain negative values as well\n",
    "```\n",
    "signed char p = 7;\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Int\n",
    "\n",
    "Note that `unsigned` or `signed` without type name means `unsigned int` and `signed int` respectively\n",
    "```c\n",
    "unsigned amount = 150;\n",
    "signed height = -1;\n",
    "signed weight = 1300;\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Floating point data types\n",
    "\n",
    "Floating point literals can have suffix to avoid ambiguities in functions calls\n",
    "\n",
    "|name | size (bits/bytes) | suffix |\n",
    "| --- | ---- | ------ |\n",
    "| float | 32/4 | f |\n",
    "| double | 64/8 |  |\n",
    "| long double | 80/10 | L |\n",
    "\n",
    "If no suffix specified for literal, `double` type used.\n",
    "\n",
    "```c\n",
    "float width = 1.15;\n",
    "float price = 1.0f;\n",
    "long double frequency = 1.0L;\n",
    "double temp = 0.0;\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Functions\n",
    "\n",
    "Here is the simplest function, which does nothing\n",
    "```c\n",
    "void f() {\n",
    "}\n",
    "```\n",
    "\n",
    "Every function should have strictly specified type of returned value\n",
    "\n",
    "Sometimes it's needed to use function, which doesn't return any values &mdash; in this case you can use `void` type.\n",
    "\n",
    "```c\n",
    "void f() {\n",
    "    return; // This is correct too\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Function return type\n",
    "\n",
    "It's time to write something better than stub\n",
    "```c\n",
    "int get_five() {\n",
    "    return 5;\n",
    "}\n",
    "```\n",
    "\n",
    "Almost like in Python\n",
    "```python\n",
    "def get_five():\n",
    "    return 5\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Function parameters\n",
    "\n",
    "Function parameters should have type too\n",
    "```c\n",
    "int add(float a, long b) {\n",
    "    return a + b;\n",
    "}\n",
    "```\n",
    "\n",
    "A bit harder than in `Python`, but still good\n",
    "```python\n",
    "def add(a, b):\n",
    "    return a + b\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Calling functions\n",
    "\n",
    "Just like in `Python`, but don't forget about semicolons\n",
    "```c\n",
    "add(2, 2);\n",
    "int result = add(add(1.0f, 0) * 2f, 5);\n",
    "float k = add(0.0, result);\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# See results of your code\n",
    "\n",
    "`C` applications should be compiled to be executed\n",
    "\n",
    "You cannot run it in interpreter"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Print\n",
    "\n",
    "Function `printf` in `C` behaves like `print` in `Python`\n",
    "```C\n",
    "printf(\"Hello world!\\n\");\n",
    "```\n",
    "\n",
    "This function allows you to use formatted string\n",
    "```C\n",
    "printf(\"Give me %d cup(s) of tea, mr. %s\",\n",
    "        3, \"John\");\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Entry point\n",
    "\n",
    "`C` application should have a function called `main`, which will be executed on application start\n",
    "\n",
    "```c\n",
    "int main() {\n",
    "    return 0;\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Hello world\n",
    "\n",
    "We need to include header file of library with `printf` function and here we are\n",
    "```c\n",
    "#include <stdio.h>\n",
    "\n",
    "int main() {\n",
    "    printf(\"Hello world!\\n\");\n",
    "    return 0;\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Execute\n",
    "\n",
    "In Linux you should have `gcc` installed in order to compile your code\n",
    "```bash\n",
    "gcc file.c -o app\n",
    "```\n",
    "\n",
    "and execute\n",
    "```bash\n",
    "./app\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Conditions\n",
    "\n",
    "Conditions in `C` are very similar to those in `Python`\n",
    "```c\n",
    "if (true) {\n",
    "    printf(\"That's true\\n\");\n",
    "} else if (false) {\n",
    "    printf(\"This shouldn't be done\\n\");\n",
    "} else {\n",
    "    printf(\"Are you kidding me?\");\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Switch-case\n",
    "\n",
    "If you have checks like\n",
    "```c\n",
    "int a = 5;\n",
    "if (a == 0) printf(\"Zero\\n\");\n",
    "else if (a == 5) printf(\"Five\\n\");\n",
    "else printf(\"Another variant\\n\");\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "In `C` language you can do this\n",
    "```c\n",
    "int a = 5;\n",
    "switch(a) {\n",
    "    case 0:\n",
    "        printf(\"Zero\\n\");\n",
    "        break;\n",
    "    case 5:\n",
    "        printf(\"Five\\n\");\n",
    "        break;\n",
    "    default:\n",
    "        printf(\"Another variant\\n\");\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Seems not so pretty, but it's better to use `switch-case` instead of `if`, when you want to check, for example, which key was pressed in your videogame\n",
    "```c\n",
    "switch(key) {\n",
    "    UP:\n",
    "        move_hero_up(hero);\n",
    "        break;\n",
    "    DOWN:\n",
    "        move_hero_down(hero);\n",
    "        break;\n",
    "    default:\n",
    "        hero_stay(hero);\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Loops\n",
    "\n",
    "`C` language has following loop types\n",
    "- `for`\n",
    "- `while`\n",
    "- `do-while`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Increment and decrement\n",
    "\n",
    "There's a shorthand for numbers increment or decrement by `1` in `C` language\n",
    "```c\n",
    "i++; // Increment i\n",
    "a--; // Decrement a\n",
    "++b; // Increment b\n",
    "--c; // Decrement c\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Post and pre\n",
    "\n",
    "Post increment like `i++` means, that addition will be performed after other operations in current expression\n",
    "```c\n",
    "int i = 5;\n",
    "printf(\"%d\\n\", i++); // Will print 5\n",
    "printf(\"%d\\n\", i); // Will print 6\n",
    "```\n",
    "Pre increment written ad `++i` will increment before expression evaluation\n",
    "```c\n",
    "int i = 5;\n",
    "printf(\"%d\\n\", ++i); // Will print 6\n",
    "printf(\"%d\\n\", i); // Will print 6\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## While\n",
    "\n",
    "Just like in `Python`, but in `C` we have handy increment\n",
    "```c\n",
    "int i = 0;\n",
    "while (i < 10) {\n",
    "    printf(\"%d\\n\", i);\n",
    "    i++;\n",
    "}\n",
    "```\n",
    "\n",
    "`Python` code\n",
    "```python\n",
    "i = 0\n",
    "while i < 10:\n",
    "    print i\n",
    "    i += 1\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## do-while\n",
    "\n",
    "Like `while`, but condition check occurs not in the start, but in the end of loop\n",
    "```c\n",
    "int i = 0;\n",
    "do {\n",
    "    printf(\"%d\\n\", i);\n",
    "} while (++i < 10);\n",
    "```\n",
    "\n",
    "In `Python`\n",
    "```python\n",
    "i = 0\n",
    "while True:\n",
    "    print i\n",
    "    i += 1\n",
    "    if i > 10: break\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## `Goes to` operator (not a `goto`, IYKWIM)\n",
    "\n",
    "We can not only increment number by `++`, but decrement with `--` as well and this means, that following is correct\n",
    "```c\n",
    "int i = 10;\n",
    "while (i --> 0) {\n",
    "    next_step();\n",
    "}\n",
    "```\n",
    "\n",
    "What happens:\n",
    "- we have `i` equal to `10`\n",
    "- on each loop iteration we compare it to `0` and decrement"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## For loop\n",
    "\n",
    "This loop in `C` is not like `for` in `Python`\n",
    "```c\n",
    "for (int a = 0; a < 10; a++)\n",
    "    printf(\"Number %d\\n\", a);\n",
    "```\n",
    "\n",
    "This means\n",
    "```c\n",
    "{\n",
    "    int a = 0;\n",
    "    while (a < 10){\n",
    "        printf(\"Number %d\\n\", a);\n",
    "        a++;\n",
    "    }\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Arrays\n",
    "\n",
    "Arrays in `C` are not similar to lists in `Python`, because they should have fixed size and type of each element"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Static arrays\n",
    "\n",
    "Such arrays can have constant size, which cannot be defined during runtime\n",
    "\n",
    "This is useful for strings, which is an array of `char`\n",
    "```c\n",
    "char hello_world[] = \"Hello world\";\n",
    "```\n",
    "Also you can write\n",
    "```c\n",
    "char hello_world[] = {'H', 'e', 'l', 'l', 'o', ' ',\n",
    "                      'w', 'o', 'r', 'l', 'd', 0};\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Also you may want to create an array of numbers\n",
    "```c\n",
    "float steps[3] = {1.0, 2.0, 5.5};\n",
    "```\n",
    "You can fill not all values at one time and take care of them later\n",
    "```c\n",
    "int numbers[5] = {0, -14, 17};\n",
    "numbers[4] = 16;\n",
    "numbers[3] = 5;\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Loop through array\n",
    "\n",
    "You can give size of static array by `sizeof` operator\n",
    "\n",
    "It will give you size of all array in bytes and you will need to divide it by size of its elements\n",
    "```c\n",
    "int n[] = {1, 2, 3};\n",
    "for (int i = 0; i < sizeof(n)/sizeof(n[0]); i++)\n",
    "    printf(\"%d\\n\", n[i]);\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Pointer\n",
    "\n",
    "Pointer is just a type of variable, which contains address of byte in memory"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Address of variable\n",
    "\n",
    "Every variable has an address and you can get it by `&` sign like `&var_name`\n",
    "```c\n",
    "int a = 0;\n",
    "printf(\"Address of a is %X\\n\", &a);\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Creating pointer\n",
    "\n",
    "You can store this address in separate variable\n",
    "\n",
    "Pointer to variable of some type should have a type of that variable with `*` sign\n",
    "```c\n",
    "float a = 1;\n",
    "float* b = &a; // Now b contains address of a\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Working with memory pointed by pointer\n",
    "\n",
    "You can access the value pointer points to with `*` sign\n",
    "```c\n",
    "double a = 0.5;\n",
    "double* b = &a;\n",
    "printf(\"%f = %f\\n\", a, *b); // 0.500000 = 0.500000\n",
    "*b = 0.8; // Value of a will be changed\n",
    "printf(\"%f = %f\\n\", a, *b); // 0.800000 = 0.800000\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Passing pointer as function parameter\n",
    "\n",
    "Such usage of pointers, as it was shown, is useless\n",
    "\n",
    "Though, if you want to change value of the variable, pointer is your best friend\n",
    "```c\n",
    "void change_me(int* a, int b) {\n",
    "    *a += b;\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Following example will change value of variable\n",
    "```c\n",
    "int a = 5;\n",
    "\n",
    "change_me(&a, 3);\n",
    "\n",
    "printf(\"%d\\n\", a); // will print 8\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Dynamic arrays\n",
    "\n",
    "Now we are ready to see dynamic arrays\n",
    "\n",
    "Size of dynamic array can be set during runtime, because it's just a pointer to allocated part of memory"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Allocate memory\n",
    "\n",
    "Function `malloc` (memory allocation) allocates memory for array and returns pointer to its start\n",
    "\n",
    "It accepts needed memory size in bytes, so we need to multiply number of elements in array by size of its type\n",
    "```c\n",
    "int* a = malloc(sizeof(int) * 10);\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Use array\n",
    "\n",
    "Usage of dynamic array is the same as of static\n",
    "```c\n",
    "a[0] = 15;\n",
    "printf(\"%d\\n\", a[0]);\n",
    "```\n",
    "\n",
    "The main difference is: you should free the memory after allocation"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  },
  "widgets": {
   "state": {},
   "version": "1.1.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
<commit_msg>Added newline in C slideshow to see underscore<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Variables\n",
    "\n",
    "`C` language uses static typing, which means that you should specify type of variable on its declaration\n",
    "\n",
    "This may look familiar to you\n",
    "```c\n",
    "int a; // Create integer 'a'\n",
    "```\n",
    "\n",
    "You can assign value immediately\n",
    "```c\n",
    "int k = 10;\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Integer data types\n",
    "\n",
    "Types arranged from the smallest to the biggest\n",
    "- char\n",
    "- short\n",
    "- int\n",
    "- long\n",
    "- long long"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Unsigned and signed\n",
    "\n",
    "You can attach prefix `unsigned` to denote that you want to use non-negative integers\n",
    "```\n",
    "unsigned long t = 0;\n",
    "```\n",
    "\n",
    "Prefix `signed` used to have guarantees, that variable can contain negative values as well\n",
    "```\n",
    "signed char p = 7;\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Int\n",
    "\n",
    "Note that `unsigned` or `signed` without type name means `unsigned int` and `signed int` respectively\n",
    "```c\n",
    "unsigned amount = 150;\n",
    "signed height = -1;\n",
    "signed weight = 1300;\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Floating point data types\n",
    "\n",
    "Floating point literals can have suffix to avoid ambiguities in functions calls\n",
    "\n",
    "|name | size (bits/bytes) | suffix |\n",
    "| --- | ---- | ------ |\n",
    "| float | 32/4 | f |\n",
    "| double | 64/8 |  |\n",
    "| long double | 80/10 | L |\n",
    "\n",
    "If no suffix specified for literal, `double` type used.\n",
    "\n",
    "```c\n",
    "float width = 1.15;\n",
    "float price = 1.0f;\n",
    "long double frequency = 1.0L;\n",
    "double temp = 0.0;\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Functions\n",
    "\n",
    "Here is the simplest function, which does nothing\n",
    "```c\n",
    "void f() {\n",
    "}\n",
    "```\n",
    "\n",
    "Every function should have strictly specified type of returned value\n",
    "\n",
    "Sometimes it's needed to use function, which doesn't return any values &mdash; in this case you can use `void` type.\n",
    "\n",
    "```c\n",
    "void f() {\n",
    "    return; // This is correct too\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Function return type\n",
    "\n",
    "It's time to write something better than stub\n",
    "```c\n",
    "int get_five() {\n",
    "    return 5;\n",
    "}\n",
    "```\n",
    "\n",
    "Almost like in Python\n",
    "```python\n",
    "def get_five():\n",
    "\n",
    "    return 5\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Function parameters\n",
    "\n",
    "Function parameters should have type too\n",
    "```c\n",
    "int add(float a, long b) {\n",
    "    return a + b;\n",
    "}\n",
    "```\n",
    "\n",
    "A bit harder than in `Python`, but still good\n",
    "```python\n",
    "def add(a, b):\n",
    "    return a + b\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Calling functions\n",
    "\n",
    "Just like in `Python`, but don't forget about semicolons\n",
    "```c\n",
    "add(2, 2);\n",
    "int result = add(add(1.0f, 0) * 2f, 5);\n",
    "float k = add(0.0, result);\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# See results of your code\n",
    "\n",
    "`C` applications should be compiled to be executed\n",
    "\n",
    "You cannot run it in interpreter"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Print\n",
    "\n",
    "Function `printf` in `C` behaves like `print` in `Python`\n",
    "```C\n",
    "printf(\"Hello world!\\n\");\n",
    "```\n",
    "\n",
    "This function allows you to use formatted string\n",
    "```C\n",
    "printf(\"Give me %d cup(s) of tea, mr. %s\",\n",
    "        3, \"John\");\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Entry point\n",
    "\n",
    "`C` application should have a function called `main`, which will be executed on application start\n",
    "\n",
    "```c\n",
    "int main() {\n",
    "    return 0;\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Hello world\n",
    "\n",
    "We need to include header file of library with `printf` function and here we are\n",
    "```c\n",
    "#include <stdio.h>\n",
    "\n",
    "int main() {\n",
    "    printf(\"Hello world!\\n\");\n",
    "    return 0;\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Execute\n",
    "\n",
    "In Linux you should have `gcc` installed in order to compile your code\n",
    "```bash\n",
    "gcc file.c -o app\n",
    "```\n",
    "\n",
    "and execute\n",
    "```bash\n",
    "./app\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Conditions\n",
    "\n",
    "Conditions in `C` are very similar to those in `Python`\n",
    "```c\n",
    "if (true) {\n",
    "    printf(\"That's true\\n\");\n",
    "} else if (false) {\n",
    "    printf(\"This shouldn't be done\\n\");\n",
    "} else {\n",
    "    printf(\"Are you kidding me?\");\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Switch-case\n",
    "\n",
    "If you have checks like\n",
    "```c\n",
    "int a = 5;\n",
    "if (a == 0) printf(\"Zero\\n\");\n",
    "else if (a == 5) printf(\"Five\\n\");\n",
    "else printf(\"Another variant\\n\");\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "In `C` language you can do this\n",
    "```c\n",
    "int a = 5;\n",
    "switch(a) {\n",
    "    case 0:\n",
    "        printf(\"Zero\\n\");\n",
    "        break;\n",
    "    case 5:\n",
    "        printf(\"Five\\n\");\n",
    "        break;\n",
    "    default:\n",
    "        printf(\"Another variant\\n\");\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Seems not so pretty, but it's better to use `switch-case` instead of `if`, when you want to check, for example, which key was pressed in your videogame\n",
    "```c\n",
    "switch(key) {\n",
    "    UP:\n",
    "        move_hero_up(hero);\n",
    "        break;\n",
    "    DOWN:\n",
    "        move_hero_down(hero);\n",
    "        break;\n",
    "    default:\n",
    "        hero_stay(hero);\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Loops\n",
    "\n",
    "`C` language has following loop types\n",
    "- `for`\n",
    "- `while`\n",
    "- `do-while`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Increment and decrement\n",
    "\n",
    "There's a shorthand for numbers increment or decrement by `1` in `C` language\n",
    "```c\n",
    "i++; // Increment i\n",
    "a--; // Decrement a\n",
    "++b; // Increment b\n",
    "--c; // Decrement c\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Post and pre\n",
    "\n",
    "Post increment like `i++` means, that addition will be performed after other operations in current expression\n",
    "```c\n",
    "int i = 5;\n",
    "printf(\"%d\\n\", i++); // Will print 5\n",
    "printf(\"%d\\n\", i); // Will print 6\n",
    "```\n",
    "Pre increment written ad `++i` will increment before expression evaluation\n",
    "```c\n",
    "int i = 5;\n",
    "printf(\"%d\\n\", ++i); // Will print 6\n",
    "printf(\"%d\\n\", i); // Will print 6\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## While\n",
    "\n",
    "Just like in `Python`, but in `C` we have handy increment\n",
    "```c\n",
    "int i = 0;\n",
    "while (i < 10) {\n",
    "    printf(\"%d\\n\", i);\n",
    "    i++;\n",
    "}\n",
    "```\n",
    "\n",
    "`Python` code\n",
    "```python\n",
    "i = 0\n",
    "while i < 10:\n",
    "    print i\n",
    "    i += 1\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## do-while\n",
    "\n",
    "Like `while`, but condition check occurs not in the start, but in the end of loop\n",
    "```c\n",
    "int i = 0;\n",
    "do {\n",
    "    printf(\"%d\\n\", i);\n",
    "} while (++i < 10);\n",
    "```\n",
    "\n",
    "In `Python`\n",
    "```python\n",
    "i = 0\n",
    "while True:\n",
    "    print i\n",
    "    i += 1\n",
    "    if i > 10: break\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## `Goes to` operator (not a `goto`, IYKWIM)\n",
    "\n",
    "We can not only increment number by `++`, but decrement with `--` as well and this means, that following is correct\n",
    "```c\n",
    "int i = 10;\n",
    "while (i --> 0) {\n",
    "    next_step();\n",
    "}\n",
    "```\n",
    "\n",
    "What happens:\n",
    "- we have `i` equal to `10`\n",
    "- on each loop iteration we compare it to `0` and decrement"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## For loop\n",
    "\n",
    "This loop in `C` is not like `for` in `Python`\n",
    "```c\n",
    "for (int a = 0; a < 10; a++)\n",
    "    printf(\"Number %d\\n\", a);\n",
    "```\n",
    "\n",
    "This means\n",
    "```c\n",
    "{\n",
    "    int a = 0;\n",
    "    while (a < 10){\n",
    "        printf(\"Number %d\\n\", a);\n",
    "        a++;\n",
    "    }\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Arrays\n",
    "\n",
    "Arrays in `C` are not similar to lists in `Python`, because they should have fixed size and type of each element"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Static arrays\n",
    "\n",
    "Such arrays can have constant size, which cannot be defined during runtime\n",
    "\n",
    "This is useful for strings, which is an array of `char`\n",
    "```c\n",
    "char hello_world[] = \"Hello world\";\n",
    "```\n",
    "Also you can write\n",
    "```c\n",
    "char hello_world[] = {'H', 'e', 'l', 'l', 'o', ' ',\n",
    "                      'w', 'o', 'r', 'l', 'd', 0};\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Also you may want to create an array of numbers\n",
    "```c\n",
    "float steps[3] = {1.0, 2.0, 5.5};\n",
    "```\n",
    "You can fill not all values at one time and take care of them later\n",
    "```c\n",
    "int numbers[5] = {0, -14, 17};\n",
    "numbers[4] = 16;\n",
    "numbers[3] = 5;\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Loop through array\n",
    "\n",
    "You can give size of static array by `sizeof` operator\n",
    "\n",
    "It will give you size of all array in bytes and you will need to divide it by size of its elements\n",
    "```c\n",
    "int n[] = {1, 2, 3};\n",
    "for (int i = 0; i < sizeof(n)/sizeof(n[0]); i++)\n",
    "    printf(\"%d\\n\", n[i]);\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Pointer\n",
    "\n",
    "Pointer is just a type of variable, which contains address of byte in memory"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Address of variable\n",
    "\n",
    "Every variable has an address and you can get it by `&` sign like `&var_name`\n",
    "```c\n",
    "int a = 0;\n",
    "printf(\"Address of a is %X\\n\", &a);\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Creating pointer\n",
    "\n",
    "You can store this address in separate variable\n",
    "\n",
    "Pointer to variable of some type should have a type of that variable with `*` sign\n",
    "```c\n",
    "float a = 1;\n",
    "float* b = &a; // Now b contains address of a\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Working with memory pointed by pointer\n",
    "\n",
    "You can access the value pointer points to with `*` sign\n",
    "```c\n",
    "double a = 0.5;\n",
    "double* b = &a;\n",
    "printf(\"%f = %f\\n\", a, *b); // 0.500000 = 0.500000\n",
    "*b = 0.8; // Value of a will be changed\n",
    "printf(\"%f = %f\\n\", a, *b); // 0.800000 = 0.800000\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Passing pointer as function parameter\n",
    "\n",
    "Such usage of pointers, as it was shown, is useless\n",
    "\n",
    "Though, if you want to change value of the variable, pointer is your best friend\n",
    "```c\n",
    "void change_me(int* a, int b) {\n",
    "    *a += b;\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Following example will change value of variable\n",
    "```c\n",
    "int a = 5;\n",
    "\n",
    "change_me(&a, 3);\n",
    "\n",
    "printf(\"%d\\n\", a); // will print 8\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Dynamic arrays\n",
    "\n",
    "Now we are ready to see dynamic arrays\n",
    "\n",
    "Size of dynamic array can be set during runtime, because it's just a pointer to allocated part of memory"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Allocate memory\n",
    "\n",
    "Function `malloc` (memory allocation) allocates memory for array and returns pointer to its start\n",
    "\n",
    "It accepts needed memory size in bytes, so we need to multiply number of elements in array by size of its type\n",
    "```c\n",
    "int* a = malloc(sizeof(int) * 10);\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Use array\n",
    "\n",
    "Usage of dynamic array is the same as of static\n",
    "```c\n",
    "a[0] = 15;\n",
    "printf(\"%d\\n\", a[0]);\n",
    "```\n",
    "\n",
    "The main difference is: you should free the memory after allocation"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  },
  "widgets": {
   "state": {},
   "version": "1.1.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}

{'fraction_non_alphanumeric': 0.23322546464675392, 'fraction_numerical': 0.009335891634926544, 'mean_word_length': 1.9668649107901444, 'pattern_counts': {'":': 420, '<': 17, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2050889', 'n_tokens_mistral': 14220, 'n_tokens_neox': 13581, 'n_words': 3324}