Laboratory Overview
What is new in this laboratory.
You will learn how to read values from the user, format output more precisely,
and make simple decisions using if-else and switch.
These are the first steps toward writing interactive programs in C.
Learning Outcomes
- log in to the AGH UNIX server and work in the correct directory,
- use
printfandscanfcorrectly, - format output using width specifiers,
- read characters and print ASCII codes,
- use
if-elseandswitch, - understand basic type promotion in arithmetic expressions,
- compile and run all tasks using the workflow introduced in Laboratory 1.
Task 0 — Reminder and preparation
Step 1 — Log in to the AGH UNIX server
ssh your_login@student.agh.edu.pl
Step 2 — Go to the course directory
cd ~/I2PL
Step 3 — Create the directory for Laboratory 2
mkdir lab2
cd lab2
Step 4 — General rules
- Work only in
~/I2PL/lab2. - Do not use global variables.
- Keep the code readable and properly indented.
- Compile with:
gcc -Wall program.c -o program
- Run with:
./program
student.agh.edu.pl, use nano, compile with -Wall,
and run programs with ./program.
Task 1 — Formatted text output
Step 1 — Create the file
nano ex1.c
Step 2 — Print the required text
Write a program that prints a decorative text block containing:
- a line of repeated symbols,
- the word Hello!,
- another line of repeated symbols,
- horizontal tabulation and empty lines where needed.
A possible target layout is:
%%%%%%%%%%%%
Hello!
%%%%%%%%%%%%
Use escape sequences such as:
\n
\t
\"
\\
Step 3 — Compile and run
gcc -Wall ex1.c -o ex1
./ex1
Task 2 — Right-aligned numbers and sum
Step 1 — Create the file
nano ex2.c
Step 2 — Read four integers
The program should read four integers from the user using scanf.
Step 3 — Print numbers right-aligned
Print all numbers aligned to the right in a column, and then print their sum.
The output should have the following structure:
123
1000
1
17
----
1141
printf, for example:
printf("%4d\n", x);
or
printf("%5d\n", x);
Step 4 — Print separator and sum
Print a separator line (for example ----) and the computed sum.
Step 5 — Compile and run
gcc -Wall ex2.c -o ex2
./ex2
printf should be consistent for all numbers to ensure proper alignment.
Task 3 — Character and ASCII code
Step 1 — Create the file
nano ex3.c
Step 2 — Read a character
Read a single character from the user using scanf.
scanf(" %c", &c);
The leading space tells scanf to ignore whitespace characters such as \n.
Step 3 — Print the ASCII code
Print both:
- the character,
- its ASCII code (as an integer).
Example:
Enter a character: A
Character: A
ASCII code: 65
Step 4 — Compile and run
gcc -Wall ex3.c -o ex3
./ex3
char is internally represented as a number.
This task demonstrates the relationship between characters and their numeric ASCII values.
Task 4 — Greater of two numbers
Step 1 — Create the file
nano ex4.c
Step 2 — Read two integers
The program should read two integers from the user using scanf.
Step 3 — Compare the values
Use conditional statements to determine:
- which number is greater,
- or whether both numbers are equal.
if (a > b) {
...
} else if (a < b) {
...
} else {
...
}
Step 4 — Print the result
Example interaction:
Enter two numbers: 5 8
Greater number: 8
Enter two numbers: 7 7
Numbers are equal: 7
Step 5 — Compile and run
gcc -Wall ex4.c -o ex4
./ex4
Task 5 — Simple calculator using switch
Step 1 — Create the file
nano ex5.c
Step 2 — Read an expression
The program should read an expression containing:
- an integer,
- an operator,
- another integer.
Example input:
12+5
Read the expression using:
scanf("%d%c%d", &a, &op, &b);
Step 3 — Use switch to select the operation
Supported operators:
+
-
*
/
switch statement with one case for each operator.
Step 4 — Print the result
Example interaction:
Enter the expression: 12+5
Result: 17
Step 5 — Handle invalid cases
- If the operator is not supported, print an error message.
- If division by zero is requested, print an error message instead of performing the calculation.
Step 6 — Compile and run
gcc -Wall ex5.c -o ex5
./ex5
scanf, printf, and a switch statement.
Task 6 — Temperature conversion (Celsius → Fahrenheit)
Step 1 — Create the file
nano ex6.c
Step 2 — Read the temperature in Celsius
Read the temperature in Celsius as an integer value.
Step 3 — Convert to Fahrenheit
Use the formula:
F = C * 9.0 / 5.0 + 32
Step 4 — Print both values
The output should have the following form:
Celsius: 25
Fahrenheit: 77.00
printf("Fahrenheit: %.2f\n", f);
Step 5 — Compile and run
gcc -Wall ex6.c -o ex6
./ex6
9 / 5 will result in integer division
(value 1 instead of 1.8).
Use 9.0 / 5.0 or explicit casting.You should understand why
9.0 / 5 and 9 / 5.0
produce a different result than 9 / 5.
Type Promotion in C (very short)
In C, the result of an expression depends on the types of operands.
9 / 5→ integer division → result: 19.0 / 5→ one operand is floating-point → result: 1.89 / 5.0→ one operand is floating-point → result: 1.8
Explicit casting
(float) 9 / 5→ result: 1.89 / (float) 5→ result: 1.8
What will be checked during the next laboratory
- working on
student.agh.edu.plin the correct course directory, - compiling and running programs with warnings enabled,
- using
scanfandprintfcorrectly, - reading integers and characters from the user,
- formatting output using width and basic format specifiers,
- using
if-elseandswitchin simple programs, - understanding the difference between integer and floating-point division,
- writing readable and properly indented code.