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


     Since putchar(c) returns c as its  function  value,  we
could also copy the input to the output by nesting the calls
to getchar and putchar:

     main( ) {
             while( putchar(getchar( )) != '\0' ) ;
     }


What statement is being repeated?  None, or technically, the
null  statement,  because all the work is really done within
the test part of the while.  This version is  slightly  dif-
ferent  from  the  previous  one,  because the final `\0' is
copied to the output before we decide to stop.

8. Arithmetic


     The arithmetic operators are the usual `+',  `-',  `*',
and  `/'  (truncating  integer  division if the operands are
both int), and the remainder or mod operator `%':

     x = a%b;

sets x to the remainder after a is divided by b (i.e., a mod
b).   The  results  are machine dependent unless a and b are
both positive.

     In arithmetic, char variables can  usually  be  treated
like  int  variables.   Arithmetic  on  characters  is quite
legal, and often makes sense:

     c = c + 'A' - 'a';

converts a single lower case ascii character stored in c  to
upper  case, making use of the fact that corresponding ascii
letters are a fixed distance apart.  The rule governing this
arithmetic is that all chars are converted to int before the
arithmetic is done.   Beware  that  conversion  may  involve
sign-extension  _  if  the leftmost bit of a character is 1,
the resulting integer might be negative.  (This doesn't hap-
pen with genuine characters on any current machine.)

     So to convert a file into lower case:

     main( ) {
             char c;
             while( (c=getchar( )) != '\0' )
                     if( 'A'<=c && c<='Z' )
                             putchar(c+'a'-'A');
                     else
                             putchar(c);
     }


Characters  have  different  sizes  on  different  machines.
Further, this code won't work on an IBM machine, because the
letters in the ebcdic alphabet are not contiguous.

9. Else Clause; Conditional Expressions


     We just used an else after an  if.   The  most  general
form of if is

     if (expression) statement1 else statement2

the else part is optional, but often useful.  The  canonical
example sets x to the minimum of a and b:

     if (a < b)
             x = a;
     else
             x = b;


Observe that there's a semicolon after x=a.

     C provides an alternate form of  conditional  which  is
often  more concise.  It is called the ``conditional expres-
sion'' because it is a  conditional  which  actually  has  a
value and can be used anywhere an expression can.  The value
of

     a<b ? a : b;

is a if a is less than b; it is b  otherwise.   In  general,
the form

     expr1 ? expr2 : expr3

means ``evaluate expr1.  If it is not zero, the value of the
whole thing is expr2; otherwise the value is expr3.''

     To set x to the minimum of a and b, then:

     x = (a<b ? a : b);

The parentheses aren't necessary because `?:'  is  evaluated
before `=', but safety first.

     Going a step further, we could write the  loop  in  the
lower-case program as

     while( (c=getchar( )) != '\0' )
             putchar( ('A'<=c && c<='Z') ? c-'A'+'a' : c );



     If's and else's can be used  to  construct  logic  that
branches one of several ways and then rejoins, a common pro-
gramming structure, in this way:

     if(...)
             {...}
     else if(...)
             {...}
     else if(...)
             {...}
     else
             {...}


The conditions are tested in order, and exactly one block is
executed  _  either  the first one whose if is satisfied, or
the one for the last else.  When this block is finished, the
next  statement executed is the one after the last else.  If
no action is to be taken for the ``default'' case, omit  the
last else.

     For example, to count letters, digits and others  in  a
file, we could write

     main( ) {
             int let, dig, other, c;
             let = dig = other = 0;
             while( (c=getchar( )) != '\0' )
                     if( ('A'<=c && c<='Z') || ('a'<=c && c<='z') )  ++let;
                     else if( '0'<=c && c<='9' )  ++dig;
                     else  ++other;
             printf("%d letters, %d digits, %d others\n", let, dig, other);
     }


The `++' operator means ``increment by 1''; we will  get  to
it in the next section.

10. Increment and Decrement Operators


     In addition to the usual `-',  C  also  has  two  other
interesting  unary  operators,  `++'  (increment)  and  `--'
(decrement).  Suppose we want to count the lines in a file.

     main( ) {
             int c,n;
             n = 0;
             while( (c=getchar( )) != '\0' )
                     if( c == '\n' )
                             ++n;
             printf("%d lines\n", n);
     }


++n is equivalent to n=n+1 but clearer, particularly when  n
is  a  complicated expression.  `++' and `--' can be applied
only to int's and char's (and pointers which we haven't  got
to yet).

     The unusual feature of `++' and `--' is that  they  can
be used either before or after a variable.  The value of ++k
is the value of k AFTER it has been incremented.  The  value
of k++ is k BEFORE it is incremented.  Suppose k is 5.  Then

     x = ++k;

increments k to 6 and then sets x to  the  resulting  value,
i.e., to 6.  But

     x = k++;

first

Page : << Previous 3  Next >>