Termbank
  1. A
    1. Abstraction
    2. Alias
    3. Argument
    4. Array
  2. B
    1. Binary code file
    2. Binary number
    3. Bit
    4. Bitwise negation
    5. Bitwise operation
    6. Byte
  3. C
    1. C library
    2. C-function
    3. C-variable
    4. Character
    5. Code block
    6. Comment
    7. Compiler
    8. Complement
    9. Conditional statement
    10. Conditional structure
    11. Control structure
  4. D
    1. Data structure
    2. Duck typing
  5. E
    1. Error message
    2. Exception
  6. F
    1. Flag
    2. Float
  7. H
    1. Header file
    2. Headers
    3. Hexadecimal
  8. I
    1. Immutable
    2. Initialization
    3. Instruction
    4. Integer
    5. Interpreter
    6. Introduction
    7. Iteroitava
  9. K
    1. Keyword
  10. L
    1. Library
    2. Logical operation
  11. M
    1. Machine language
    2. Macro
    3. Main function
    4. Memory
    5. Method
  12. O
    1. Object
    2. Optimization
  13. P
    1. Parameter
    2. Placeholder
    3. Pointer
    4. Precompiler
    5. Precompiler directive
    6. Prototype
    7. Python console
    8. Python format
    9. Python function
    10. Python import
    11. Python list
    12. Python main program
    13. Python variable
    14. Python-for
    15. Pääfunktio
    16. printf
  14. R
    1. Resource
    2. Return value
  15. S
    1. Statement
    2. Static typing
    3. String
    4. Syntax
  16. T
    1. Terminal
    2. Type
    3. Typecast
  17. U
    1. Unsigned
  18. V
    1. Value
  19. W
    1. Warning
    2. while
Completed: / exercises
NOTE: The project is found at Github

Session 1

Step 1: Virtual machine

For this laboratory exercise, you can use your own computer, or university workstation. We strongly recommend using the university workstations - this way we can ensure that everything works as intended. In any case, at this stage only one member of the team needs to do this setup.

If you use your own computer

Install VirtualBox along with VirtualBox Extension Pack. You can install the Extension Pack by following these instructions. After installing these, you can follow the same instructions as the students using university workstations (below).

If you use university workstation

Download the virtual appliance (.ova-file). Files and history can be found in Lovelace
In VirtualBox, choose File -> Import appliance and find the previously downloaded .ova-file. In the next screen, choose the directory you want to place the file in, and press "Import".
After some time, you will see a screen like the one below. Choose the freshly imported virtual machine and press "Start" at the top right.
"Virtualbox VM"
Wait (patiently) for the virtual machine to start. In the meanwhile answer the attendance sheet.
Please, include the following information (one piece of information per line).
- Attendance code that appears on the whiteboard.
- Name and surname of each member of the team who is currently present. Please, do not include team members that are not in the class right now.

Warning: You have not logged in. You cannot answer.

Step 2: Introducing Visual Studio Code

Starting up

1. Open Visual Studio Code. This will be our IDE (Integrated Development Environment), which we use for writing, compiling, debugging, and flashing our device (Raspberry Pi Pico W).
"VScode icon"
The welcome screen shows the options available. Initially, you can choose to Open Folder, Clone Repository, or Create Pico Project from scratch.
"VSCode Welcome Screen"
We are not going to create one from scratch, because configuring a new project requires some additional steps that we will skip for now. Instead, we will download the project from the GitHub repository.
Before doing that, let’s explore the different tools available in the menu on the left.
"VSCode Welcome Screen"
The tools that we are going to use most are Explorer, where we edit our code, and Source Control, which is used if you are working with Git.
2. Let’s download our default project. You have two options. The first one (recommended by the course staff) is using git, if you have a GitHub account. The second one, if you do not have a GitHub account or do not want to use Git, is to download the files directly.
In any case, knowing Git is not required for this course, but it is a tool that you will definitely need later in your studies, since it is widely used in industry. Moreover, we will use a graphical interface, so you do not need to memorize command-line commands.

Option 1: Using GitHub version control system

For this option you should have a Github account.
2.1 Open the repository and fork it by pressing the Fork button at the top-right corner. Only one team member should do this; the others can then join the project.
After you have forked the repository, you no longer need to access the original one — you will use your personal or group repository that was just created.
"Fork button"
2.2 The next step is to clone the repository. Open your forked project and copy the repository URL by clicking the Code button. Select the HTTPS URL and copy it.
"Cloning repo."
2.3 Now press the Clone Repository button. In the Command Palette, choose Clone with GitHub, and then paste the URL of your personal repository.
"Cloning repo." "Cloning repo."
2.4 You might be asked to add your Github account in a new browser window.
2.5 Select the destination folder for the repository. It should be inside /home/student/jtkj-projects. Click Set as repository folder. When asked, choose Yes, I trust the authors. The system will start downloading the necessary libraries.
If you are asked which "kit" to use, select "Pico".
2.6 The system is now ready. Check that the terminal at the bottom shows the following text:
"Downloading repo."

