Topic : Programming In C
Author : Brian Kernighan
Page : << Previous 4  Next >>
Go to page :


sets x to to 5, and  THEN  increments  k  to  6.   The
incrementing  effect  of  ++k and k++ is the same, but their
values are respectively 5 and 6.  We shall soon see examples
where both of these uses are important.

11. Arrays


     In C, as in Fortran or PL/I, it  is  possible  to  make
arrays  whose elements are basic types.  Thus we can make an
array of 10 integers with the declaration

     int x[10];

The square brackets mean subscripting; parentheses are  used
only  for function references.  Array indexes begin at zero,
so the elements of x are

     x[0], x[1], x[2], ..., x[9]

If an array has n elements, the largest subscript is n-1.

     Multiple-dimension arrays are provided, though not much
used  above  two  dimensions.   The declaration and use look
like

     int name[10] [20];
     n = name[i+j] [1] + name[k] [2];


Subscripts can be  arbitrary  integer  expressions.   Multi-
dimension arrays are stored by row (opposite to Fortran), so
the rightmost subscript varies fastest; name has 10 rows and
20 columns.

     Here is a program which reads a line, stores  it  in  a
buffer,  and prints its length (excluding the newline at the
end).

     main( ) {
             int n, c;
             char line[100];
             n = 0;
             while( (c=getchar( )) != '\n' ) {
                     if( n < 100 )
                             line[n] = c;
                     n++;
             }
             printf("length = %d\n", n);
     }



     As a more complicated problem, suppose we want to print
the  count  for  each  line  in the input, still storing the
first 100 characters of each line.  Try it  as  an  exercise
before looking at the solution:

     main( ) {
             int n, c; char line[100];
             n = 0;
             while( (c=getchar( )) != '\0' )
                     if( c == '\n' ) {
                             printf("%d0, n);
                             n = 0;
                     }
                     else {
                             if( n < 100 ) line[n] = c;
                             n++;
                     }
     }



12. Character Arrays; Strings


     Text is usually kept as an array of characters,  as  we
did  with line[ ] in the example above.  By convention in C,
the last character in a character array  should  be  a  `\0'
because  most  programs  that  manipulate  character  arrays
expect it.  For example, printf uses the `\0' to detect  the
end of a character array when printing it out with a `%s'.

     We can copy a character array s  into  another  t  like
this:

             i = 0;
             while( (t[i]=s[i]) != '\0' )
                     i++;



     Most of the time we have to put in our own `\0' at  the
end  of  a string; if we want to print the line with printf,
it's necessary.  This code prints the character count before
the line:

     main( ) {
             int n;
             char line[100];
             n = 0;
             while( (line[n++]=getchar( )) != '\n' );
             line[n] = '\0';
             printf("%d:\t%s", n, line);
     }


Here we increment n in the subscript itself, but only  after
the  previous  value  has been used.  The character is read,
placed in line[n], and only then n is incremented.

     There is one place and one place only where C  puts  in
the  `\0'  at the end of a character array for you, and that
is in the construction

     "stuff between double quotes"

The compiler puts a `\0' at  the  end  automatically.   Text
enclosed in double quotes is called a _s_t_r_i_n_g; its properties
are precisely those of an (initialized) array of characters.

13. For Statement


     The for statement is a somewhat generalized while  that
lets us put the initialization and increment parts of a loop
into a single statement along with the  test.   The  general
form of the for is

     for( initialization; expression; increment )
             statement


The meaning is exactly

             initialization;
             while( expression ) {
                     statement
                     increment;
             }


Thus, the following code does the same  array  copy  as  the
example in the previous section:

             for( i=0; (t[i]=s[i]) != '\0'; i++ );

This slightly more ornate example adds up the elements of an
array:

             sum = 0;
             for( i=0; i<n; i++)
                     sum = sum + array[i];



     In the for statement, the initialization  can  be  left
out  if  you  want,  but the semicolon has to be there.  The
increment is also optional.  It is NOT followed by  a  semi-
colon.   The  second clause, the test, works the same way as
in the while: if  the  expression  is  true  (not  zero)

Page : << Previous 4  Next >>