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

Memory Hierarchy

Learning Objectives: Organizing computer system memory hierarchically and implementing virtual memory.
In the first lecture, we introduced the general concept of a computer's memory hierarchy. At the top of this hierarchy are the processor's internal memory areas, such as registers, caches (and the stack). The closer the memory is to the processor, the faster it is, but the less of it there is, and the more expensive it is (per bit). The goal is to store data as close to the processor as possible to maximize performance.
"Memory Hierarchy"
In modern processors, the delays at different levels of the hierarchy are distributed approximately as follows:
Memory Type Location Latency (~clock cycles)
1. CPU Registers CPU 0
2. TLB Registers CPU 0
3. L1 Cache: memory blocks (64-bit) CPU 4
4. L2 Cache: memory blocks (64-bit) CPU 10
5. L3 Cache: memory blocks (64-bit) CPU/External 50
6. Virtual Memory RAM 200
7. I/O Buffers (device caches) RAM 200
8. Hard Drive Disk Controller 100,000
It is noteworthy that even within caches, there are order-of-magnitude differences in latency. This is because, in modern processors, the L1 and L2 caches are typically integrated into the processor, while the L3 cache is external to the processor. This is also reflected in the size of the caches, ranging from (tens of) kilobytes to a few megabytes (L3). Typically, embedded systems use caches smaller than 1KB to keep microcontroller costs low.

Principle of Locality

In this course, memory addressing has so far been either direct (direct access) or indirect (indirect access), where the program specifies a memory address directly, or the processor calculates an address to access main memory.
Now, when examining memory access patterns, it has been observed that the placement of data in memory adheres to the principle of locality (temporal and spatial locality). This means that, at any given time, a program only needs a small part of the memory allocated to it, and memory accesses tend to target these areas. This locality can be observed in two dimensions:
  1. Temporal locality: the same memory addresses are accessed repeatedly (regularly).
  2. Spatial locality: memory accesses often target addresses that are close to one another.
-> It is therefore beneficial to keep only the most frequently accessed parts of memory in the cache.
When a program makes a memory access, the memory hierarchy is checked to see if the desired data is already in the cache. Once the data is found, an entire block of data is moved through the hierarchy closer to the processor. Since different levels of the hierarchy have varying amounts of memory, the block size decreases, and its content becomes more specific in line with the locality principles. Moving a block closer to the processor implements both temporal locality (keeping the most recent data close) and spatial locality (accessing nearby addresses).
Example: Code and data for loop structures would ideally be entirely in a nearby cache!

Hit Rate

To organize memory hierarchically, computational parameters can be used. A common metric is the hit rate (denoted as h), which is the ratio of memory accesses that hit the cache to the total number of memory accesses.
For example, a hit rate h=95% means that 95% of memory accesses find the desired data in the cache, while 5% must fetch it from lower levels of the hierarchy. A higher hit rate means fewer slow memory accesses and faster program execution.
From the hit rate h, the average memory access time (AMAT) can be calculated:
Here, Tcycle is the clock cycle time, and Tmiss is the (maximum) fetch time from memory:
For example, if the clock cycle is 1ns and the memory read time is 20ns with a hit rate of 95%:
AMAT = 1ns + (1 - 0.95) * 20ns = 2ns

Memory Addressing

Previously, we mentioned that the address bus from the processor is used to read a memory address, and the corresponding word is fetched from memory. This is called linear memory addressing, where the address is used as is.
In modern computers, memory addressing often uses paging (engl. paging). The idea is that paging allows addressing a larger memory space than what the address width of the memory bus permits. Memory must therefore be divided into fixed-size pages (e.g., matching or smaller than the address bus width). Unused or additional address bits indicate which page contains the data. These bits are provided using registers and specific address memory locations and are represented in a page table (engl. page table). More on page tables shortly.
Example: In x86-based computers, memory is segmented into blocks with specific purposes. A set of registers (CS-GS, as shown in the introductory material) is reserved for memory addresses that indicate where each block begins. For instance, the CS segment (engl. code segment) holds program code, and the DS segment (engl. data segment) holds program data.
A paged memory address consists of three parts:
  1. Page directory (location in memory)
  2. Page table number/address
  3. Word address within the page.
