Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Last updated: Friday 13 November 2026 @ 17:29:40

Learning C

Programming for Engineers

Learning C

In this practical workbook you will develop, compile and run C programmes alongside the C lecture. Each activity builds on the previous one: from your first printf() to formatted input, data types, conditional statements and independent exercises.

CGCCVS CodeData typesConditions
Practical checklist
Chapter checklist 0 / 13 completed

1. Configure the C compiler

Your shell must be able to locate gcc through the PATH environment variable.

  1. Open Git Bash.

    Git Bash terminal
  2. Open your Bash configuration file:

    nano ~/.bashrc
  3. On the university Windows machines, add the following line to the end of ~/.bashrc:

    export PATH="$PATH:/c/Program Files/W64DevKit-V2.9.1/w64devkit/bin"

    In nano, press Ctrl+O, then Enter to save; press Ctrl+X to exit.

  4. Reload the configuration and check the compiler:

    source ~/.bashrc
    gcc --version

    Use the w64devkit path and commands shown above. This practical uses .exe output filenames.

    Use a system-installed GCC compiler. Confirm it is accessible with:

    gcc --version

    On Linux you would normally compile to an executable without the .exe suffix, such as gcc helloworld.c -o helloworld.

    A suitable C compiler must be installed. Check the available compiler using cc --version. You may use cc instead of gcc for the compilation tasks; the compiler may be Clang rather than GNU GCC.

    GCC and accompanying tools

2. Create your first C project

  1. Open Visual Studio Code and create a folder called Learning_C.

  2. Inside that folder, create a file called helloworld.c.

    Creating the C source file in VS Code

  3. Enter the following programme and save it:

    #include <stdio.h>
    
    int main(void) {
        printf("Hello World\n");
        printf("Goodbye World\n");
        return 0;
    }
  4. Open the terminal in the project directory and compile the programme:

    gcc helloworld.c -o helloworld.exe
  5. Run the new executable:

    ./helloworld.exe
    Reading the first programme
    • #include <stdio.h> makes declarations for standard input/output functions, including printf(), available.
    • main() is the entry point for this programme.
    • printf() writes formatted text to standard output; \n starts a new line.
    • return 0; reports successful termination to the host environment.
    • gcc helloworld.c -o helloworld.exe compiles and links the source into an executable.
    Expected output
    Hello World
    Goodbye World

    Original reference screenshot:

    First C programme output

3. Print an integer

We’ll now create inputoutput.c and progressively extend it through the next sections.

  1. Create inputoutput.c in the Learning_C folder.

    code inputoutput.c
  2. Add this programme:

    #include <stdio.h>
    
    int main(void) {
        int testInteger = 5;
        printf("Number = %d\n", testInteger);
        return 0;
    }
  3. Compile and run it:

    gcc inputoutput.c -o inputoutput.exe
    ./inputoutput.exe
    Expected output
    Number = 5

    Integer output reference

4. Print floating-point values

  1. Replace the contents of inputoutput.c with the following programme:

    #include <stdio.h>
    
    int main(void) {
        int testInteger = 5;
        float number1 = 13.5f;
        double number2 = 12.4;
    
        printf("Number = %d\n", testInteger);
        printf("number1 = %f\n", number1);
        printf("number2 = %f\n", number2);
        return 0;
    }
  2. Save, recompile and run:

    gcc inputoutput.c -o inputoutput.exe
    ./inputoutput.exe
    Expected output
    Number = 5
    number1 = 13.500000
    number2 = 12.400000

    Floating-point output reference

5. Print characters

  1. Extend inputoutput.c to include a character:

    #include <stdio.h>
    
    int main(void) {
        int testInteger = 5;
        float number1 = 13.5f;
        double number2 = 12.4;
        char chr = 'a';
    
        printf("Number = %d\n", testInteger);
        printf("number1 = %f\n", number1);
        printf("number2 = %f\n", number2);
        printf("character = %c, value is = %d\n", chr, chr);
        return 0;
    }
  2. Save, compile and run the programme again.

    Expected output
    Number = 5
    number1 = 13.500000
    number2 = 12.400000
    character = a, value is = 97

    Character output reference

6. Read user input

The scanf() function reads formatted input from standard input, usually your terminal keyboard.

  1. Update inputoutput.c:

    #include <stdio.h>
    
    int main(void) {
        int testInteger = 5;
        float number1 = 13.5f;
        double number2 = 12.4;
        char chr = 'a';
    
        printf("Number = %d\n", testInteger);
        printf("number1 = %f\n", number1);
        printf("number2 = %f\n", number2);
        printf("character = %c, value is = %d\n", chr, chr);
    
        printf("Enter an integer: ");
        if (scanf("%d", &testInteger) != 1) {
            printf("Invalid integer input.\n");
            return 1;
        }
        printf("Number = %d\n", testInteger);
        return 0;
    }
  2. Recompile and execute the programme, then enter an integer when prompted.

    gcc inputoutput.c -o inputoutput.exe
    ./inputoutput.exe
    Example interaction
    Enter an integer: 42
    Number = 42

    The earlier lines printing the fixed variables will appear before this prompt.

    User input reference

7. Format specifiers

The following formats are useful for these exercises. The table distinguishes formatted output (printf) from formatted input (scanf), where the required pointer type matters.

C typeprintf outputscanf input
int%d%d
unsigned int%u%u
short%hd%hd
long%ld%ld
long long%lld%lld
unsigned long%lu%lu
unsigned long long%llu%llu
float%f%f
double%f%lf
long double%Lf%Lf
char (character)%c%c
signed char / unsigned char (numeric)%hhd / %hhu%hhd / %hhu
size_t (for sizeof)%zu%zu

