COMP1721 - Higher Computing 1B

Higher Computing 1B - Week 2 Laboratory Exercises

Introduction to C Programming

Welcome to your first Computing 1B laboratory.

Objectives

In this Lab, you will practice:

Introduction

In this first Lab Exercise, the primary aim is for you to familiarise yourself with the process of creating, compiling and executing simple C programs. Don't forget that your Lab Exercises are assessable. Each week, before the Lab Class finishes, you should demonstrate your work to the tutor. If the tutor says it's ok, then you should submit it using the give command. Once you've done this, you are free to leave the Lab Class. In this Lab you'll be working with a number of small programs.

Instructions

Create a separate directory (lab02 is a good name) using mkdir for this week's lab exercises and change to that directory.

Exercise 1 - Hello World!

Create file hello.c containing the Hello World program from lectures.

Compile and execute this program. The following commands show what ought to happen:

% /home/cs1721/bin/dcc  hello.c
% ./a.out
Hello, World!
Note that the dcc command doesn't print anything if the compilation is successful. You can check that it has produced an executable by typing ls -l and looking for a newly-created a.out file (check the file time to see if it really is new).

Exercise 2 - Fixing Some Bugs

Copy the code below into a file hey.c


/*
 * Variation of the "Hello, World" program
 * Asks for your name and then greets you.
 * Find the bugs, fix them, and then delete this line.
/

#include <stdio.h>

int main(void) {
	char name[16];  /* string to hold name */

	printf("What"s your name? ");
	scanf("%15s", name)
	printf("Hi there, %s!\n);
	return 0;

When you attempt to compile this program you should get a number of errors:

% /home/cs1721/bin/dcc hello.c
hey.c:10: warning: `/*' within comment
hey.c:12: parse error before string constant
hey.c:13: nondigits in number and not hexadecimal
hey.c:14: stray '\' in program
hey.c:14: warning: type defaults to `int' in declaration of `printf'
hey.c:14: warning: data definition has no type or storage class

Find and fix these errors. The working program should behave like this:

% ./a.out
What's your name? Andrew
Hi there, Andrew!

Don't worry that you can't understand the entire program. It contains some C features that we haven't covered in lectures (e.g. the type of the variable name). You should know enough to fix the errors.

Exercise 3 - Summing Some Integers

Now it's time for you to do some programming of your own. We want you to write a C program that will read in two integers n and m and print out the sum of all the values between n and m inclusive. The program should look like this when it's working:
   % ./a.out
   Enter first number: 3
   Enter second number: 5
   Sum [3..5] = 12
Put your program in a file sum.c.

Remember you saw an example of reading a value into an int variable in your tutorial.

What you need to do is: fill in the main program, get it to compile, test it on a number of examples, and then demonstrate it to your tutor.

  • Once your tutor has said that the program is ok, you should submit it as follows:
    % give cs1721 lab02 sum.c
    


    Andrew Taylor (andrewt@cse.unsw.edu.au)
    Higher Computing 1B, Computer Science & Engineering, UNSW