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
-brf
-i4
-ci4
-cli4
-l120
-nut
-npcs
-npsl
-bad
-bap
| option | effect |
|---|---|
-kr | K&R style for braces and control statements |
-brf | keep the opening brace of a function definition on the same line |
-i4 | basic indentation = 4 spaces |
-ci4 | continuation indentation = 4 spaces |
-cli4 | case label indentation = 4 spaces |
-l120 | maximum line length = 120 |
-nut | use spaces instead of tabs |
-npcs | do not put a space between a function name and ( |
-npsl | keep the selected style for function declaration formatting |
-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
help cd
Step 3 โ Directory navigation exercise
pwd
cd ~
ls
mkdir I2PL
ls
cd I2PL
pwd
mkdir lab1
ls
cd lab1
pwd
cd
ls
cd I2PL/lab1
ls
Step 4 โ Create a simple directory structure
Create the following directory structure:
I2PL
โโโ lab1
Verify it using:
ls
cd ..
ls
cd lab1
ls
tree command may not be available on the server. If it is installed, you can also use:
tree
Otherwise, a recursive listing such as:
ls -R
may also help you inspect the directory structure.
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
indent tool automatically reformats C code according to the selected style.
Compare the formatted file with the backup copy:
diff hello.c hello.c~
Task 7 โ Compile and run the program
Step 1 โ Compile
gcc -Wall 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.