types and variables in C practice gedit Fedora gcc programs

Types and variables in C practice — 3 real programs in gedit and Fedora

In the previous article we covered types and variables in C theory. Now it’s time to write real programs.

In this article we build three programs from scratch using gedit and compile them with gcc in Fedora, the exact workflow you’ll use in every IC2 lab. Each program uses all the main types and shows the differences from Python in a real context.

Open your Fedora terminal before starting.

Setting up your workspace

cd ~/GCID/IC2/Labs
mkdir Lab_types_variables
cd Lab_types_variables

Create a separate file for each program as we go. Always open gedit with & to keep the terminal free.

Types and variables in C practice — Program 1: Basic calculator

This program asks for two numbers and shows the result of all arithmetic operations. The goal is to see how integer division, type casting and format specifiers work in practice.

Create the file:

touch calculator.c
gedit calculator.c &

Type this in gedit:

#include <stdio.h>

int main() {
    int a, b;
    double result;

    printf("=== BASIC CALCULATOR ===\n\n");

    printf("Enter first integer: ");
    scanf("%d", &a);

    printf("Enter second integer: ");
    scanf("%d", &b);

    printf("\n--- Results ---\n");

    /* Addition */
    printf("%d + %d = %d\n", a, b, a + b);

    /* Subtraction */
    printf("%d - %d = %d\n", a, b, a - b);

    /* Multiplication */
    printf("%d * %d = %d\n", a, b, a * b);

    /* Integer division */
    if (b != 0) {
        printf("%d / %d = %d  (integer division)\n", a, b, a / b);
        printf("%d %% %d = %d  (remainder)\n", a, b, a % b);

        /* Real division with cast */
        result = (double)a / b;
        printf("%d / %d = %.4f  (real division)\n", a, b, result);
    } else {
        printf("Cannot divide by zero\n");
    }

    /* Power — no built-in operator in C */
    result = 1;
    int i;
    for (i = 0; i < b; i++) {
        result *= a;
    }
    printf("%d ^ %d = %.0f\n", a, b, result);

    return 0;
}

Save with Ctrl + S, go to the terminal and compile:

gcc calculator.c -o calculator -Wall
./calculator

Output with 10 and 3:

=== BASIC CALCULATOR ===

Enter first integer: 10
Enter second integer: 3

--- Results ---
10 + 3 = 13
10 - 3 = 7
10 * 3 = 30
10 / 3 = 3  (integer division)
10 % 3 = 1  (remainder)
10 / 3 = 3.3333  (real division)
10 ^ 3 = 1000

Three things to notice here. First, %% in printf produces a literal % — a single % would be interpreted as the start of a format specifier and cause a warning or error. Second, (double)a / b casts a to double before the division — without the cast, a / b would do integer division before the cast, giving 3.0 instead of 3.3333. Third, C has no power operator — we simulate a ^ b with a loop.

Types and variables in C practice — Program 2: Grade calculator

This program introduces double for grades, formatted output and the relationship between types. Create the file:

touch grades.c
gedit grades.c &
#include <stdio.h>

int main() {
    char name[50];
    int age;
    double grade1, grade2, grade3, grade4;
    double average, weighted_average;
    int passed;

    printf("=== GRADE CALCULATOR ===\n\n");

    /* Read student info */
    printf("Student name: ");
    scanf("%s", name);

    printf("Age: ");
    scanf("%d", &age);

    /* Read grades */
    printf("\nEnter 4 subject grades (0-10):\n");
    printf("  Grade 1: ");
    scanf("%lf", &grade1);
    printf("  Grade 2: ");
    scanf("%lf", &grade2);
    printf("  Grade 3: ");
    scanf("%lf", &grade3);
    printf("  Grade 4: ");
    scanf("%lf", &grade4);

    /* Calculate averages */
    average = (grade1 + grade2 + grade3 + grade4) / 4.0;

    /* Weighted average — grades 3 and 4 count double */
    weighted_average = (grade1 + grade2 + grade3 * 2 + grade4 * 2) / 6.0;

    /* Pass/fail — in C no bool, use int (0 = false, 1 = true) */
    passed = average >= 5.0;

    /* Output */
    printf("\n--- Results for %s (age %d) ---\n", name, age);
    printf("Grade 1:           %.2f\n", grade1);
    printf("Grade 2:           %.2f\n", grade2);
    printf("Grade 3:           %.2f\n", grade3);
    printf("Grade 4:           %.2f\n", grade4);
    printf("Simple average:    %.2f\n", average);
    printf("Weighted average:  %.2f\n", weighted_average);
    printf("Passed?:           %s\n", passed ? "Yes" : "No");

    /* Classification */
    printf("Classification:    ");
    if (average >= 9.0)
        printf("Outstanding\n");
    else if (average >= 7.0)
        printf("Merit\n");
    else if (average >= 5.0)
        printf("Passed\n");
    else
        printf("Failed\n");

    return 0;
}
gcc grades.c -o grades -Wall
./grades

Output:

=== GRADE CALCULATOR ===

Student name: Sergio
Age: 20

Enter 4 subject grades (0-10):
  Grade 1: 7.5
  Grade 2: 8.0
  Grade 3: 6.5
  Grade 4: 9.0

--- Results for Sergio (age 20) ---
Grade 1:           7.50
Grade 2:           8.00
Grade 3:           6.50
Grade 4:           9.00
Simple average:    7.75
Weighted average:  7.92
Passed?:           Yes
Classification:    Merit

Three important C details in this program. %lf in scanf for doubles, not %f. 4.0 instead of 4 in the average calculation, dividing by integer 4 would do integer division and give 7 instead of 7.75. The ternary operator passed ? "Yes" : "No", C has this too, identical syntax to Java.

