ENG421 Practice worksheet 1 – Reading source code, compiling programs and debugging
Academic year 2015/16
The online compiler
The first textbox is used to write the source code for the C program.
The second textbox is used to type text that is intended to be input for the program when it is running. Leave this box empty when there is no input.
The third box is used to display the output from compiling and running the program.
Click the Run program button to compile and run the C program.
Question 1
Type the program below into the first textbox of the online compiler.
#include <stdio.h>
int main(void) { int number1, number2;
scanf("%d %d", &number1, &number2 ); printf("The sum is %d", number1 + number2 ); }
Type the text below into the second textbox of the online compiler.
4 5
Click the Run program button to compile the source code, run the program and display the output in the third box of the online compiler.
What is displayed as the output? Why?
The main() function
The main() function is required for all C programs.
The main() function is the where the program starts.
The program ends when the main() function ends.
The main() function should be defined before all other functions.
Question 2
How could the program from Question 1 be modified to perform subtraction of two numbers?
Test your program.
Source code comments
Source code may include comments to describe and explain the design and usage of the program. Comments start with /* and continue until it reaches a matching */ end. For example:
/* Comment text do not affect the functionality of the C program */
Do not use C++, Java or PHP style comments, which use a double forward slash at the indicate the start of comments. For example:
// Do not use comments in this style. They reduce portability between C compilers.
The program above is missing a parenthesis at the end of line 3. Read the error message from the compiler. Look for:
Text description of the compiler error.
Change the source code to fix the error. Test your program.
Question 5
#include <stdio.h>
int main(void)
{
print("Hello world");
}
The program above is using a print() function in line 5, which does not exist. Read the error message from the compiler. Look for:
Text description of the compiler error.
Indications of possible line number causing the compiler error.
Change the source code to fix the error. Test your program.
Syntax errors are caused by mistakes with the source code. The compiler will not run a program with syntax errors. Instead, it will try to display error messages as close as possible to the source of the syntax errors.
Question 6
#include <stdio.h>
int main(void)
{
double furlong = 50;
double kilometers = furlong * 2.011680;
printf("%.0lf furlongs is equivalent to %.2lf kilometers", furlong, kilometers );
}
The program above will compile and run successfully. But, the output is incorrect because there is a logic error in the source code.
Read this Wikipedia entry to find the correct conversion rate and fix the program:
Change the source code to fix the error. Test your program.
Logic errors are caused by mistakes in the algorithm or steps implemented by the programmer. The program compiles and runs, but, the output or functions do not work as expected. These are often the most difficult to find and fix.
Question 7
The cost to stay at a hotel is £24.99 per night. You arrive at the hotel at 4pm on 5 Jan and depart at 8am on 11 Jan. Write a C program to calculate and display the total cost of your stay at the hotel.