Week 3 · Foundation

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

Write your first C program and understand each part of it
See how code becomes a program through compilation
Learn the int, char, float, double types and their size in memory
Print formatted output with printf
Build logic with if/else and loops
Write a function and reuse your code

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.

In Scratch it was hard to make mistakes, because blocks only connected if they fit. In C, even forgetting a small semicolon produces an error. This is not a bad thing; it teaches order and discipline, and it is exactly this skill that makes you a strong programmer.

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:

Program anatomy click a part
salom.c
Click one of the buttons above, and you will see what each part does.

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.

A semicolon (;) 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:

Compilation pipeline run it
terminal
$
Click the "Run" button: you will see how code becomes a program.

So the path is always the same: write codecompile (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.

turlar.c
1int yosh = 19; // whole number
2float bo'y = 1.78; // decimal number
3char daraja = 'A'; // single character
4double pi = 3.14159; // precise decimal number

Each type takes up a different amount of space in memory. Click a card to see more detail:

Data types click a card
Click one of the four types above.

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:

  • %d for a whole number (int)
  • %f for a decimal number (float, double)
  • %c for a single character (char)
  • \n to move to a new line

Below, choose a type and enter a value, and see how printf outputs it:

printf field change the value
main.c
1printf("Result: %d\n", 42);
terminal
To get data from the user, 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:

Operator calculator change the numbers
terminal

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 tracer change the age
age =
tekshir.c
terminal
If there are many options, an 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:

Code tracer (debugger) step through it
yigindi.c
terminal

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?

main.c
1for (int i = 0; i < 3; i++)
2 printf("*");
* * * (with spaces)
***
0 1 2

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).

kvadrat.c
1int kvadrat(int n) { // n is the parameter
2 return n * n; // returns the result
3}

Now if we call kvadrat(4), it returns 16. Below, choose a number and watch the function call:

Function call choose a number
kvadrat( )
Click the "Call" button.

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.

massiv.c
1int ballar[3] = {90, 85, 70};
2printf("%d\n", ballar[0]); // 90

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.

satr.c
1char ism[] = "Ali";
2printf("Salom, %s!\n", ism); // Salom, Ali!

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.

Arrays, strings and pointers are all about working with memory. That is why C is called "the language closest to the computer": it lets you manage memory directly.

Glossary of terms

inta type that stores a whole number (for example 5, -20, 100).
printfa function that prints text and values to the screen.
mainthe function that runs first when the program starts.
gcca compiler that translates C code into machine code.
fora loop that repeats a set number of times.
returnreturns a result from a function.

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