8. Examine data-type sizes

The sizes of C data types depend on the compiler and platform. Investigate the sizes on your system rather than assuming they are identical everywhere.

  1. Create a new file named dataTypeSize.c.

    code dataTypeSize.c
  2. Add the following programme:

    #include <stdio.h>
    
    int main(void) {
        printf("Data type\t\tSize (bytes)\n");
        printf("char\t\t\t%zu\n", sizeof(char));
        printf("int\t\t\t%zu\n", sizeof(int));
        printf("double\t\t\t%zu\n", sizeof(double));
        printf("float\t\t\t%zu\n", sizeof(float));
        printf("unsigned char\t\t%zu\n", sizeof(unsigned char));
        printf("long\t\t\t%zu\n", sizeof(long));
        printf("unsigned long\t\t%zu\n", sizeof(unsigned long));
        printf("long double\t\t%zu\n", sizeof(long double));
        return 0;
    }
  3. Compile, run and record the sizes you observe:

    gcc dataTypeSize.c -o dataTypeSize.exe
    ./dataTypeSize.exe

    Data-type sizes reference

9. Conditional statements

A conditional statement allows a programme to choose what to do based on a Boolean condition. We’ll move from a single if to if/else and then to an else if chain.

9.1 A basic if

  1. Create IfStatement.c and enter:

    #include <stdio.h>
    
    int main(void) {
        int num = 10;
    
        if (num > 5) {
            printf("The number is greater than 5.\n");
        }
        return 0;
    }
  2. Save, compile and execute it:

    gcc IfStatement.c -o IfStatement.exe
    ./IfStatement.exe
  3. Change num to 3, rebuild and observe the difference.

9.2 Add else

  1. Replace the contents of IfStatement.c with:

    #include <stdio.h>
    
    int main(void) {
        int num = 3;
    
        if (num > 5) {
            printf("The number is greater than 5.\n");
        } else {
            printf("The number is not greater than 5.\n");
        }
        return 0;
    }
  2. Recompile and test with num = 3 and num = 10.

9.3 Add else if

  1. Replace the programme with:

    #include <stdio.h>
    
    int main(void) {
        int num = 0;
    
        if (num > 0) {
            printf("The number is positive.\n");
        } else if (num < 0) {
            printf("The number is negative.\n");
        } else {
            printf("The number is zero.\n");
        }
        return 0;
    }
  2. Compile and test three cases: negative, zero and positive integers.

    What you should now be able to do

    Explain the differences between if, if/else and if/else if, and predict which branch executes for a given integer.

10. Independent exercises

Apply what you’ve learnt without immediately copying the solution. Attempt each task first, then expand the reference implementation to check your approach.

Exercise 1 : Even or odd

  1. Create evenOdd.c and implement the requested programme.

  2. Compile and test with positive, negative and zero values.

    Hint

    An integer is even when its remainder after division by 2 is zero: num % 2 == 0.

    ./evenOdd
    Enter an integer: 67
    67 is odd.
    #include <stdio.h>
    
    int main(void) {
        int num;
    
        printf("Enter an integer: ");
        if (scanf("%d", &num) != 1) {
            return 1;
        }
    
        if (num % 2 == 0) {
            printf("%d is even.\n", num);
        } else {
            printf("%d is odd.\n", num);
        }
        return 0;
    }

Exercise 2 : Prime-number checker

  1. Create prime.c and implement the prime-number test.

  2. Test the programme with 0, 1, 2, 9, 11 and a negative number.

    ./prime
    Enter a number: 67
    67 is a prime number.
    #include <stdio.h>
    
    int is_prime(int number) {
        if (number < 2) {
            return 0;
        }
        for (int i = 2; i <= number / i; i++) {
            if (number % i == 0) {
                return 0;
            }
        }
        return 1;
    }
    
    int main(void) {
        int num;
    
        printf("Enter a number: ");
        if (scanf("%d", &num) != 1) {
            return 1;
        }
    
        if (is_prime(num)) {
            printf("%d is a prime number.\n", num);
        } else {
            printf("%d is not a prime number.\n", num);
        }
        return 0;
    }

Exercise 3 : Simple calculator

  1. Create calculator.c and implement the calculator using conditional statements.

  2. Test all four operations, division by zero, and an unsupported operator.

    ./calculator
    Enter operation (+, -, *, /): *
    Enter first number: 33.5
    Enter second number: 2
    Result: 67.000000
    #include <stdio.h>
    
    int main(void) {
        char operation;
        double num1, num2, result;
    
        printf("Enter operation (+, -, *, /): ");
        if (scanf(" %c", &operation) != 1) {
            return 1;
        }
    
        printf("Enter first number: ");
        if (scanf("%lf", &num1) != 1) {
            return 1;
        }
    
        printf("Enter second number: ");
        if (scanf("%lf", &num2) != 1) {
            return 1;
        }
    
        if (operation == '+') {
            result = num1 + num2;
        } else if (operation == '-') {
            result = num1 - num2;
        } else if (operation == '*') {
            result = num1 * num2;
        } else if (operation == '/') {
            if (num2 == 0) {
                printf("Cannot divide by zero.\n");
                return 1;
            }
            result = num1 / num2;
        } else {
            printf("Invalid operation.\n");
            return 1;
        }
    
        printf("Result: %f\n", result);
        return 0;
    }