"Memory Paging"
Here, the memory address works as follows: first, the directory provides page table addresses; then the page table is fetched, which gives the page address; finally, a specific memory location on the page is retrieved. All these blocks can be scattered across physical memory.
In the figure, a 32-bit (linear) address is divided such that the page table's directory address uses 10 bits, the page location in the table uses another 10 bits, and the word location on the page uses 12 bits. The total memory size would then be 2^32 = 4GB, with 2^20 = 1,048,576 pages, and each page would be 2^12 = 4096B in size.
(The processor's control logic could already infer from the components of the memory address whether the data is in the cache, as caches might, for instance, hold entire tables/blocks.)

Cache Organization

When handling memory, we aim to optimize the principles of locality, which is reflected in how the memory hierarchy is organized. Since the cache is always smaller in size than the level below it in the hierarchy, cache memory locations must be shared among accesses.
The figure illustrates the parts of a memory address from the perspective of cache organization (as presented in the course textbook). The rule is that caches are organized into sets, each containing a specified number of blocks, which in turn consist of a specified number of words.
"Memory Address"
  1. Tag: The block address/id, when the set contains 2^t blocks. A block is the smallest memory region fetched from main memory.
  2. Set: A group of memory blocks, when there are 2^s sets.
  3. Block Offset (index): The position of a word within a block, where a block contains 2^b words.
Example: In the 32-bit memory address illustrated above, the Set + Tag could be 10 + 10 bits, and the Block Offset 12 bits.
This structure of a memory address proves useful when using caches. The following explains methods of allocating cache memory locations among accesses.

Direct Mapping

The simplest addressing method is direct mapping, where the memory has 2^s sets, and each set has only one cache block/memory location. In this case, the Tag bits identify which block is currently in the cache location.
The cache location's Valid bit indicates whether the cache block is in use. (Otherwise, default values of 0 for the memory address and data in the location could be misinterpreted as values fetched into the cache.)
Example: With two Tag bits, we can address four blocks. When updating the cache, the Tag bits change according to the set being addressed. The cache location remains the same throughout.
Tag Set Block
--- --- ---
00  000 110 
01  000 101
10  000 000
11  000 001
The advantage of direct mapping is that the set does not need to be searched to locate the block (see below); it can be directly calculated from the address. The downside, however, is that memory locations are shared among blocks, potentially increasing memory fetches and leaving some cache locations underutilized.

Associative Cache

In an associative cache, a set has multiple cache locations for blocks, and a block can be placed in any of these locations.
Such a cache is generally an E-associative cache (engl. E-way set-associative cache). The cache's name is based on the number of blocks per set E. In the figure above, there are two blocks per set, so E=2, making this a "2-associative cache."
Now, to retrieve data from the cache, a search must be conducted across all cache locations in the set to locate the block. In real implementations, there could be up to 1024 blocks.

Fully Associative Cache

Direct mapping can be thought of as a 1-associative cache because there is only one possible cache location per set.
The other extreme is the fully associative cache, where a single set contains all cache locations. In such a cache, there are no Set bits in the memory address—only Tag bits. During memory access, every cache location must be searched to find the desired block.
One reason why this method exists today is that searches in such memory can now be programmatically optimized using search algorithms. More on this shortly under virtual memory.

Cache Updates

If the desired data is not found in the cache, a cache miss occurs. The data must then be fetched from the next level of cache or, eventually, from main memory.
Fetching data updates the cache, which can be done in various ways:
* Empty block: If there are free blocks in the set, they are filled first with new data.
* Random: A block is selected for overwriting randomly.
* Least Frequently Used (LFU): The block with the fewest accesses is overwritten. This requires bookkeeping.
* Least Recently Used (LRU): The block that has not been accessed for the longest time is overwritten. This also requires bookkeeping.
Before fetching new data into the cache, the existing data in the location (if any) may need to be written back to main memory because the program may have updated it. To manage this, cache locations have several bits that indicate the status of the location. The Valid bit above is one example, but there could also be bits indicating changes to the content or the freshness of the data.
To write updated data from the cache to lower levels of the hierarchy, two methods are used:

Implementations

In modern PCs, the two upper levels of the cache hierarchy, L1 and L2, are typically integrated into the processor, while the L3 cache is built using separate chips. The L3 cache is significantly larger than the higher levels.
The figure shows the general cache structure of Intel's Itanium processors.
"Actual Cache Structure"
In Intel's Xeon processor architectures, the L2 cache has its own parallel high-speed memory bus alongside the system bus. Since the L1 cache is integrated into the processor, the L2 cache is also made faster.

Virtual Memory

In modern computers, programs do not actually see where their memory is physically located, and the memory area can be larger than the part of main memory allocated to the program. This kind of memory space is called virtual memory. Virtual memory consists of physical main memory and external mass storage, such as a hard drive, organized using paging and caches so that programs perceive the memory space as contiguous.
Virtual memory uses virtual memory addresses, which are first translated into physical memory addresses before the memory is accessed. The operating system's services manage virtual memory addresses and perform memory operations, or processors may have dedicated MMU circuits (engl. Memory Management Unit) as part of bus control to handle memory operations and manage memory space.
The advantages include:
  1. Virtual memory allows programs to access a larger memory area than what is physically available in main memory.
    • Naturally, virtual memory adheres to the principles of locality but on a much larger scale.
  2. Main memory can be shared among running programs based on their resource requirements.
    • This is helpful because programs generally need only a small portion of their allocated memory space at a given time.

Memory Addressing

Virtual memory addressing is generally a slow operation because data must be fetched via caches and, in the worst case, from mass storage. Memory management can be done collaboratively by hardware and firmware, as the default assumption is that memory fetches are slow, leaving enough time for complex optimization operations!
Virtual memory is divided into pages (engl. page), much like main memory. The organization of virtual memory is fully associative, meaning that a page can be placed anywhere in main memory. Translating a virtual memory address to a physical one is done using an address translation mechanism, where the upper bits of the address point to the target physical memory.
Example: Assume a 32-bit virtual memory address where the block size is 12 bits, meaning the block size is 2^12B = 4KB. The remaining 20 bits of the address indicate the memory page (i.e., set + tag). Thus, the total virtual memory size would be 2^20 * 4KB = 4GB. The physical part of the virtual memory would consist of, for example, 2^18 * 4KB = 1GB blocks in memory chips or mass storage.

Fast Memory Addressing

The speed of inherently slow virtual memory accesses improves when the addressed memory is already present in the caches or main memory. According to the principle of locality, the virtual-to-physical address translations used frequently should be stored in advance in a cache called a page table.
However, even when using page tables, the problem persists that each memory access requires three lookups: one for the page directory, one for the page table, and one for the physical memory.
Virtual memory address translation can be accelerated by storing previously translated addresses in a register or a cache called the TLB (engl. Translation Lookaside Buffer). When the translated address is found in the TLB, the other cache lookups are unnecessary.
Since the TLB is a cache, it must have a Valid bit to indicate the state of the data in the memory location. Additionally, a D-bit (engl. dirty bit) indicates whether the corresponding virtual memory page has been updated by the program, in which case it should be written back to physical memory before loading a new page.
The translation process for virtual-to-physical memory using the TLB is as follows:
  1. Check whether the translation of the virtual memory page is found in the TLB register.
  2. If the translation is found, retrieve the data from the specified physical memory address.
  3. If the translation is not in the TLB but is in the page table, update the TLB during the lookup.
  4. If the page is not in the page table either, the page must be fetched via a page fault exception from mass storage.
Addresses are updated based on the LRU principle, where the TLB register and page tables use status bits (engl. reference bit) for bookkeeping to indicate whether and when a page was last accessed.
For write-back operations in virtual memory, the write-back method is used due to the slowness of mass storage, meaning the data is kept in a buffer and written to physical memory in bulk.

Memory Technologies

Several different memory technologies are currently used to implement the various levels of the memory hierarchy.

Implementations

Traditionally, the microchips of computers are placed side by side on a circuit board, making the board literally two-dimensional. A consequence of this is that the connections between microchips are often long, which, in modern microchip technologies, can already act as a limiting factor.
In memory chips, the new HBM technology (engl. High-Bandwidth Memory) stacks memory chips vertically so that connections run "vertically" through the memory chips. This arrangement saves circuit board space and achieves a wider memory bus, allowing for lower clock speeds.

Additional bibliography

Please refer to the course book Bryant & O'Hallaron, Computer Systems: A Programmer's Perspective, 3rd edition. Chapters 6 and 9.

Conclusion

"We are ... forced to recognize the possibility of constructing a hierarchy of memories, each of which has greater capacity than the preceding but which is less quickly accessible."
- Burks, Goldstine & von Neumann (1946)
While the idea of caches was introduced with the first modern computers in the 1940s, memory management is a highly complex operation in modern processors, involving various solutions distributed across the operating system, firmware, and hardware.
?
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).