.. include:: Programmieren mit C (2021-06-14 - 2021-06-18) ============================================= .. sidebar:: *Training Information* **Start** Montag 14.6.2021, 9:00 **Offizielle Beschreibung** `tecTrain `__ **Material** * Slides, :download:`C ` * Slides, :download:`CMake ` * Slides, :download:`GNU Make ` * `Github Repository `__ **CPA Exam** * `Sample Questions `__ .. contents:: :local: Preparation: Tools ------------------ * `C/C++ for Visual Studio Code `__ * `Get started with CMake Tools on Linux `__ * `CMake download `__ Log --- Day 1 ..... * Introduction (from the :download:`C slides `) * Hello World * Variables and Arithmetic * ``for`` Loops * Symbolic Constants * Character I/O * Occasional live hacking * `hello.c `__ * `fahr.c `__ * `fahr-for.c `__ * `wc.c `__. This is going to be morphed into a formal state machine tomorrow. Day 2 ..... State machine ````````````` It turns out that the correct solution to exercise on slide 37 of :download:`the slide deck ` is a *state machine*. It enables us to correctly cover all corner cases (e.g. multiple consecutive spaces between two words) ... .. image:: state-machine.jpg :scale: 20% * `wc.c `__ became a state machine, implementing the above state chart. Using ``if-then-else`` to cover the states. * Introduced the ``switch`` statement (slightly :-) deviating from the regular course flow); changed the state maching to use that instead of ``if-then-else``. See `wc-switch.c `__ Regular Course Topics ````````````````````` * Introduced pre and post increment operators (again severely deviating). See `pre-post-increment.c `__. * Ah, arrays: leading to the "histogram" exercise (slide 41 from the :download:`slides `). Solution see `here on Github `__ * Functions. Slides 44ff. from the :download:`slides ` (the ``power()`` function from the K&R book) as a live hacking gig. `See here on Github `__. * Quick :download:`CMake ` intro, as a preparation for the upcoming group project. Confusing people. Day 3 ..... Development Tools (Git & CMake) Installation Massacre ````````````````````````````````````````````````````` * *Git*. See `here `__ for instructions about how to use Git with VS Code. It turns out that * `Git Extension Pack `__ guides users through workflows pretty well. Staging, commits, merges. * Except that we appear to have problems with Github authentication, which is why we use `Github Desktop `__ for remote stuff like clone, push, pull. * *CMake*. `Doze installation `__; use the MSI (the installer package). Regular Course Material ``````````````````````` * Fast run-through: slides 64-75 * Stop at enums/76: morph wc.c state machine into a perfect enum candidate. This is the time to ... * Show how to use ``enum`` in the state machine from above. * Enable ``-Wswitch-enum`` (nowadays included in ``-Wall``, apparently) `CMakeLists.txt `__ * Show how ``default:``-less ``switch`` statements, ``enum``s, and ``-Wswitch-enum`` can work together and help me maintain code better. Program see `here `__. * "Variable Definitions" section. Slides 77ff. Emphasize on local variables (this is what we saw so far) more. Lifetime, initialization, etc. * Almost skip 81-88. Operators, boring. * Hard stop at "Type Conversions", slides 89ff. Make people run away screaming (intentionally). Group Project Kick-Off :-) `````````````````````````` Kick off "group project". Git, CMake, and team development, with rather artificial use cases. But anyway, these are use cases. See `here `__. *Exercise*: in the ``group-project/`` directory, * Build a program that uses *declarations* from ``db.h``, to * Iterate over what is in the user database |longrightarrow| ``userdb`` alongside ``num_users``, *external variables*. * Write CSV output. * Add build instructions to ``group-project/CMakeLists.txt``. * When done, commit and push to Github. Day 4 ..... Regular Course Material ``````````````````````` * Repeat ``external``, local vs. global variables, visibility * Pointers: 199ff from the :download:`slides `. Address space and pointers: .. figure:: address-space-and-pointers.jpg :scale: 40% Entire address space * Pointers: slide 203, "More Examples": `pointers.c `__ .. figure:: take-address.jpg :scale: 60% * Slide 204ff, "Pointers as Function Parameters": `pointers-as-function-parameters.c `__ * Slide 208ff, "Pointers and Arrays": `pointer-arrays-arithmetic.c `__ * Slide 220ff, "Commandline": `pointers-argv.c `__ * C++: STL, Iterators, and their relation to pointer arithmetic. `pointer-array-in-c++.cpp `__. * ``struct`` chapter, slides 222ff * Live-hack the ``struct point`` story from the slides. Show how an API could look like. `point.c `__. Exercise: Encapsulate Index Access to ``userdb`` ```````````````````````````````````````````````` * In your CSV export programs, replace the direct index access to ``userdb`` with the API function ``db_get_user_at_index()`` from ``db.h``. * ``userdb`` must not be seen anymore in any user code. .. figure:: pointer-to-user-struct.jpg :scale: 60% Group Project (Use Case Driven Course Flow :-) ) ```````````````````````````````````````````````` * Hide implementation details: use ``static`` for the ``userdb`` array. (More on slides 298ff.) * Add user record to DB. Have to use ``strcpy()``. Here a sketch of the internal "organisation". .. image:: db-extend.jpg :scale: 40% Day 5 ..... Pointer Recap ````````````` * ``strlen()``. `Man page `__ .. figure:: strlen.jpg :scale: 60% * ``strcpy()``. `Man page `__. Here is how to produce undefined bahavior by allocating too less space for the target string. .. literalinclude:: strcpy-undefined-behavior.c :caption: :download:`strcpy-undefined-behavior.c` .. figure:: strcpy-overwrite.jpg :scale: 60% * Pointer massacre: ``strtol()``. `Man page `__. Live hacking example: `str-to-int-conversion.c `__ .. figure:: strtol.jpg :scale: 60% On With Group Project ````````````````````` * Show how *error codes* can be implemented * Error code definition using an ``enum``. `db.h `__, ``enum Error``. * Error code stringification, ``db_error_string()``. * `db.h `__, declaration. * `db.c `__, definition. Note how we are using a ``default``-less ``switch`` statement, together with ``-Wall`` (implicitly enabling ``-Wswitch-enum``) in the `toplevel CMakeLists.txt `__. * `Valgrind `__. Here's a `YouTube Video `__ on it. .. raw:: html * File IO: binary vs. text. Here's a `YouTube Video `__ on it. .. raw:: html * CMake: show how you build and use libraries. `group-project/CMakeLists.txt `__. Further Information ------------------- Here's some literature :-) to study as a preparation for our upcoming C++ course. Unfortunately, there are no absolute high-quality tutorial videos out there - one reason might be that those languages are not as sexy anymore as, for example, Python (where you can pick your favorites from a large pool of high quality material). C++ Basics .......... * `C++ Strings - Intro and Comparison to C Strings `__. `Caleb Curry `__. .. raw:: html * `C++ Examples - Pass by Value vs Reference vs Pointer `__. `Caleb Curry `__. .. raw:: html Pointers, Pointer Arithmetic, and the Standard Template Library (STL) ..................................................................... * `C++ Pointers - Finally Understand Pointers `__. `Caleb Curry `__. .. raw:: html * `Pointer Arithmetic `__. Pointer Arithmetic in C; good to understand before going into C++ containers and iterators. .. raw:: html * `STL Containers - Learn Modern C++ `__. A somewhat boring but comprehensive overview of available C++/STL datastructures *and their runtime behaviour*. .. raw:: html * `STL Vector `__. .. raw:: html * `STL Map `__. .. raw:: html * `STL Set `__. .. raw:: html