Learning Outcomes
After this laboratory, the student should be able to:
- log in to the AGH UNIX server,
- configure the shell environment to use English,
- configure
nanoandindent, - use basic UNIX commands and manual pages,
- create, format, compile and run a simple C program,
- explain the stages of compilation: preprocessing, assembly, object code and linking,
- explain the difference between a header file and a library,
- recognize simple examples of undefined behaviour.
Task 1 — Log in to the AGH student server
ssh your_login@student.agh.edu.pl
Detailed instructions (if you have never connected before)
Before connecting to the server, make sure that your UNIX account is activated.
Go to the AGH network services panel:
https://panel.agh.edu.pl
After logging in:
- Open UNIX accounts
- Activate the account on student.agh.edu.pl
Then connect to the server using SSH:
ssh your_login@student.agh.edu.pl
If you connect for the first time, the system may ask you to confirm the host key.
yes
Task 2 — Configure the terminal environment to use English
Step 1 — Open the profile file
LANG=en nano ~/.profile
The prefix LANG=en forces nano to start in English.
Step 2 — Add locale settings
export LC_ALL=C.UTF-8
export LC_CTYPE=C.UTF-8
export LANG=C.UTF-8
export LC_COLLATE=C.UTF-8
export LANGUAGE=en
export LC_MESSAGES=C.UTF-8
Step 3 — Save the file and exit nano
Press:
Ctrl + X
Then press:
Y
and confirm the filename with:
Enter
Step 4 — Log out and reconnect
exit
Step 5 — Verify the configuration
locale
You should see values similar to:
LANG=C.UTF-8
LANGUAGE=en
LC_MESSAGES=C.UTF-8
LC_ALL=C.UTF-8
nano still opens in another language, you can always force English with:
LANG=en nano filename
Task 3 — Configure development tools
Step 1 — Configure nano
nano ~/.nanorc
Add:
set linenumbers
set autoindent
set tabsize 4
set tabstospaces
Step 2 — Configure indent
nano ~/.indent.pro
Add:
-kr
-i4
-l120
-nut
-bad
-bap
| option | effect |
|---|---|
-kr | K&R style (if (...) {) |
-i4 | indentation = 4 spaces |
-l120 | maximum line length = 120 |
-nut | use spaces, not tabs |
-bad | blank line after declarations |
-bap | blank line after function definitions |
Task 4 — Manual pages and basic UNIX commands
Step 1 — Learn how to use man
man man
| key | action |
|---|---|
Space | next page |
Enter | next line |
b | previous page |
/text | search text |
n | next search result |
q | quit |
Step 2 — Explore documentation of basic commands
man mkdir
man pwd
man ls
man cat
man tree
help cd
Step 3 — Directory navigation exercise
pwd
cd ~
ls
mkdir I2PL
ls
cd I2PL
pwd
mkdir lab1
ls
cd lab1
pwd
cd
tree
cd I2PL/lab1
ls
Step 4 — Create a simple directory structure
Create the following structure:
I2PL
└── lab1
└── test
Verify it using:
tree
Task 5 — Create the first C program
nano hello.c
#include <stdio.h>
int main(void) {
printf("Hello\n");
return 0;
}
Task 6 — Format the source code
indent hello.c
Compare the formatted file with the backup copy:
diff hello.c hello.c~
Task 7 — Compile and run the program
Step 1 — Compile
gcc hello.c -o hello
Step 2 — Run
./hello
Task 8 — Explore the compilation pipeline
Step 1 — Preprocessing
gcc -E hello.c > hello.i
wc -l hello.i
Step 2 — Assembly generation
gcc -S hello.c
Step 3 — Object file
gcc -c hello.c
This creates:
hello.o
Step 4 — Full compilation and linking
gcc hello.c -o hello
| command | stage | output |
|---|---|---|
gcc -E | preprocessing | .i |
gcc -S | assembly generation | .s |
gcc -c | object file generation | .o |
gcc | full compilation + linking | executable file |
Task 9 — Header file vs library
Step 1 — Locate the header file
whereis stdio.h
Step 2 — Check linked libraries
ldd hello
Step 3 — Find printf in the C library
nm -D /lib/x86_64-linux-gnu/libc.so.6 | grep " printf@"
| element | role |
|---|---|
stdio.h | header file with declarations |
libc.so.6 | library with implementations |
printf | function declared in the header and implemented in the library |
Task 10 — Variables and formatted output
#include <stdio.h>
int main(void) {
int x = 1;
int y;
printf("x=%d, y=%d\n", x, y);
printf("x=%5d, y=%5d\n", x, y);
float f1 = 12.0f;
float f2 = 12.3678f;
printf("f1=%f, f2=%f\n", f1, f2);
printf("f1=%8.2f, f2=%8.2f\n", f1, f2);
return 0;
}
Compile and run the program several times. Observe that y may have a different value each time.
Task 11 — Undefined behaviour
#include <stdio.h>
int main(void) {
printf("%d\n", 3.14);
return 0;
}
Step 1 — Compile normally
gcc test2.c -o test2
Step 2 — Run several times
./test2
Step 3 — Compile with warnings
gcc -Wall test2.c -o test2
%d expects int, but 3.14 is a double. This is an example of undefined behaviour.
Task 12 — Running programs and PATH
Step 1 — Try running without ./
test2
You should see:
command not found
Step 2 — Run correctly
./test2
Step 3 — Check PATH
echo $PATH
PATH.
That is why programs in the current directory are run with ./program.
What will be checked during the next laboratory
The student should be able to:
- log in to the server,
- configure the shell environment and development tools,
- use manual pages and basic UNIX commands,
- create, format, compile and run a simple C program,
- explain the meaning of
gcc -E,gcc -S,gcc -candgcc, - explain the difference between a header file and a library,
- recognize simple examples of undefined behaviour.