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
    6. määrittely
  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

The basic blocks of C programming

As was promised before, the course proceeds in about the same order as the Elementary Programming course. At least as much as it is possible with C. This time we will refine two basic pieces of programming:
variables
and
functions
. Without them it is rather difficult to do any programming, especially in C, which does not even have a main program.

A frame in the shape of C

Learning objectives: After this portion you will know how to write a C program that can be compiled and executed.

Compulsory symbols

Unlike in Python, where a code file may contain just a single line of code that does something, C requires certain basic things. At the end of the previous piece of material there was an example of a very simple C program:
#include <stdio.h>

int main() {
    printf("aasisvengaa!\n");
    return 0;
}
The actual code to be executed still is only one line, but around it has appeared this and that. We got a preparatory look at these in the previous material. The #-symbol at the beginning does not, by the way, make a line a
comment
(// does!). All the lines that begin with # are instead lines meant for the
preprocessor
to handle. In short, a preprocessor is a part of the
compiler
that processes the code file according to the
directives
, the lines marked with the #-symbol at their beginning. At this point we will only handle the directive called include, which resembles Python's
import command
. Unlike Python, C does not provide a built-in print function. The printf function belongs to the stdio
library
(Standard Input/Output). To use printf, the program includes the stdio.h header file, which provides the compiler with the information it needs to recognize and correctly use the function.
The name ends with the .h, which is short for header. This .h-file is a library's
header file
, which includes the introductions of the functions, constants and possible variables of the library. The actual code of the library is in the stdio.c file. We usually will not be seeing it, since compilers typically deliver the standard libraries as compiled binary files. There will be more about header files later on the course. While perusing the code you might notice one difference to the import of Python: when printf function is called, it is not written stdio.printf, just printf. In C include works actually like Python's from moduuli import * and separate namespaces are not used. This means you need to be more careful with naming your functions than usually.
The library used here, stdio, includes functions that are related to printing and reading input. At this point it will be used in practically every program. Other at this point or soon relevant libraries are at least math and stdlib. When you need these they will be introduced to you so that you know when and how to use them. C does not have a similar friendly documentation base as Python, so we recommend you either use a C book or the ”UTFG” method (just google everything).
Another notable difference is that you can not make a C program without
functions
. The main program like in Python does not exist. Instead, C programs have a
main function
named main, where the execution of the program begins. In this course, the return type of main is int. This return value, provide information on how the function ended. This value can be useful, for example, in scripts that execute several programs in sequence and use the return value to determine, among other things, whether the program was executed successfully. We will go deeper within the inner workings of a C function in this material, but not yet. At this point it is enough that you get what this mysterious int main() {-line is. It defines the main function, which does not have
parameters
. At the end of the line the curved bracket is the beginning of a statement block. All the things between this and the closing curly bracket are inside the function.

Variables' metamorphosis

Learning objectives: After this portion you will have mastered the basics of the variables in C. This includes understanding and defining the types of variables, and some sort of understanding in which way variables reside within the memory of a computer. We will also take a look at how variables can be printed and how their types can be changed.

Refreshing memories

Just a reminder about what was said about the
variables of Python
in Elementary Programming before we delve into the inner workings of
C's variables
. The basic idea was to connect the name appearing in the program to the value inside the computer's
memory
. This familiar animation might work as a fine refresher:
So, that was Python, about which was nagged how a
variable
is a reference to a
value
and does not actually contain a value. This is demonstrated in the animation at y = x, where both of the variables point at the same value. It was possible to show this even in the Python interpreter using id function:
>>> a = 5
>>> id(a)
10894208
>>> b = a
>>> id(b)
10894208
Here can be seen how a and b are not identical only by their value, they are the same
object
. This does not have much to do with the basic types of variables, like numbers and strings, because these
data types
are
immutable
in Python. With
lists
you could instead see actual repercussions:
>>> a = [1, 2, 3]
>>> b = a
>>> b.append(4)
>>> a
[1, 2, 3, 4]
This behaviour had at least one typical application: the functions were given lists that the function edited but did not return. They were not returned, because that was not necessary - the edited list was the same list the function had been given. As an example of this, we can use the add function from the collection program example:
def add(collection):
    print("Enter the details of the album to add. Leave the album name empty to stop.")
    while True:
        album = input("Album name: ")
        if not album:
            break
            
        collection.append({
            "artist": input("Artist name: "), 
            "album": album, 
            "n": ask_number("Number of tracks: "),
            "duration": ask_time("Duration: "),
            "year": ask_number("Release year: ")
        })

Comparing memories

In C
variables
are tied to their values in the sence that you could say the variable contains a value. The variable is a name the programmer has set for the area of the
memory
the value is located in. When a new variable is created, a new area of memory is reserved for it. Let us return back to this after a moment, but let us watch an animation about it before we continue.
Another way C's variables clearly differ from Python's is that they have a separately defined
type
. The type is specified in the variable's
declaration
, which has the form variable_type variable_name.
Unlike in Python, C variables must be declared before they can be used. In the simple cases used on this course (we will see almost at the end of Module 1 some exceptions), declaring a variable also
defines
it, meaning that storage is reserved for the variable. A value can also be given when the variable is defined. This is called
initialization
.
For example:
int i;
int x = 5;
Both lines declare and define a variable. The second line also initializes x with the value 5.
What happens if you do not initialize a variable? Usually you get very unpredictable results, which in the worst case can also pose a security risk.

Types everywhere

In C the types of the variables play a bigger part than in Python. C has more basic types. Above you can see an
integer
type int. Other integer types of C are short and long. These types also have modifiers signed or unsigned. signed is the default for the integer types, meaning that the variable can represent both negative and positive values. An unsigned integer cannot represent negative values, but it can represent a larger range of non-negative values using the same number of bits. For characters there is the variable type char, which is actually an integer type, and that will be explained later. Unlike in Python, in C there is no
string
of characters variable type. We will later return to how to handle strings.
Also
floating point numbers
exist in C, and they work in a similar manner as they do in Python. Not surprisingly, also they are just bits in the memory, they are just interpreted in a different manner. In C there are two float types, float and double. It is not relevant to this course to understand how floating point numbers act within memory, but it is good to know that many values cannot be represented exactly and are therefore stored as approximations. We will return to variables and floating point number systems later.
Now that we know something about variable types, we can take on some exercises so that we will remember something about them later.
These exercises have no effect on course grading.

Defining variables - A presentation about variables

Let's begin from the simplest ones. Your job is to introduce three variables:
  • unsigned small integer N
  • floating point number keskipituus
  • character luokitus
Write the introductions in the box below.
Warning: You have not logged in. You cannot answer.

Defining variables - At the beginning there was...

Let's continue the exercises. Now your job is to introduce and initialize variables. This is the most recommended way of introduce variables, because it ensures that all the variables have some sort of sensible value. The required variables are:
  • unsigned big integer "counter" with initial value 0
  • integer "sum" with initial value 0
Write the definig lines in the box below.
Warning: You have not logged in. You cannot answer.

Types in print out

Among all the other fun things, the
types
of
variables
are also related to printing. When using printf, the format string tells the function how each argument should be interpreted and displayed. This means that a value of the same type can be displayed in different ways. For example, the integer 12 can be printed as 12, +12, 012, or 0xc. At this point you only need to know that printf receives both the values to print and a description of how they should be printed. There's an example of this below.
printf("%d\n", x);
In short (we will get back to this later), in the printf call above the value of the variable x is printed on screen as an integer and right after it a newline character. The first parameter of the function is a string, which says how to print, in this case "%d\n". Now %+letter characters are the same as the
placeholders
in Python's
format
method and after that the newline character \n. Actually Python does also have similar syntax - format is only a bit more modern and versatile way of doing the same thing. In the place of every placeholder in the
printf
function there will be one of the
arguments
after the string in the given order. In the example in the place of %d will be the value of the variable x in the print out. Unlike in Python, where you could use an empty place holder like "You gave a number {}".format(luku), in C each placeholder is tied to one or more types. Here is a list of the most commonly used ones on this course:
placeholder variable type
%d int
%i int
%hi short
%li long
%u unsigned int
%hu unsigned short
%lu unsigned long
%f float, double
%c char
%s char[]
In addition to these, there are other placeholders that display values using a different representation. For example:
placeholder meaning
%x prints the value of an unsigned integer in hexadecimal format
These exercises have no effect on course grading.

Class C of the Basic Printing School

Printing is not actually the most important thing on this course, but it is good to be able to handle the basics of it also. In this exercise you mainly practice choosing the right
placeholder
based on the type of the variable. The
compiler
usually returns
warning messages
about incorrect placeholders, but it still compiles the program through since it can be executed - the results just may be unexpected.
Your task is to pick the piece of code below and add print outs so that each defined variable's contents are printed on their own line. Unlike Python's printf function, C's printf function does not automatically print line break at the end, so you have to add it there yourself - you do not need to add anything else to the print outs.
HUOM: Check that the last row includes also a new line character
tkj-tulostusperuskoulu-c
int main() {
    unsigned long n = 4000;
    unsigned short laskuri = 0;
    char luokka = 'C';
    float keskiarvo = 0.0;
}

Return the code file below. The code has to compile without warnings.
Warning: You have not logged in. You cannot answer.

Functions' metamorphosis

Learning objectives: In this portion we will handle how to define functions in C and what differences they have with Python functions (typed parameters and return values). Prototype is introduced as a new concept.

Danger: Functions devour types!

On the base level defining and using
C function
do not differ from defining and using
Python function
. Function is defined on its own definition line, on which is presented the function's name and
parameters
, and the code of the function follows. The syntax is somewhat different: on the definition line there is no separate keyword like Python's def. Instead of it the definition of the function looks similar to a variable's definition - the brackets after the name tell us that it is a function instead of a variable:
int main() {
    return 0;
}
Instead of the def keyword, the function definition starts with a
type
. This type defines the type of the function's
return value
. The main function's retun value type is int, because it should return a status code of whether the function's execution went well or not. At the end of the function should usually be return 0;, which says that everything went well. In an error situation the return value is some other number, but we do not need to know much about them yet - right now the most important thing is to remember that there should be that return line at the end of the main function. In the same way the parameters should be defined to have a type:
float  calculate_distance(float speed, float time) {
    return speed * time;
}
If the arguments in the function call are of the wrong type, the
compiler
will return an error message (well, this is not completely accurate, because some types can be implicitly converted to other types, but let's not go into that yet). A function also has to return a value that is of the type defined for its return value. Otherwise you get another compilation error (again, this is not completely accurate, because some types can be implicitly converted to other types). A function that is not meant to return anything can be defined to have the return type void:
void print_instructions() {
    printf("This program does not do anything useful.");
}
The most notable functional difference between C functions and Python functions is that C functions can have only one return value. If you want to return more than one value, you need to use references (we will call them
pointers
) or the variables can be packed in a
data structure
. We will get back to this later.

Prototypes

In C also functions have to be introduced before using them.
Prototype
is function's introduction and tells the
compiler
that the program should include a function with this name that takes these types of variables as parameters and returns that type of value.
Prototypes sort of form a table of contents of the functions that appear in the program. If the C program consists of several different code modules, each of the shared functions has to be introduced in their header files so that the table of contents would include all of them. The header file provides the information that other code modules need in order to use these functions. The contents of the header file are included with the #include directive, which is handled by the preprocessor. You will learn more about this later.
Naturally, you can write the whole function before it is used. In that case, the function definition also provides its declaration. However, if the function is used from another source file, its declaration should normally be placed in a header file that the other source file includes.
The defining line for the prototype is identical to the actual function's defining line:
int calculate_sum(int a, int b);

int calculate_sum(int a, int b) {
    return a + b;
}
Usually adding prototypes to the code does not require anything else besides copying the definitions of the functions to the beginning of the code (below the
preprocessor's
instructions). Prototypes can also be located in a separate
header file
, which will be explained more thoroughly later. Using prototypes on this course is not optional - the evaluator's compiler has been instructed to produce an
error message
if prototypes are missing.
Note that the main function does not require a prototype, but it has to be named main.
These exercises have no effect on course grading.

Conical function

Let's write a little function as a warm up exercise. May the function be called cone_volume. As input it takes two floating point parameters, the cone's bottom radius and the maximum height of the cone. The function calculates the volume of the cone and returns it as a floating point number. You can use the primary school method for calculating the exponentiation (r*r). Use the double data type for presenting the floating point numbers!
Hint. Include math-library (math.h) into your source code to get the value of pi as the constant M_PI.
Write a code file where only this function is defined, without the main function. Remember the prototype!
Warning: You have not logged in. You cannot answer.

Basics of the C compiler

Learning objectives: In this section you will learn the basics of using a C compiler, at least enough for this C programming part of the course. This mainly means becoming familiar with a few of the most important compiler flags and learning how to read the error and warning messages produced by the compiler.
During the course we will use a virtual machine on the workstations where the compiler environment is already installed, but on your own computer you will need to install it yourself. You can find a link to the installation instructions on the course front page.

Basics of compiler magic

Earlier we introduced the following basic spell:
C:\path\to\somewhere>gcc -Wall -o myprogram.exe mycode.c
This command has altogether 4 parts, as was briefly explained in the first material. gcc is the name of the program being used., in this case the
compiler
(GNU Compiler Collection). The code file or file(s) to be compiled are found at the end. They are not preceded by an
option flag
beginning with a hyphen. Of these flags, -o defines the name of the file where the compiled code is stored. In Windows the name is given the .exe extension, while in Linux executable files normally have no extension. With the last flag, -Wall, we have actually cheated a little: it is not strictly needed to compile the program. It increases the number of
warnings
produced by the compiler by including several questionable and easily fixable constructs among the things it warns about. We have included the flag in every basic example because its use is highly recommended. Actually almost mandatory on this course, because the evaluator's compiler uses it and rejects code that produces warnings.
The order of these parts does not matter, as long as -o and the program name following it stay together.

A world of alternatives

In addition to the C compiler included in the GNU Compiler Collection, there are other compilers available. One very popular alternative to gcc is clang, which is part of the LLVM project. Like gcc, it is open source and is installed on the virtual machine intended for solving the exercises. clang accepts largely the same compiler flags as gcc, so you can use it simply by replacing gcc with clang in the commands. In fact, trying clang in addition to gcc is recommended, because in some situations clang may present errors and warnings in a clearer form. Programs that compile with more than one compiler are also a good sign of code quality: a well-written program should compile with different compilers and be more likely to follow the C standard.
Intel and Microsoft, among others, also provide their own C compilers. Intel's compiler in particular has been known for producing highly optimized binaries.

More flags

As was already mentioned in the first material, a
compiler
has about as many
option flags
as there are cats on the internet. In this section we will briefly go through a few that are used on this course.
A particularly important one for this course is -Werror, which can be used by itself or together with an = sign. By itself, it turns all
warnings
into
compiler errors
, meaning that the code will not be compiled into an executable file until the warnings have been fixed. This is also a recommended flag to use. The flag can also be configured so that only a certain warning or set of warnings is treated as an error. A particularly important use on this course is -Werror=missing-prototypes, which is used by the evaluator. This flag causes a program that is missing function
prototypes
to fail compilation. It is therefore a good idea to use it on your own computer as well, so that you can reliably eliminate these errors before submitting your code to the evaluator.
Another warning-related flag is -Wextra, which enables even more warnings than -Wall. The naming is slightly confusing, since "all" does not actually include absolutely everything...
Linux users should also know one more basic flag at this point: -lm. When using functions from the mathematics library (math.h) on Linux, the -lm flag is usually needed. It tells the build process to use the mathematics library. Without this flag, the source code may compile, but the executable file cannot be built because the code that implements the mathematical functions is not available when the executable is created. In this case it is also important that the flag appears in the command after the files being compiled:
/path/to/somewhere$ gcc -Wall -Werror=missing-prototypes -o calculation_program calculation_program.c -lm

Mysterious compiler messages

Being able to read messages from the
compiler
is central to fixing faulty programs. If you have interpreted Python error messages before, there is nothing particularly new here. Largely the same information can be found in these messages as well. Below is code containing a few errors.
These exercises have no effect on course grading.
tkj-rikki-c
#include <stdio.h>
#include <math.h>

const float PI = 3.1416;

float laske_sektorin_ala(float sade, float kulma) {
    return  PI * pow(sade, 2) * kulma / 360;
}

int main() {
    printf("%f\n", laske_sektorin_ala(5));
    return 0;
}

If this code is compiled with the command shown above, the result is:
broken.c:6:7: error: no previous prototype for ‘calculate_sector_area’ [-Werror=missing-prototypes]
 float calculate_sector_area(float radius, float angle) {
       ^
broken.c: In function ‘main’:
broken.c:11:20: error: too few arguments to function ‘calculate_sector_area’
     printf("%f\n", calculate_sector_area(5));
                    ^
broken.c:6:7: note: declared here
 float calculate_sector_area(float radius, float angle) {
       ^
cc1: some warnings being treated as errors
When compiling C, the compiler normally reports several errors in the code at once, whereas with Python the execution of the program stops at the first error and the rest are revealed only after it has been fixed. This behaviour can be changed with the -Wfatal-errors flag. This is mainly useful when compiling the code is a long process. Let us go through this example output one message at a time:
broken.c:4:7: error: no previous prototype for ‘calculate_kinetic_energy’ [-Werror=missing-prototypes]
 float calculate_kinetic_energy(float speed, float mass) {
       ^
This error is caused by the previously mentioned -Werror=missing-prototypes
flag
, which the
compiler
actually also tells us at the end of the first line inside square brackets. Otherwise, the first line consists of the location of the message in the form file name:line number:column number (the column number indicates the position of the character where the error was detected). After the location comes first the type of the message (error), followed by a textual explanation. The second line shows the faulty line, and below it a helpful arrow points to the location where the compiler detected the error. The next message contains one additional line:
broken.c: In function ‘main’:
broken.c:9:20: error: too few arguments to function ‘calculate_kinetic_energy’
     printf("%f\n", calculate_kinetic_energy(5));
                    ^
The first line is new – it tells us in which function the error occurred. The previous message did not originate inside any function, but in the definition section. Otherwise this message is similar to the previous one. This time the error is caused by accidentally giving the function only one argument when it requires two. In fact, the following three lines are also related to this error.
broken.c:4:7: note: declared here
 float calculate_kinetic_energy(float speed, float mass) {
       ^
This message is of type note. It tells us where in the code the function related to the previous error was defined. In other words, it is only additional information. The final line is also an additional note:
cc1: some warnings being treated as errors
This is simply a reminder that at least some warnings were treated as errors.

Final summary

In this material we have covered two basic concepts of C programming, variables and functions, and shown how they differ from Python. A characteristic feature of C is that the types of variables have much more significance. In general, C is somewhat more rigid to write because there are more details that need to be taken into account. On the other hand, this allows us to get closer to the hardware level of the computer. From the point of view of problem solving, however, programming is still largely similar to Python – we still write functions and work with variables. Only the details have changed.
?
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.
A variable definition creates a variable and reserves storage for it in memory.
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).