Answers to Questions 


C Programming Tutorial 

Go to the Table of Contents

 

Chapter 1

1) A tool which translates high level language into machine language.

2) By typing the name of an executable file.

3) By typing something like "cc filename"

4) NO!

5) Compiler errors and runtime errors.

Chapter 3

1) printf ("Wow big deal");

2) printf ("22");

3) printf ("The 3 wise men");
   printf ("The %d wise men",3);

4) Most facilities are held in libraries

Chapter 4

1) To provide a basic set of facilities to the user

2) The filename used by a computer to reference a device

3) accounts.c

4) accounts.x (or perhaps accounts.EXE)

5) By typing the name in 4)

Chapter 5

1) #include <filename> or #include "filename"

2) stdio.h

3) No. Only macro names can be used if the header file is not included.

4) Header file.

Chapter 7

1) A group of statements enclosed by curly braces {}.

2) Comments, preprocessor commands, functions, declarations, variables,
statements. (This is a matter of opinion, of course.)

3) Not necessarily. It starts wherever main() is.

4) It signifies the end of a block, the return of control to somethng else.

5) The semi-colon (;)

Chapter 8

1) The compiler thinks the rest of the program is all one  comment!

Chapter 9

1) function (a,b)

   int a,b;

   {
   return (a*b);
   }

2) No.

3) The value is discarded.

4) The result is garbage.

5) By using "return".

Chapter 10

1) A name for some variable, function or macro

2) a,c,f

3) int i,j;

4) double is twice the length of float and can hold significantly larger values.

5) int can have values + or -. Unsigned can only be + and can hold
slightly larger + values than int.

6) I = 67;

7) int

8) At the function defintion and in the calling function.

9) printf ("%d",(int)23.1256);

10) No.

Chapter 11

1) With variable parameters or with return()

2) Where a function is definned, after its name: e.g.

     function (...)

     <-- here

     {
     }

3) Yes.

4) No and it is illegal.

5) * means "the contents of" and & means "the address of"

6) No.

Chapter 12

1) A global variable can be accessed by any part of a program.

2) A local variable canonly be accessed by a select part of a program.

3) Local variables cannot leak out. Nothing outside them can reach local variables.

4) Variable parameters do. Value parameters use their own local copies, so they do not.

5)  int i,j;

    main ()

   { float x,y;

   another(x,y);
   }

   another(x,y)

   float x,y;

   {
   }

There are 6 storage spaces altogether.

Chapter 13

1) #define birthday 19

2) #include <math.h>

3) false

4) false

Chapter 14

1) A variable which holds the address of another variable

2) With a * character. e.g. int *i;

3) Any type at all!

4) doubleptr = (double *)chptr;

5) Because number has not been initialized. This expression initializes
the place that number points to, not number itself. (See main text)

Chapter 15

printf

1) #include <stdio.h>

   main ()

   {
   printf ("%2e",6.23);
   }

2) This depends on individual compilers

3) a) No conversion string
   b) Conversion string without matching value
   c) Probably nothing
   d) Conversion string without matching value

scanf

1) space, newline or tab

5) true.

Low level I/O

1) The statement is possible provided putchar() is not implemented as a
macro. It copies the input to the output: a simple way of writing on the
screen. (Note however that the output is buffered so characters may not
be seen on the output for some time!)

2) ch = getchar();
   putchar (ch);

Chapter 16

1) The thing(s) an operator acts upon.

2) printf ("%d",5 % 2);

3) rem = 5 % 2;

4) variable = 10 - -5;

5) if (1 != 23)
{
printf ("Thank goodness for mathematics");
}

Chapter 18

1) Three: while, do..while, for

2) while : at the start of each loop
   do    : at the end of each loop
   for   : at the start of each loop

3) do..while

4) #include <stdio.h>
   #define TRUE 1

   main ()

   { char ch;

   while (true)

{
ch = getchar();
putchar (ch);
}

Chapter 19

1) The array identifier (without square brackets) is a pointer to the
first element in the array.

2) You pass the array identifier, without square brackets.
   No! Arrays are always variable parameters.

3) double array[4][5];
   Valid array bounds from array[0][0] to array[3][4]

Chapter 20

1) Arrays of characters. Pointers to arrays of characters.

2) static char *strings[];
  Could then initialize with braces {} and item list. (See main text)

3) See the Morse code example.

Chapter 22

1) double

2) Probably true. This is implementation dependent. The actual types are
double, long float and int.

3) The length of a string (excluding NULL byte)

4) Joins two strings.

5) Overflow, underflow, domain error, Loss of accuracy and division by zero.

Chapter 23

1) ++, -- and any assignment or unary operator

2) It could make a program too difficult to read

3) No. The function would return before the vaue could be incremented.

Chapter 23

1) FILE is defined by stdio.h It is reserved only when this file is
included. It is not a built in part of the language.

2) FILE *fp;

3) False. They are meant for comparitive purposes only. It does not make
sense to do arithmetic with enumerated data.

4) Yes. It provides a generic pointer. i.e. one which can be assigned to
any other pointer type.

5) volatile

6) typedef double real;

7) True.

Chapter 24

1) Nothing -- only the way it is used. Yes, every variable is a bit
pattern. It is normal to use integer or character types for bit
patterns.

2) Inclusive OR is true if all possiblilities are true
simultaneously. Exclusive OR is false if all possibilites are true
simultaneously.

3) Some kind of flag message perhaps. A bit pattern for certain.

4) a) 00000111 & 00000010 == 00000010 == 2
   b) 00000001 & 00000001 == 00000001 == 1
   c) 00001111 & 00000011 == 00000011 == 3
   d) 00001111 & 00000111 == 00000111 == 7
   e) 00001111 & 00000111 & 00000011 == 00000011 = 3

5) a) 00000001 | 00000010 == 00000011 == 3
   b) 00000001 | 00000010 | 00000011 == 00000011 == 3

6) a) 1 & (~1) == 00000001 & 11111110 == 0
   b) 23 & ~23 == 00011111 & 11100000 == 0
   c) similarly 0: n & (NOT n) is always zero

Chapter 26

1) a) a string which labels a file
   b) a variable of type *fp which points to a FILE structure
   c) the number of a file "portal" in the I/O array

2) High level filing performs translations to text. Low level files untranslated bit data.

3) fp = fopen ("filename","r");

4) fd = open ("filename",O_WRONLY);

6) fprintf ()

Chapter 27

1) A structure can hold several values at the same time. A union holds
only one value at any one time.

2) A part of a structure, or a possible occupant of a union.

3) x.mem

4) ptr->mem

5) False.

Chapter 28

1) A diagram which shows how structures are put together.

2) With pointers.

3) False. Pointers are used to reference variables and data structures
are built in such a way as to require only one name for a whole
structure.

4) With pointers. ptr->member etc...

5) ptr=(struct Binary Tree *)malloc(sizeof(struct Binary Tree));

Chapter 29

1) A function which is defined in terms of itself.

2) A data structure run by the C-language for keeping track of function calls and for storing local data.

3) A lot of memory is used as stack space.

Chapter 31

1) Declarations are in the wrong place.

2) Missing closing brace }

3) Missing semi-colon after closing brace };