C language basics
The ideas we learned in Scratch with blocks, we now move into real text-based code. C is a fast and precise language that sits at the foundation of almost every modern language. In this lesson we go step by step from the first program to variables, conditions, loops and functions, watching each one in a live IDE-style code tracer.
What you will learn in this lesson
3.1 What is C?
C is a programming language created in 1972, and it is still one of the most used and most influential languages today. Operating systems (Windows, the Linux kernel), browsers and even other programming languages are often built on top of C.
In Scratch we connected blocks. In C, however, we write code, that is, text written according to special rules. At first this feels harder, but in return the language runs very fast and works very close to the computer.
Why do we start with C in particular?
- It is simple and precise: you write only what is needed yourself, nothing is hidden
- It teaches you to deeply understand how memory and the computer work
- Its ideas (types, loops, functions) also exist in Python, JavaScript, Java and others
Most importantly: once you understand C, other languages become much easier for you. That is why it is a true foundation.
3.2 First program
Every programmer's first program usually prints "Hello" on the screen. Here is a complete program in C, just six lines:
So every C program begins with the main function: when the computer starts the program, it begins reading from exactly here. And printf prints text to the screen.
;) must appear at the end of every command. It tells C "this command is finished", much like a period at the end of a sentence.3.3 Compilation
The computer does not understand the C code we write directly. It only knows machine code, made up of 0s and 1s. So we translate our code into machine code using a compiler (for C usually gcc). Run this process step by step below:
So the path is always the same: write code → compile (gcc) → run the program. If there is an error in the code, the compiler does not create the program, but instead tells you which line the error is on.
3.4 Variables and types
A program needs to store data somewhere. For this we use a variable, that is, a named box in memory. It is exactly the same "variable" as in Scratch, except here every variable also has a type.
A type means what kind of data a variable stores: a whole number, a decimal number or a single character. We tell C the type in advance, so it knows how much memory to allocate.
Each type takes up a different amount of space in memory. Click a card to see more detail:
3.5 printf and input
printf prints text to the screen. But not only plain text; it can also print the values of variables. For this, special symbols called format specifiers are used:
%dfor a whole number (int)%ffor a decimal number (float, double)%cfor a single character (char)\nto move to a new line
Below, choose a type and enter a value, and see how printf outputs it:
scanf is used, for example scanf("%d", &yosh);. It writes the number entered from the keyboard into the yosh variable.3.6 Operators
An operator is a symbol that performs an action on values. In C there are three main groups:
- Arithmetic:
+addition,-subtraction,*multiplication,/division,%remainder - Comparison:
==equal,!=not equal,>greater,<less - Logical:
&&and,||or,!not
An important subtle point: in C, when whole numbers are divided, the result is also whole. For example, 7 / 2 gives 3 (the fractional part is dropped), while 7 % 2 gives 1 (the remainder). Try it below:
3.7 Conditions: if / else
The "if ..." block from Scratch is written as if in C. The program checks a condition: if the condition is true, it goes one way; otherwise, through else, it goes another way.
if ... else if ... else chain or a switch is used. Note: to check equality you put not one but two equals signs (==). A single = is assignment, two == is comparison.3.8 Loops: for / while
To repeat one action many times, we use a loop, just like the "repeat" block in Scratch. The most commonly used one in C is the for loop. It consists of three parts: an initial value, a continuation condition and a change at each step.
The program below adds up the numbers from 1 to 5. Step through it and watch how the variables and the result change, like a debugger:
Result: 1, 3, 6, 10, 15. The while loop works the same way, but the condition is given up front and the body repeats as long as the condition is true.
Make a prediction
What does this loop print to the terminal?
3.9 Functions
In Scratch we made "your own block". In C this is called a function: we give a name to a piece of code and call it anywhere we want. A function can take in data (a parameter) and return a result (return).
Now if we call kvadrat(4), it returns 16. Below, choose a number and watch the function call:
3.10 Going deeper advanced
You have mastered the basics. Now let us briefly get to know three more important concepts in C. We will study these in depth in the coming weeks; for now we just get a general idea.
Arrays
An array means storing several values of the same type under one name. Each element is accessed by its number (index), and the index starts from 0.
Strings
A string means a sequence of characters. In C, a string is actually an array of char. A word, a name or a sentence is stored this way.
Pointers
A pointer means a variable that stores the address of another variable in memory. That is, it stores not the value itself but the information about "where it is located". This is the most powerful and at the same time the most delicate concept in C, and we will study it fully in week 6.
Glossary of terms
3.11 Knowledge quiz
16 questions. To complete the week, answer at least 11 of them correctly.
Congratulations! Week 3 is complete
Now you can write real C code: you have learned about variables, types, printf, operators, conditions, loops and functions. These are the basic building blocks of any large program.
Next week: Arrays and strings (storing many values, the index, strings and the null terminator).
Go to the next module