Learning C
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.
Practical checklist
1. Configure the C compiler
Your shell must be able to locate gcc through the PATH environment variable.
-
Open Git Bash.

-
Open your Bash configuration file:
nano ~/.bashrc -
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. -
Reload the configuration and check the compiler:
source ~/.bashrc gcc --versionUse the
w64devkitpath and commands shown above. This practical uses.exeoutput filenames.Use a system-installed GCC compiler. Confirm it is accessible with:
gcc --versionOn Linux you would normally compile to an executable without the
.exesuffix, such asgcc helloworld.c -o helloworld.A suitable C compiler must be installed. Check the available compiler using
cc --version. You may useccinstead ofgccfor the compilation tasks; the compiler may be Clang rather than GNU GCC.
2. Create your first C project
-
Open Visual Studio Code and create a folder called
Learning_C. -
Inside that folder, create a file called
helloworld.c.
-
Enter the following programme and save it:
#include <stdio.h> int main(void) { printf("Hello World\n"); printf("Goodbye World\n"); return 0; } -
Open the terminal in the project directory and compile the programme:
gcc helloworld.c -o helloworld.exe -
Run the new executable:
./helloworld.exeReading the first programme
#include <stdio.h>makes declarations for standard input/output functions, includingprintf(), available.main()is the entry point for this programme.printf()writes formatted text to standard output;\nstarts a new line.return 0;reports successful termination to the host environment.gcc helloworld.c -o helloworld.execompiles and links the source into an executable.
Expected output
Hello World Goodbye WorldOriginal reference screenshot:

3. Print an integer
We’ll now create inputoutput.c and progressively extend it through the next sections.
-
Create
inputoutput.cin theLearning_Cfolder.code inputoutput.c -
Add this programme:
#include <stdio.h> int main(void) { int testInteger = 5; printf("Number = %d\n", testInteger); return 0; } -
Compile and run it:
gcc inputoutput.c -o inputoutput.exe ./inputoutput.exeExpected output
Number = 5
4. Print floating-point values
-
Replace the contents of
inputoutput.cwith 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; } -
Save, recompile and run:
gcc inputoutput.c -o inputoutput.exe ./inputoutput.exeExpected output
Number = 5 number1 = 13.500000 number2 = 12.400000
5. Print characters
-
Extend
inputoutput.cto 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; } -
Save, compile and run the programme again.
Expected output
Number = 5 number1 = 13.500000 number2 = 12.400000 character = a, value is = 97
6. Read user input
The scanf() function reads formatted input from standard input, usually your terminal keyboard.
-
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; } -
Recompile and execute the programme, then enter an integer when prompted.
gcc inputoutput.c -o inputoutput.exe ./inputoutput.exeExample interaction
Enter an integer: 42 Number = 42The earlier lines printing the fixed variables will appear before this prompt.

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 type | printf output | scanf 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.
-
Create a new file named
dataTypeSize.c.code dataTypeSize.c -
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; } -
Compile, run and record the sizes you observe:
gcc dataTypeSize.c -o dataTypeSize.exe ./dataTypeSize.exe
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
-
Create
IfStatement.cand enter:#include <stdio.h> int main(void) { int num = 10; if (num > 5) { printf("The number is greater than 5.\n"); } return 0; } -
Save, compile and execute it:
gcc IfStatement.c -o IfStatement.exe ./IfStatement.exe -
Change
numto3, rebuild and observe the difference.
9.2 Add else
-
Replace the contents of
IfStatement.cwith:#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; } -
Recompile and test with
num = 3andnum = 10.
9.3 Add else if
-
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; } -
Compile and test three cases: negative, zero and positive integers.
What you should now be able to do
Explain the differences between
if,if/elseandif/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
-
Create
evenOdd.cand implement the requested programme. -
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
-
Create
prime.cand implement the prime-number test. -
Test the programme with
0,1,2,9,11and 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
-
Create
calculator.cand implement the calculator using conditional statements. -
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; }