Types and variables in C practice — Program 3: Student profile with char

This program brings in char variables and shows how characters work in C — including their ASCII codes. Create the file:

touch profile.c
gedit profile.c &
#include <stdio.h>

int main() {
    /* String — array of char */
    char first_name[30];
    char last_name[30];

    /* Single characters */
    char grade_letter;
    char gender;

    /* Numbers */
    int age;
    int student_id;
    double gpa;

    /* Constants */
    const int CURRENT_YEAR = 2025;
    const double PASS_GPA = 5.0;

    printf("=== STUDENT PROFILE ===\n\n");

    /* Read data */
    printf("First name: ");
    scanf("%s", first_name);

    printf("Last name: ");
    scanf("%s", last_name);

    printf("Age: ");
    scanf("%d", &age);

    printf("Student ID: ");
    scanf("%d", &student_id);

    printf("GPA (0-10): ");
    scanf("%lf", &gpa);

    printf("Gender (M/F): ");
    scanf(" %c", &gender);    /* space before %c skips whitespace */

    /* Determine grade letter */
    if (gpa >= 9.0)       grade_letter = 'A';
    else if (gpa >= 7.0)  grade_letter = 'B';
    else if (gpa >= 5.0)  grade_letter = 'C';
    else                  grade_letter = 'F';

    /* Calculations */
    int birth_year = CURRENT_YEAR - age;
    int years_to_graduate = 4 - (age - 18);  /* assuming started at 18 */
    double percentage = gpa * 10.0;
    int passed = gpa >= PASS_GPA;

    /* Display profile */
    printf("\n");
    printf("================================\n");
    printf("      STUDENT PROFILE\n");
    printf("================================\n");
    printf("Name:        %s %s\n", first_name, last_name);
    printf("Gender:      %c (ASCII: %d)\n", gender, gender);
    printf("Age:         %d (born %d)\n", age, birth_year);
    printf("Student ID:  %07d\n", student_id);    /* zero-padded to 7 digits */
    printf("GPA:         %.2f / 10.00\n", gpa);
    printf("Percentage:  %.1f%%\n", percentage);
    printf("Grade:       %c\n", grade_letter);
    printf("Status:      %s\n", passed ? "PASSED" : "FAILED");
    printf("--------------------------------\n");

    /* First initial of first name */
    printf("Initial:     %c (ASCII code: %d)\n", first_name[0], first_name[0]);

    /* Show uppercase/lowercase conversion for initial */
    char initial_lower = first_name[0] + 32;  /* uppercase to lowercase */
    printf("Lowercase:   %c\n", initial_lower);

    printf("================================\n");

    return 0;
}
gcc profile.c -o profile -Wall
./profile

Output:

=== STUDENT PROFILE ===

First name: Sergio
Last name: Medina
Age: 20
Student ID: 1234
GPA (0-10): 7.75
Gender (M/F): M

================================
      STUDENT PROFILE
================================
Name:        Sergio Medina
Gender:      M (ASCII: 77)
Age:         20 (born 2005)
Student ID:  0001234
GPA:         7.75 / 10.00
Percentage:  77.5%
Grade:       B
Status:      PASSED
--------------------------------
Initial:     S (ASCII code: 83)
Lowercase:   s
================================

Three things worth understanding in this program. %07d pads the student ID with zeros to 7 digits: 0 means zero-fill, 7 is the minimum width. first_name[0] accesses the first character of the string, strings in C are arrays of chars, and [0] is the first element, just like Python list indexing. first_name[0] + 32 converts uppercase to lowercase by adding 32 to the ASCII code — ‘S’ is 83, ‘s’ is 115, difference is exactly 32. This works because uppercase and lowercase letters are always 32 apart in ASCII.

The most common mistakes in these programs

After writing and compiling these three programs you’ve probably already encountered some of these:

/* 1. Forgetting & in scanf for non-string types */
scanf("%d", age);      /* wrong — crash */
scanf("%d", &age);     /* correct */

/* 2. Using %f instead of %lf for double in scanf */
double d;
scanf("%f", &d);       /* wrong — reads wrong value */
scanf("%lf", &d);      /* correct */

/* 3. Integer division when expecting decimal */
double avg = (7 + 8 + 6) / 3;    /* → 7.0, not 7.0 — wait, this gives 7! */
double avg = (7 + 8 + 6) / 3.0;  /* → 7.0 — correct: 3.0 forces double division */

/* 4. Forgetting space before %c in scanf */
scanf("%c", &c);      /* may read leftover newline from previous scanf */
scanf(" %c", &c);     /* correct — space skips whitespace including \n */

/* 5. Using %% to print a literal % */
printf("50%\n");      /* warning: incomplete format specifier */
printf("50%%\n");     /* correct → prints "50%" */

Visualise with Python Tutor

pythontutor.com supports C, select C from the language dropdown. Paste this minimal version to see types in action:

#include <stdio.h>
int main() {
    int a = 10;
    int b = 3;
    double result_int = a / b;
    double result_double = (double)a / b;
    printf("%f vs %f\n", result_int, result_double);
    return 0;
}

Step through it and watch how a / b gives 3 (integer division) even though you’re storing it in a double, the division happens first, giving 3, then 3 gets converted to 3.0. The cast (double)a / b converts a to 10.0 before the division, so the division itself is 10.0 / 3 = 3.333....

Summary and next step

In this article you practised types and variables in C with three real programs. You used int and double for numbers, char for characters and strings, scanf with & and the correct format specifiers, formatted output with printf and the integer division behaviour that catches everyone the first time.

In the next article you’ll find exercises to solve on your own in Fedora.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *