Laboratory 2

Laboratory 2 – Variables, Input, Output and Basic Control Flow

Formatted output, basic input with scanf, ASCII codes, conditional statements, and a simple calculator using switch.

printf scanf ASCII if-else switch

Laboratory Overview

What is new in this laboratory.

In this laboratory, your programs stop being static and start reacting to user input.

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 printf and scanf correctly,
  • format output using width specifiers,
  • read characters and print ASCII codes,
  • use if-else and switch,
  • understand basic type promotion in arithmetic expressions,
  • compile and run all tasks using the workflow introduced in Laboratory 1.

Task 0 — Reminder and preparation

Before starting the exercises, log in to the AGH server and prepare the working directory.

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
This laboratory continues the workflow introduced in Laboratory 1: work on student.agh.edu.pl, use nano, compile with -Wall, and run programs with ./program.

Task 1 — Formatted text output

Create a program that prints formatted text using escape sequences.

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
Use formatting characters instead of manually aligning everything with spaces.

Task 2 — Right-aligned numbers and sum

Read four integers from the user and print them right-aligned together with their 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
All numbers should be aligned to the right.
Use field width in 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
The width used in printf should be consistent for all numbers to ensure proper alignment.
Assume that each number contains at most 4 digits.

Task 3 — Character and ASCII code

Read a character from the user and print its ASCII code.

Step 1 — Create the file

nano ex3.c

Step 2 — Read a character

Read a single character from the user using scanf.

When reading a character, remember to skip whitespace:
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
In C, a 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

Compare two numbers and print the greater one.

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.
Use the following structure:
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
All three cases must be handled: greater, smaller, and equal.
This is your first structured use of conditional statements in C.

Task 5 — Simple calculator using switch

Read a simple arithmetic expression and calculate the result.

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
At this stage, assume that the input is given exactly in this form, without spaces.

Read the expression using:

scanf("%d%c%d", &a, &op, &b);

Step 3 — Use switch to select the operation

Supported operators:

+
-
*
/
Use a 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
Division by zero must be handled explicitly.
The program should also handle an unsupported operator.
Use only scanf, printf, and a switch statement.

Task 6 — Temperature conversion (Celsius → Fahrenheit)

Convert an integer Celsius value to a floating-point Fahrenheit value.

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
The Fahrenheit value must be displayed with two decimal places.
printf("Fahrenheit: %.2f\n", f);

Step 5 — Compile and run

gcc -Wall ex6.c -o ex6
./ex6
Be careful: using 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: 1
  • 9.0 / 5 → one operand is floating-point → result: 1.8
  • 9 / 5.0 → one operand is floating-point → result: 1.8
If at least one operand is a floating-point number, the result is also floating-point.

Explicit casting

  • (float) 9 / 5 → result: 1.8
  • 9 / (float) 5 → result: 1.8
You should understand why all expressions above give different results.
Integer division is one of the most common sources of bugs in C programs.

What will be checked during the next laboratory

  • working on student.agh.edu.pl in the correct course directory,
  • compiling and running programs with warnings enabled,
  • using scanf and printf correctly,
  • reading integers and characters from the user,
  • formatting output using width and basic format specifiers,
  • using if-else and switch in simple programs,
  • understanding the difference between integer and floating-point division,
  • writing readable and properly indented code.