From source code to a running program:
.c
extension.o
extensionYou can use:
However, the best practice is to use an IDE (Integrated Development Environment)
IDE combines an editor, a compiler and a debugger in a single application
The editors in IDEs often recognize and highlight the syntax as you type
Additionally, advanced editors perform static analysis to provide hints about problems in the source code
Run in the terminal:
sudo snap install code --classic
Install the compiler:
sudo apt update && sudo apt install gcc
Check if everything works fine by running:
gcc --version
You should see something like that:
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run in the terminal (cmd.exe
):
winget install Microsoft.VisualStudioCode
Download and run the latest MSYS2 installer from https://github.com/msys2/msys2-installer/releases
(e.g. msys2-x86_64-20220128.exe
)
Run the MSYS2 terminal and execute:
pacman -Syu
The terminal will be closed, so open a new MSYS2 terminal and execute:
pacman -Su
Next, install the compilers:
pacman -S --needed base-devel mingw-w64-x86_64-toolchain
Finally, configure the system to recognize compiler binaries:
Search for Edit the system enviroment variables
Click Environment variables button
Double-click Path environment variable
Click New and paste
C:\msys64\mingw64\bin
(this is a valid path if you install MSYS2 in the default
location)
Check if everything works fine by running a new terminal
(cmd.exe
) and executing:
gcc --version
You should see something like that:
gcc (Rev9, Built by MSYS2 project) 11.2.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Click File → Open Folder in the top menu
(this is important; you need to have a folder opened, not just a single
file, to build programs)
Click New File and save it as main.c
in the
selected folder
Paste the following code into the editor:
#include <stdio.h>
int main() {
("Hello World!\n");
printfreturn 0;
}
Press CTRL+SHIFT+B
(a shortcut to build the program)
and select C/C++
from the drop-down menu
Press CTRL+SHIFT+`
to open a new terminal
Run your code with ./main
(Linux) or
.\main.exe
(Windows). You should see:
Hello World!
Let’s analyze the code line by line:
#include <stdio.h>
tells the compiler that we
want to use input/output functions from the standard library.int main() {
defines a function named main
which returns an integer and takes no arguments.printf("Hello World!\n");
prints in the terminal a
string Hello World! and a new line character (the
\n
) – note that the line has to end with a semicolon
;
.return 0;
ends the program with code zero meaning
success (if you want to let know that something went wrong, you should
return any non-zero value).}
a closing bracket of the main
function.Write the following programs.
Ask for a name and print: Hello name
To read a string of maximum 10 characters:
char name[11];
("%s", name); scanf
In general, every string’s last byte is 0, so n characters require n+1 bytes to store
The scanf
function reads the input and converts
it
The %s
means that it should treat the input as a
string
Ask for an integer and print its absolute value
To read an integer:
int value;
("%d", &value); scanf
The %d
means that the input contains a decimal
number
The &
in front of value
is required
by scanf
to know where to store the result
To perform a conditional execution:
if (condition) {
// do something if condition is true
} else {
// do something else
}
Ask for an integer n and print out n times a string: Hello World!
To execute operations in a loop:
for (int i = 0; i < n; i++) {
// repeat something n times
}
Ask for an integer n and print an ASCII half-tree with asterisks
For example, if n=4:
*
**
***
****
Ask for two numbers - hours and minutes - and print the total number of seconds they contain
Ask for a number of meters and print the number of miles
Use real numbers instead of integers:
float real;
("%f", &real); scanf
The number of miles in a kilometer is 0.621371192
Ask for hours, minutes and kilometers. Let’s say these describe the time you needed to run the given distance. Print the average speed [km/h]
Ask for a radius of a sphere and print its volume
Ask for a number of book copies and print the total cost of buying and delivering them. The first ten books cost 40 zl each. Then the price is 35 zl per book. If customer orders 40 or more books, there is a 5% discount from the total book price. Delivery boxes contain up to twelve books. The cost of delivery per box is 12 zl. Customer pays for delivery of up to five boxes. The bookstore will pay for delivery of any additional box required to fulfill the delivery.
Ask for three numbers: a, b and c. Check if these three numbers can be interpreted as the lengths of sides of a triangle
Ask for a string and check if it is a palindrome (reads the same from the beginning and from the end)
Ask for two numbers: a and b. Check if a is a power of b