Practical C


Part 1: Examples from Above

  1. Based on the example from Page 46 in "A Course in C". Compile and run the following programme.
      #include <stdio.h>
    
      main() {
          int array [10];
          double rreal[10];
          int i;
    
          for (i = 0; i < 10; i++) {
              array[i] = i;
              rreal[i]  = i*0.1;
    
              printf("%d  ", &array[i]);
              printf("%d\n", &rreal[i]);
            }
    
          printf("That's all!\n");
        }
      
    What does the output illustrate?

  2. Example from P58 of "A Course in C". Compile and run the following programme.
        /* Illustrates the difference between call-by-value 
           and call-by-reference. */
    
        #include <stdio.h>
    
        void call_by_value(int y) {
            y = 1;
          }
    
        void call_by_reference(int *y) {
            *y = 1;
          }
    
        main() {
            int x = 5;
            printf("original:                 x = %d\n", x);
    
            call_by_value(x);
            printf("should be same as before: x = %d\n", x);
    
            call_by_reference(&x);
            printf("should have changed:      x = %d\n", x);
          }
      
    Make sure you understand the significance of the output!

  3. Example from P60 of "A Course in C". Compile and run the following programme.
        /* Illustrates the passing of arrays to functions. */
    
        #include <stdio.h>
        #define SIZE 5
    
        int sum(int *data, int num_items) {
            int total, i;
     
            for (i = total = 0;  i < num_items; i++) 
                total = total + *(data + i);     /* Try both versions of  */
                                                 /* this line (see Arrays */
            return total;                        /* as Args, above).      */
          }
    
        main() {
            int values[SIZE], i;
    
            for (i = 0;  i < SIZE; i++)
                values[i] = i;
    
            printf("total = %d\n", sum(values, SIZE));
          }
    
      
    Try both versions of this program (see Arrays as Arguments, above); verify that they give the same results.


Part 2: Run-time Errors

The remaining examples are mainly to illustrate run-time errors, though there also compile-time errors.


...previousup (conts)next...



About this document:

Produced from the SGML: /home/isd/public_html/_course_crash_in_c/_reml_grp/index.reml
On: 3/3/2003 at 17:43:41
Options: reml2 -i noindex -l long -o html -p multiple