Option 2: Download the folder to your computer

NOTE: Use this option only if you did not choose Option 1.
2.1 Open the repository in Firefox (use the browser in the virtual machine).
2.2 Press the green Code button, then select Download ZIP in the new window. This will download a ZIP file to /home/student/Downloads.
"Downloading repo."
2.3 Extract the folder into your /home/student directory. We recommend using the folder /home/student/jtkj-projects.
2.4 In Visual Studio Code, press the Open Folder button. Select the folder where you extracted the repository and click Open.
When asked, press Yes, I trust the authors. The system will start downloading the necessary libraries.
If you are asked which "kit" to use, select "Pico".
"Downloading repo."
2.5 The system is now ready. Check that the terminal at the bottom shows the following text:
"Downloading repo."

Exploring VSCode

If you have followed carefully previous instructions the Explorer present the following folders:
"Explorer windows"
At the bottom we can see an important bar that we are going to use a lot:
"Bottom bar"
The button that reads [all] indicates which is the target that we are compiling. The target is the application which contains the main() function you are compiling. We will explain later how do you know which are the possible targets (small advance, you would need to check the CMakeList.txt) The two buttons on the right, are used to compile and flash the compiled code into the device.
Let's first quickly compile and upload the first code to the Raspberry Pi Pico.
Open the file main.c from the directory src. Have a quick look the file and try to understand the structure. Remember that we run always the main function first. This code will just toggle the Raspberry Pi Pico LED every second. The code is a bit strange, because the Raspberry Pi Pico W controls the LED using the networking chip (strange, right?), but if you read the comments it should be easy to follow.

Compiling and uploading code

First, we need to select which target we are going to compile. On the bottom bar, you can see the text Build and next to it all. If we keep it as all, we are compiling the entire project (including libraries and example applications). Let’s do that now, as it will save time later. Since we are compiling many files, the process may take a while.
"Bottom bar"
To start the compilation, press the Compile button on the right side of the bottom bar. If you are asked for a launch target for project jtkj_project, select hello_blink. The launch target indicates which application will be uploaded to the device when we press run.
For now, let’s focus on compilation. Select hello_blink as the launch target and let the system compile all your source files. You can see in the terminal how the .o files are being generated.
At the end of the process, you should see a Build completed message, and it should return with status code 0 (meaning everything is OK). If that is not the case, contact the course staff.
"Successful compilation"
Now your files have been compiled, and the executable/binary file (in this case a .bin, .elf, or .uf2 file, depending on the flashing method you are using) is stored in the build folder. You do not need to worry about the exact location because the VS Code extension handles that for you.
Now that we have the file, we still need to upload it to the device. The process is a bit tricky, so please follow the next steps carefully.
1. Connect the USB cable to your Pico, press the BOOTSEL button (image below), and while holding it, connect the other end of the USB cable to the PC.
By doing this, we put the Pico in USB Mass Storage (bootloader) mode. In this mode, the Pico appears as a removable USB drive named RPI-RP2, which allows uploading the binary file. Whenever you want to upload new software, you must disconnect the USB and press the BOOTSEL button. Later we will see that this step is sometimes necessary only the first time you connect the Pico to the computer.
"Pico BOOTSEL-button"
2. After the Pico is connected to the computer via the USB cable, it will appear in the Devices -> USB menu of VirtualBox as Raspberry Pi RP2 Boot.You need to explicitly select this device so the virtual machine can interact with the Pico.
"Connecting USB to virtual machine"
NOTE: You can make the VM automatically recognize your device when it is connected by selecting Devices > USB > USB Settings in your VM menu and adding the device to the USB Device Filters (see image below).
"Connecting USB to virtual machine"
3. Once your device is recognized by the Virtual Machine (NOT by the host computer), press the Run button on the bottom bar.When the binary file is uploaded to the device, you should see a success message (see below). In addition, you should see the LED on your Pico blinking every second.
"Successfully uploaded"
4. Now the device appears as a different USB device to the VM. You should repeat step 2, but this time select Raspberry Pi Pico [0100] as the device.
You should notice that the bottom bar now shows a new element:
"Compilation and Launch target"
The target on the left is the Compilation target, and the one on the right is the Launch target. This means that from now on, we do not need to compile the entire project, only the specific targets we modify. We can also choose which target to upload to the device. This is useful later when you have, for example, different versions of the software to test.
If you want, you can select [hello_blink] as the compilation target, modify part of the code (for example, change the value of LED_DELAY_MS), compile, and run again. Try first running the code without disconnecting the USB cable. If you have followed the previous steps, it should not be necessary to remove the Pico from the device.

