C Programming - Task Sheet
Introduction
Task 1
- Create a new folder called “CTasks1”
- You can do this from the file manager, or from the terminal.
- In the terminal
- you can use
lsto see the contents of the current folder - you can use
cdto change directory -cd foldername - you can use
mkdirto make a directory -mkdir CTasks1
- Open Visual Studio Code
- You can do this from the Linux menu
- Choose
Open Folderfrom the menu and select your new folder * Or you can do this from terminal - you can use
codeto launch VSC - if you change to the directory you want to work in VSC will open using that folder
- Create a new file called
HelloWorld.c - Put the code below in the file
#include <stdio.h>
int main(void) {
printf("Hello World\n");
return 0;
}
- Compile and run the code
- Right click on the file and select run
- or
- Select the terminal window (this can be opened from the View menu)
- Enter
gcc HelloWorld.c -o HelloWorldto compile the code to an executable calledHelloWorld. - Enter
./HelloWorldto run the code
Task 2
- Create a new file in the
CTasks1folder calledprintfDemo.c - Open this file in VSC
- Put the code below in the file
#include <stdio.h>
int main(void) {
printf("Hello World\n");
return 0;
}
- Experiment with adding copies of the printf line
- place a
\nin between Hello and World - place a
\tin between Hello and World
- place a
Task 3
- Return to your
printf_democode - Add the two lines of code
int iVal = 65;
printf("Variable 0 %d", iVal);
- What happens if you change the %d to %c?
- Add a float variable with
float fVal=5.6; - Output the new variable using printf.
Task 4
- Return to your
printf_democode - Add these lines to your code
-
float fVal = 3.14159265; printf("float is %f\n", fVal); printf("float is %.2f\n", fVal);
Do you get different output?
- Try adding a new line
printf("float is %6.2f\n", fVal);
What happens? Why?
Task 5
- Return to your
printf_democode - Print out a receipt for the following items Bread £1.20, Milk 95p, Cheese £5, Butter £2.10.
- The price for each item should be stored in a float variable and printed from there.
- Calculate the sum of the items and store it in another variable
- The receipt should be headed “Recipt”, should print out each item and its price, each item on its own line
- The total should be printed from the variable as shown below
Receipt
-------
Bread £1.20
Milk £0.95
Cheese £5.00
Butter £2.10
-----
Total £9.25
Variables
Task 6
- Create a new file and call it
variables_demo.c - Create an
enumcalleddaysfor the days of the week. It should store the name of each day, numbered from 0. - Create an
enumvariable calledtoday. - Create an integer array that stores 7 values {10,15,5,60,20,40,30}.
- Set
todayto Thursday. - Output the value from the integer array for
today.
Functions
Task 7
- Create a new file and call it
functions_demo.c - Write a function that returns the middle value of three integer parameters
Strings
Task 8
- Create a new file and call it
strings_demo.c - Demonstrate outputting the length of a string stored in a char array.
- Demonstrate copying one string to another
- Demonstrate concatenating one string to the end of another
- Demonstrate checking to see if two strings are the same
- Write code that reads in a string from the keyboard and outputs it to the screen.
Bringing it together
Task 9
- Create a new file and call it
morse.c - Write an application that reads in a string of text from the keyboard and converts it to morse code, outputting one the morse for each character on a separate line. The test should consist of only lower case characters and spaces.