TEACH C.EX2 A.Sloman May 1988 updated S.Easterbrook Oct 1994 C PROGRAMMING - SECOND SET OF EXERCISES --------------------------------------- Note - these exercises are for you to try on your own. CONTENTS - (Use g to access required sections) -- Use of "external" declaration, and linking different files -- Arrays and pointers -- Use of scanf and pointers -- TO BE EXTENDED -- Use of "external" declaration, and linking different files --------- C allows you to define portions of programs in different files, and then combine them into a single program. You can compile the '.c' files separately producing object files with names ending in '.o'. When all the files have been compiled successfully they can be combined, i.e. linked, into a single program. However, if one file uses a procedure name (or other identifier) declared in another file, it must be declared as external in the file in which it is not defined. Thus, create a file called addup.c which defines a function addup, as explained in TEACH * C.EX1, but WITHOUT the function main. So the file will contain only something like this: long addup(start,incr,fin) long start,incr,fin; { long total=0; for(;start <= fin; start=start+incr) total=start+total; return(total); } You can compile this file into an object file called addup.o by running the C compiler cc, with the "-c" flag. E.g. using the VED ccomp facility, put the cursor in the addup.c file and do comp -c If all goes well, you will get no errors, and nothing will be printed out, but you will have created a file called addup.o You can now define a separate file do_addup.c that calls the addup function, but doesn't include its definition. Instead, it declares addup as external, and defines a "main" procedure that calls it. E.g. your do_addup.c file could contain the following extern long addup(); /* Defined in addup.c */ main() { printf("Total for 2,2,8 is %d\n",addup(2,2,8)); printf("Total for 0,5,30 is %d\n",addup(0,5,30)); } You can now compile this file and link it with addup.o to produce an executable image called addup, if you do the following ccomp addup.o do_addup.c -o addup If all is well it will create the file addup and run it, and read the results into a VED file. Alternatively you can give the Unix shell the command cc addup.o do_addup.c -o addup and when that is finished, type to the shell addup Notice that if you leave out the "-o addup" part it will create an executable file called "a.out", the default name. Notice also that it is not essential first to create the addup.o file. You can ask for several .c files to be compiled, linked, and run, e.g. ccomp addup.c do_addup.c -o addup or to Unix shell cc addup.c do_addup.c -o addup For more information on compiling and running C programs from VED see HELP CCOMP. For information about doing it direct see the Unix "man" file "man cc". WRITE NOTES FOR YOURSELF, summarising the procedures for compiling either a single C file or for linking several files. -- Arrays and pointers ------------------------------------------------ The file ~steveea/examples2.c contains a number of examples to do with arrays of characters (i.e. strings) and pointers. Copy the file to your directory. First use the following to compile and run it: ccomp -i (where the "-i") indicates that it is an interactive program, expecting you to type something when the program is run. Then work through the file, making sure you understand everything. It includes some exercises set in the comments. Try them out. (These are not intended as exercises to be handed in.) -- Use of scanf and pointers ------------------------------------------ Interactive programs can read in a line of text using getchar, as illustrated by the procedure read_string in ~cs2/examples2.c . If you wish to break up the line of text into words, numbers, etc. you would need to read a line of characters into a string, then start analysing the string. The C library procedure -scanf- provides a convenient way to do this. It is a bit like the reverse of -printf-. Try it out with a file called calc.c containing just the following primitive desk calculator. In order to understand the role of -scanf- you need to appreciate that the arguments it is given after the control string are POINTERS, as indicated by the use of "&". Put another way, it is given the addresses into which it is to put the items read in. For full details see the unix file "man scanf" /* File: calc.c Repeatedly read in a line consisting of an integer followed by an operator (one of "+", "-", "*" and "/") and then print out the result of applying the operator to the two integers. */ main() { long arg1, arg2, res; char op; while (1) { printf("Type \n"); /* read in three items, and put them into three pointers */ scanf("%d %c %d", &arg1, &op, &arg2); switch (op) { case '+' : res = arg1 + arg2; break; case '-' : res = arg1 - arg2; break; case '*' : res = arg1 * arg2; break; case '/' : res = arg1 / arg2 ; break; default : return(0); } printf("that is %d\n", res); } ; } EXERCISE: 1. Extend this to allow the C operator "%" (corresponding to the Pop-11 operator "mod" to be used. 2. Modify the case for "/" to ensure that division by 0 is not attempted. -- TO BE EXTENDED ----------------------------------------------------- --- $poplocal/local/teach/c.ex2 --- Copyright University of Sussex 1988. All rights reserved. ----------