Using the terminal

Ok, ok. But the main.c is using printf, and you mentioned in the lectures that we are going to use printf and puts to debug our code. So, we should be able to see the printed messages. Actually, you can use any serial terminal. The virtual machine has screen and minicom already installed. But this time, we are using the VSCode integrated serial monitor. Open the "Serial Monitor" tab located at the bottom of the screen.
"VSCode serial monitor"
When you open the tab, all settings should already be correct (Monitor mode: serial, View mode: text, Baud rate: 115200, Line ending: None). You just need to select the correct serial port from the "Port" menu. In the Linux operating system used by our virtual machine, this is usually /dev/ttyACM0 or /dev/ttyACM1.
If the Pico does not appear in the menu, check that it is connected to the virtual machine from VirtualBox’s Devices -> USB menu, and then press the refresh button next to the "Port" menu.
Once the correct port is selected, you only need to press the blue "Start monitoring" button, and the serial connection will open. If everything works as expected, you should see a Tick message every second, just as it was programmed in the code.
"Output in serial monitor"
We recommend activating Toggle Autoscroll so that the terminal always displays the latest line sent by the Pico, and also Toggle Automatic Reconnection.
If that is activated, try disconnecting and reconnecting the Raspberry Pi Pico.
If you have followed the previous steps and the USB device is already added to the VM’s filter list, the Serial Monitor should automatically reconnect.

A walk through the CMakeLists. Executing examples

We promised you that we were going to explain in a bit more detail the CMakeLists.txt. This file is a configuration file used by CMake, the build automation tool we are using for the Pico. CMake is a build automation tool that helps developers compile programs automatically on different systems. Instead of writing long and platform-specific build commands by hand, CMake lets you describe your project once (in the CMakeLists.txt), and then it generates the correct build system for your platform. It helps detect compilers and system paths, manage dependencies, and it works across different operating systems.
The CMakeLists.txt performs the following functions:
We will see that we can have several nested CMakeLists.txt files: one that defines the general settings, and others that define settings for specific folders (for example, a folder containing additional examples).
Let's have a quick look at the CMakeLists.txt file located in the root folder. Open the CMakeLists.txt. The file has the following sections:
Right now, we are not going to modify it, but in the next step, you might need to make a small change in the file to run the code. Stay tuned!
At some point, we mentioned that you could use the CMakeLists.txt to generate additional targets. In the root CMakeLists.txt, there are several add_subdirectory() commands. When one of those subdirectories is opened, we expect to find another CMakeLists.txt file inside that provides additional build instructions.
The examples folder includes some additional examples (more will come in Lecture 2). Please, in the Explorer view, open the hello_blink_freertos folder. You can see a new CMakeLists.txt file and a src folder containing main.c. If you open the main.c file, you will notice that it is exactly the same code currently running on your Pico, but this time it uses FreeRTOS (we create only one task). If you open the CMakeLists.txt file, you will see that it contains the four instructions we summarized before. In this case, the target name is hello_blink_freertos, and the target_link_libraries section includes the libraries FreeRTOS-Kernel and FreeRTOS-Kernel-Heap4, which are required to call FreeRTOS functions (see the #include "FreeRTOS.h" and #include "task.h" lines in main.c).
Since you know the target name, please, compile and run the hello_blink_freertos target. The LED on the board should start blinking. Open the Serial Monitor. You should now see the text Tack FreeRTOS printed every second.
If you have time, you can also explore and run the following examples:
The others are a little bit more complex, and we will explore them in the next session.

Solving compilation problems

Imagine the following scenario: a first-year student was helping with the programming, but wrote the code with several errors. Your task is to find and fix those errors so the project compiles, runs on the device, and prints meaningful output.
The broken code is in the folder /examples/compilation_errors. You can either copy the code into your main target under src/, or use the CMakeLists.txt provided inside /examples/compilation_errors to build that folder as its own target.
Please, be sure that you execute following tasks.
1. Uncomment the line add_subdirectory(examples/compilation_errors) in the root CMakeLists.txt
2. Open the files in /examples/compilation_errors and read the error messages shown in the terminal when you try to build.
3. Identify and fix the main problem in the code, by checking and understanding compilation errors and warning.
4. Rebuild the project until the build completes with status code 0 and no warnings.
5. Flash and run the corrected program on the device.
6. Open the Serial Monitor and verify that the output appears once the program is running.
If everything is fixed and working, you should see lines similar to the following in the serial monitor:
time:2494951,temp:19,lux:1281
time:2496451,temp:18,lux:791
time:2497951,temp:34,lux:575
When you change the CMakeLists.txt you might need to reload the cache. To do that, press F1 (or Ctrl+Shift+P) and select: CMake: Delete Cache and Reconfigure. This will reload the build folder. Sometimes, you need to even remove the whole build folder, but let's hope we do not need to do that in this exercise.
When you finish upload the file main.c already with changes done to the following return box.
Please, upload the main.c file after correcting all the problems.
Warning: You have not logged in. You cannot answer.

Visualizing data using Arduino plotter

Sometimes, we would like to visualize a graph with the sensor data. One option would be to print the values in the terminal, copy them to a file, and then process them using any Time Series tool. However, Arduino environment offer a very simple tool for doing that: The Serial Plotter.
In order to make it work we would need to send a string of the following format:
variable1:xxx,variable2:xxx\n.
First step is modifying your code so the output from the printTask is the following:
temp:xxx,lux:yyy\n where xxx and yyy are the values coming from the sensors (in this case stored in global variables).
Check that is working using the Serial Monitor, and Stop Monitoring
After that, open Arduino IDE environment and follow next steps:
This does not allow operate with the data, nor for instance, define the time scale. But might be interesting to detect thresholds or some basic behaviours.
Please, take a screenshot of the figure you have obtained and include it in the following return box.

Printing variables

Upload a screenshot of the windows showing the figure you have generated using Arduino serial plotter. Save it as .png file with low resolution but with a readable text. You need to return a .zip file containing the .png
Max size: 2.5MB
Warning: You have not logged in. You cannot answer.

Using external libraries. The serial-usb-debug

Great progress. But know, let's explain how to include libraries that are defined in the same project. We are going to provide you with libraries for WiFi connectivity, to interact with the Pico HAT (much more of this in session 2) and to create two different serial communication ports: one for writing debug messages and the other to send raw data (e.g sensor data).
We have defined libraries in the same way as we have defined examples. In this way the libraries are in their respective folder inside libs. If you open usb-serial-debug you observe similar structure to a project, but the CMakeLists.txt is slightly different (Come on!, open the CMakeLists.txt inside the usb-serial-debug, if you have not done it yet). Instead of using add_executable we use add_library. Libraries do not have a main so they cannot be a "Launch target", but they can be a compilation target.
The name of the library is given also in the add_library. Observe how the target_link_libraries have PUBLIC libraries (are included also to the program using current library) and PRIVATE libraries (they are internal from this library and are not shared with others).
In other to show how the library is working, we have included an example: hello_dual_cdc. If you open its CMakeLists.txt you can see that the target_link_libraries includes the usb_serial_debug. If you want to include this library in your project, you should do something similar. If you open the src/main.c in the hello_dual_cdc you can see that it includes #include "usbSerialDebug/helper.h". Where does this path comes from, well if you open the library hello_dual_cdc and its include folder that is the one that is included in the library. This folder contains another folder named usbSerialDebug that is the one we are referring. It looks a bit complicated, but if you understood how libraries work in C should be easy to grasp.
You can have a look to the code. It is explained in the Serial Communication Chapter. At some point you should be able to understand what is happening there for your final project. But today we are just interested in running the code and visualizing the data. JUST ONE IMPORTANT REMINDER, IF YOU ARE USING THIS LIBRARY, YOU CANNOT USE ANY OF THE STDIO FUNCTIONS, INSTEAD YOU SHOULD USE THE usb_serial_print FUNCTION
Another important note, as it can be seen in the CMakeLists.txt, we are not redirecting the stdio to the USB, that is why the pico_enable_stdio_usb is set to 0.
Compile and run the hello_dual_cdc target. This time you should open two different serial monitors: one for /dev/ttyACM0 (debug) and the other /dev/ttyACM1 to send raw data.
IMPORTANT NOTE: Since the library modifies the USB profile of the device, it is is recognized with a different name: University of Oulu TKJHAT. You need to mark this device in the VM -> Devices USB
Take a screenshot of your screen showing the two serial monitors running in parallel.

Two serials

Upload a screenshot of the windows showing the two serial ports opened and transmitting data. Save it as .png file with low resolution but with a readable text. You need to return a .zip file containing the .png
Max size: 2.5MB
Warning: You have not logged in. You cannot answer.

Storing the files

If you want to save your files you have two options:
  1. Save the files that you have modified (generally only the files under your root src folder). It is a good idea to .zip them. IMPORTANT: You cannot drag&drop to the host, do not even triy, likely the SYSTEM WILL CRASH. Hence, you would need either a USB stick to save your files in some online repository (GoogleDrive, OneDrive... that can run FROM THE VIRTUALMACHINE). For your your project you can use the Temporary Code Sharing Space we created in the Module 2 Project.
  2. Use the proper method: GIT (AGAIN, THERE ARE OTHER ALTERNATIVES, THIS IS NOT A MANDATORY METHOD, BUT RECOMMENDED).
During this lecture we do not need to save anything else, but when working on your project you might need it. So, we include here additional instructions. Just follow these instructions, if you want to push the project to the repo you likely created at the beginning of the session.
The mandatory part of the exercise is finished. You can read further if you want to learn GIT in more depth.

APPENDIX: Storing files with GIT

git config --local user.email "myemail@gmail.com"
git config --local user.name "Myname"
git config --local --list
"VSCode Git icon"
Cloning
If you are continuing your work in a new virtual machine or device, you need to clone the project repository, so that you get a copy of the latest version of your code and its change history. Cloning happens the easiest by using the GitHub-integration of VSCode (if your repository is saved in GitHub).
Upon opening VSCode in the new virtual machine, you can clone a repository located in GitHub by clicking "Clone Git Repository" at the start screen of VSCode.
"Clone GitHub repository from VSCode"
Following view will open at the top of the window:
"VSCode repository selector"
Here we choose the option "Clone from GitHub", after which the browser opens and you can login to GitHub. Follow the instructions given, and give VSCode the access to your GitHub-repositories. After completing the login, the browser will guide you back to VSCode, where you can select a repository to clone from the list containing all of your GitHub repositories. Choose the home folder of the virtual machine as the saving location for your repository.
Synchronizing
If you are continuing your work in the same virtual machine or device as before, you need to open the previous project folder where you created the Git-repository. It is a good practice to download the latest changes from the version control system server, so that you can avoid conflicts. This process, also called synchronizing, can be done in VSCode by clicking the synchronize-button located at the bottom left corner of the window:
"VSCode synchronize repository button"
When the synchronization is ready, you have the latest version of the project's code in your local Git-repository. Now you can start making changes, and be sure that changes made by for example other group members are included.
?
Abstraction is a process through which raw machine language instructions are "hidden" underneath the statements of a higher level programming language. Abstraction level determines how extensive the hiding is - the higher the abstraction level, the more difficult it is to exactly say how a complex statement will be turned into machine language instructions. For instance, the abstraction level of Python is much higher than that of C (in fact, Python has been made with C).
Alias is a directive for the precompiler that substitus a string with another string whenever encountered. In it's basic form it's comparable to the replace operation in a text editor. Aliases are define with the #define directeve, e.g. #define PI 3.1416
Argument is the name for values that are given to functions when they are called. Arguments are stored into parameters when inside the function, although in C both sides are often called just arguments. For example in printf("%c", character); there are two arguments: "%c" format template and the contents of the character variable.
Array is a common structure in programming languages that contains multiple values of (usually) the same type. Arrays in C are static - their size must be defined when they are introduced and it cannot change. C arrays can only contain values of one type (also defined when introduced).
Binary code file is a file that contains machine language instructions in binary format. They are meant to be read only by machines. Typically if you attempt to open a binary file in a text editor, you'll see just a mess of random characters as the editor is attempting to decode the bits into characters. Most editors will also warn that the file is binary.
Binary number is a number made of bits, i.e. digits 0 and 1. This makes it a base 2 number system.
A bit is the smallest unit of information. It can have exactly two values: 0 and 1. Inside the computer everything happens with bits. Typically the memory contains bitstrings that are made of multiple bits.
Bitwise negation is an operation where each bit of a binary number is negated so that zeros become ones and vice versa. The operator is ~.
Bitwise operations are a class of operations with the common feature that they manipulate individual bits. For example bitwise negation reverses each bit. Some operations take place between two binary values so that bits in the same position affect each other. These operations include and (&), or (|) and xor (^). There's also shift operations (<< and >>) where the bits of one binary number are shifted to the left or right N steps.
Byte is the size of one memory slot - typically 8 bits. It is the smallest unit of information that can be addressed from the computer's memory. The sizes of variable types are defined as bytes.
External code in C is placed in libraries from which they can be taken to use with the #include directive. C has its own standard libraries, and other libraries can also be included. However any non-standard libraries must be declared to the compiler. Typically a library is made of its source code file (.c) and header file (.h) which includes function prototypes etc.
Functions in C are more static than their Python counterparts. A function in C can only have ne return value and its type must be predefined. Likewise the types of all parameers must be defined. When a function is called, the values of arguments are copied into memory reserved for the function parameters. Therefore functions always handle values that are separate from the values handled by the coe that called them.
C variables are statically typed, which means their type is defined as the variable is introduced. In addition, C variables are tied to their memory area. The type of a variable cannot be changed.
Character is a single character, referred in C as char. It can be interpreted as an ASCII character but can also be used as an integer as it is the smallest integer that can be stored in memory. It's exactly 1 byte. A character is marked with single quotes, e.g. 'c'.
Code block is a group of code lines that are in the same context. For instance, in a conditional structure each condtion contains its own code block. Likewise the contents of a function are in their own code block. Code blocks can contain other code blocks. Python uses indentation to separate code blocks from each other. C uses curly braces to mark the beginning and end of a code block.
Comments are text in code files that are not part of the program. Each language has its own way of marking comments. Python uses the # character, C the more standard //. In C it's also possible to mark multiple lines as comments by placing them between /* and */.
A compiler is a program that transforms C source code into a binary file containing machine language instructions that can be executed by the computer's processor. The compiler also examines the source code and informs the user about any errors or potential issues in the code (warnings). The compiler's behavior can be altered with numerous flags.
Complement is a way to represent negative numbers, used typically in computers. The sign of a number is changed by flipping all its bits. In two's complement which is used in this course, 1 is added to the result after flipping.
Conditional statement is (usually) a line of code that defined a single condition, followed by a code block delimited by curly braces that is entered if the condition evaluates as true. Conditional statements are if statements that can also be present with the else keyword as else if. A set of conditional statements linked together by else keywords are called conditional structures.
Conditional structure is a control structure consisting of one or more conditional statements. Most contrl structures contain at least two branches: if and else. Between these two there can also be any number of else if statements. It is however also possible to have just a single if statement. Each branch in a conditional structure cotains executable code enclosed within a block. Only one branch of the structure is ever entered - with overlapping conditions the first one that matches is selected.
Control structures are code structures that somehow alter the program's control flow. Conditional structures and loops belong to this category. Exception handling can also be considered as a form of control structure.
Data structure is a comman name for collection that contain multiple values. In Python these include lists, tuples and dictionaries. In C the most common data structures are arrays and structs.
Python's way of treating variable values is called dynamic typing aka duck typing. The latter comes from the saying "if it swims like a duck, walks like a duck and quacks like a duck, it is a duck". In other words, the validity of a value is determined by its properties in a case-by-case fashion rather than its type.
An error message is given by the computer when something goes wrong while running or compiling a program. Typically it contains information about the problem that was encountered and its location in the source code.
An exception is what happens when a program encounters an error. Exceptions have type (e.g. TypeError) that can be used in exception handling within the program, and also as information when debugging. Typically exceptions also include textual description of the problem.
Flags are used when executing programs from the command line interface. Flags are options that define how the program behaves. Usually a flag is a single character prefixed with a single dash (e.g. -o) or a word (or multiple words connected with dashes) prefixed with two dashes (e.g. --system. Some flags are Boolean flags which means they are either on (if present) or off (if not present). Other flags take a parameter which is typically put after the flag separated either by a space or = character (e.g. -o hemulen.exe.
Floating point numbers are an approximation of decimal numbers that are used by computers. Due to their archicture computers aren't able to process real decimal numbers, so they use floats instead. Sometimes the imprecision of floats can cause rounding errors - this is good to keep in mind. In C there are two kinds of floating point numbers: float and double, where the latter has twice the number of bits.
Header files use the .h extension, and they contain the headers (function prototypes, type definitions etc.) for a .c file with the same name.
Headers in C are used to indicate what is in the code file. This includes things like function prototypes. Other typical content for headers are definition of types (structs etc.) and constants. Headers can be at the beginning of the code file, but more often - especially for libraries - they are in placed in a separate header (.h) file.
Hexadecimal numbers are base 16 numbers that are used particularly to represent memory addresses and the binary contents of memory. A hexadecimal number is typically prefixed with 0x. They use the letters A-F to represent digits 10 to 15. Hexadecimals are used because each digit represents exactly 4 bits which makes transformation to binary and back easy.
In Python objects were categorized into mutable and immutable values. An immutable value cannot have its contents changed - any operations that seemingly alter the object actually create an altered copy in a new memory location. For instance strings are immutable in Python. In C this categorization is not needed because the relationship of variables and memory is tighter - the same variable addresses the same area of memory for the duration of its existence.
When a variable is given its initial value in code, the process is called initialization. A typical example is the initialization of a number to zero. Initialization can be done alongside with introduction: int counter = 0; or separately. If a variable has not been initialized, its content is whatever was left there by the previous owner of the memory area.
Instruction set defines what instructions the processor is capable of. These instructions form the machine language of the processor architecture.
Integers themselves are probably familiar at this point. However in C there's many kinds of integers. Integer types are distinguished by their size in bits and whether they are signed or not. As a given number of bits can represent up to (2 ^ n) different integers, the maximum value for a signed integer is (2 * (n - 1))
Python interpreter is a program that transforms Python code into machine language instructions at runtime.
The moment a variable's existence is announed for the first is called introduction. When introduced, a variable's type and name must be defined, e.g. int number;. When a variable is introduced, memory is reserved for it even though nothing is written there yet - whatever was in the memory previously is still there. For this reason it's often a good idea to initialize variables when introducing them.
Iteroitava objekti on sellainen, jonka voi antaa silmukalle läpikäytäväksi (Pythonissa for-silmukalle). Tähän joukkoon kuuluvat yleisimpinä listat, merkkijonot ja generaattorit. C:ssä ei ole silmukkaa, joka vastaisi Pythonin for-silmukan toimintaa, joten taulukoiden yms. läpikäynti tehdään indeksiä kasvattavilla silmukoilla.
Keywords are words in programming languages that have been reserved. Good text editors generally use a different formatting for keywords (e.g. bold). Usually keywords are protected and their names cannot be used for variables. Typical keywords include if and else that are used in control structures. In a way keywords are part of the programming language's grammar.
A library is typically a toolbox of functions around a single purpose. Libraries are taken to use with the include directive. If a library is not part of the C standard library, its use must also be told to the compiler.
Logical operation refers to Boole's algebra, dealing with truth values. Typical logical operations are not, and, or which are often used in conditional statements. C also uses bitwise logical operations that work in the same way but affect each bit separately.
Machine language is made of instructions understood by the processor. Machine language is often called Assembly and it is the lowest level where it's reasonable for humans to give instructions to computers. Machine language is used at the latter part of this course - students taking the introduction part do not need to learn it.
Macro is an alias that defines a certain keyword to be replaced by a piece of code. When used well, macros can create more readable code. However, often the opposite is true. Using macros is not recommended in this course, you should just be able to recognize one when you see it.
In C the main function is the starting point when the program is run. The command line arguments of the program are passed on to the main function (although they do not have to be received), and its return value type is int. At its shortest a main function can defined as int main().
When programs are run, all their data is stored in the computer's memory. The memory consists of memory slots with an address and contents. All slots are of equal size - if an instance of data is larger, a continuous area of multiple memory slots is reserved.
Method is a function that belongs to an object, often used by the object to manipulate itself. When calling a method, the object is put before the method: values.sort().
Object is common terminology in Python. Everything in Python is treated as objects - this means that everything can be referenced by a variable (e.g. you can use a variable to refer to a function). Objects are typically used in object-oriented languages. C is not one.
Optimization means improving the performance of code, typically by reducing the time it takes to run the code or its memory usage. The most important thing to understand about opimization is that it should not be done unless it's needed. Optimization should only be considered once the code is running too slowly or doesn't fit into memory. Optimization should also not be done blindly. It's important to profile the code and only optimize the parts that are most wasteful.
A parameter is a variable defined alongside with a function. Parameters receive the values of the function's arguments when it's called. This differentation between parameters and arguments is not always used, sometimes both ends of the value transfer are called arguments.
Placeholders are used in string formatting to mark a place where a value from e.g. a variable will be placed. In Python we used curly braces to mark formatting placeholders. In C the % character is used which is followed by definitions, where the type of the value is mandatory. For instance "%c" can only receive a char type variable.
Pointers in C are special variables. A pointer contains a memory address of the memory location where the actual data value is located. In a sense they work like Python variables. A variable can be defined as a pointer by postfixing its type with * when it's being introduced, e.g. int* value_ptr; creates a pointer to an integer. The contents of the memory address can be fetched by prefixing the variable name with * (e.g. *value_ptr. On the other hand, the address of a memory adress can be fetched by prefixing a variable name with &, (e.g. &value.
The C precompiler is an apparatus that goes through all the precompiler directives in the code before the program is actually compiled. These directives include statements which add the source code of the included libraries into the program, and define directives that can define constant values (aliases) and macros.
Directives are instructions that are addressed at the precompiler. They are executed and removed from the code before the actual compilation. Directives start with the # character. The most common one is include which takes a library into use. Another common one is define, which is used e.g. to create constant values.
Prototype defines a function's signature - the type of its return value, its name and all the arguments. A prototype is separate from the actual function definition. It's just a promise that the function that matches the prototype will be found in the code file. Prototypes are introduced at the beginning of the file or in a separate header file. In common cases the prototype definition is the same as the line that actually starts the function introduction.
Interactive interpreter or Python console is a program where users can write Python code lines. It's called interactive because each code line is executed after its been fully written, and the interpreter shows the return value (if any).
The format method of string in Python is a powerful way to include variable values into printable text. The string can use placeholders to indicate where the format method's arguments are placed.
Python functions can have optional parameters that have a given default value. In Python the values of arguments in a function call are transferred to function parameters through reference, which means that the values are the same even though they may have different names. Python functions can have multiple return values.
In Python the import statement is used for bringing in modules/libraries - either built-in ones, thrid party modules or other parts of the same application. In Python the names from the imported module's namespace are accessible through the module name (e.g. math.sin). In C libraries are taken to use with include, and unlike Python import it brings the library's namespace into the program's global namespace.
Python lists were discovered to be extremely effective tools in Elementary Programming. A Python list is an ordered collection of values. Its size is dynamic (i.e. can be changed during execution) and it can include any values - even mixed types. Lists can also include other lists etc.
In Python main program is the part of code that is executed when the program is started. Usually the main program is at the end of the code file and most of the time under if __name__ == "__main__": if statement. In C there is no main program as such, code execution starts with the main function instead.
In Python a variable is a reference to a value, a connection between the variable's name in code and the actual data in memory. In Python variables have no type but their values do. The validity of a value is tested case by case when code is executed. In these ways they are different from C variables, and in truth Python variables are closer to C pointers.
Pythonin for-silmukka vastaa toiminnaltaan useimmissa kielissä olevaa foreach-silmukkaa. Se käy läpi sekvenssin -esim. listan - jäsen kerrallaan, ottaen kulloinkin käsittelyssä olevan jäsenen talteen silmukkamuuttujaan. Silmukka loppuu, kun iteroitava sekvenssi päättyy.
Pääfunktio on C:ssä ohjelman aloituspiste ja se korvaa Pythonista tutun pääohjelman. Oletuksena pääfunktion nimi on main ja se määritellään yksinkertaisimmillaan int main().
Resource referes to the processing power, memory, peripheral devices etc. that are availlable in the device. It includes all the limitations within which programs can be executed and therefore defines what is possible with program code. On a desktop PC resources are - for a programmer student - almost limitless, but on embedded devices resources are much more scarce.
Return value is what a function returns when its execution ends. In C functions can only have one return value, while in Python there can be multiple. When reading code, return value can be understood as something that replaces the function call after the function has been executed.
A statement is a generic name for a single executable set of instructions - usually one line of code.
C uses static typing This means that the type of variables is defined as they are created, and values of different types cannot be assigned to them. The validity of a value is determined by its type (usually done by the compiler). Python on the other hand uses dynamic typing aka.duck typing.
In Python all text is handled as strings and it has no type for single characters. However in C there are no strings at all - there's only character arrays. A character array can be defined like a string however, e.g. char animal[7] = "donkey"; where the number is the size of the array + 1. The +1 is neede because the string must have space for the null terminator '\0' which is automatically added to the end of the "string".
Syntax is the grammar of a programming language. If a text file does not follow the syntax of code, it cannot be executed as code, or in the case of C, it cannot be compiled.
Terminal, command line interface, command line prompt etc. are different names to the text-based interface of the operating system. In Windows you can start the command line prompt by typing md to the Run... window (Win+R). Command line is used to give text-based commands to the operating system.
The data in a computer's memory is just bits, but variables have type. Type defines how the bits in memory should be interpreted. It also defines how many bits are required to store a value of the type. Types are for instance int, float and char.
Typecast is an operation where a variable is transformed to another type. In the elementary course this was primarily done with int and float functions. In C typecast is marked a bit differently: floating = (float) integer}. It's also noteworthy that the result must be stored in a variable that is the proper type. it is not possible to change the type of an existing variable.
Unsigned integer is a an integer type where all values are interpreted as positive. Since sign bit is not needed, unsigned integers can represent twice as large numbers as signed integers of the same size. An integer can be introduced as unsigned by using the unsigend keyword, e.g. unsigned int counter;.
In the elementary programming course we used the term value to refer to all kinds of values handled by programs be it variables, statement results or anything. In short, a value is data in the computer's memory that can be referenced by variables. In C the relationship between a variable and its value is tighter as variables are strictly tied to the memory area where its value is stored.
A warning is a notification that while executing or - in this course particularly - compiling it, something suspicious was encountered. The program may still work, but parts of it may exhibit incorrect behavior. In general all warnings should be fixed to make the program stable.
One way to print stuff in C is the printf function, which closely resembles Python's print function. It is given a printable string along with values that will be formatted into the string if placeholders are used. Unlike Python, C's printf doesn't automatically add a newline at the end. Therefore adding \n at the end is usually needed.
Out of loops, while is based on repetition through checking a condition - the code block inside the loop is repeated until the loop's condition is false. The condition is defined similarly to conditional statements, e.g. while (sum < 21).