1.4. Some more programsWhile we're still in the informal phase, let's look at two more examples. You will have to work out for yourself what some of the code does, but as new or interesting features appear, they will be explained. 1.4.1. A program to find prime numbers
/*
*
* Dumb program that generates prime numbers.
*/
#include <stdio.h>
#include <stdlib.h>
main(){
int this_number, divisor, not_prime;
this_number = 3;
while(this_number < 10000){
divisor = this_number / 2;
not_prime = 0;
while(divisor > 1){
if(this_number % divisor == 0){
not_prime = 1;
divisor = 0;
}
else
divisor = divisor-1;
}
if(not_prime == 0)
printf("%d is a prime number\n", this_number);
this_number = this_number + 1;
}
exit(EXIT_SUCCESS);
}Example 1.2What was interesting in there? A few new points, perhaps. The program
works in a really stupid way: to see if a number is prime, it divides that
number by all the numbers between half its value and two—if any
divide without remainder, then the number isn't prime. The two operators
that you haven't seen before are the remainder operator The problem with the equality test is that wherever it can appear it is
also legal to put the single if(a == b) while (c == d) The assignment operator There is also the introduction for the first time of the
if(expression)
statement
if(expression)
statement
else
statement
showing that it comes in two forms. Of course, the effect is that if the
expression part is evaluated to be true, then the following statement is
executed. If the evaluation is false, then the following statement is not
executed. When there is an
if(1 > 0)
if(1 < 0)
statement-1
else
statement-2
The answer is that it is. Ignore the indentation (which is
misleading). The
if(1 > 0){
if(1 < 0)
statement-1
}
else
statement-2
Here, at least, C adheres to the practice used by most other languages. In fact a lot of programmers who are used to languages where the problem exists have never even realized that it is there—they just thought that the disambiguating rule was ‘obvious’. Let's hope that everyone feels that way. 1.4.2. The division operatorsThe division operators are the division operator 1.4.3. An example performing inputIt's useful to be able to perform input as well as to write programs
that print out more or less interesting lists and tables. The simplest of
the library routines (and the only one that we'll look at just now) is
called
#include <stdio>
#include <stdlib.h>
main(){
int ch;
ch = getchar();
while(ch != 'a'){
if(ch != '\n')
printf("ch was %c, value %d\n", ch, ch);
ch = getchar();
}
exit(EXIT_SUCCESS);
}Example 1.3There are two interesting points in there. The first is to notice that at the end of each line of input read, the character represented by '\n' (a character constant) will be seen. This just like the way that the
same symbol results in a new line when If you try that program out, you may find that some systems do not pass characters one by one to a program, but make you type a whole line of input first. Then the whole line is made available as input, one character at a time. Beginners have been known to be confused: the program is started, they type some input, and nothing comes back. This behaviour is nothing to do with C; it depends on the computer and operating system in use. 1.4.4. Simple arraysThe use of arrays in C is often a problem for the beginner.
The declaration of arrays isn't too difficult, especially the
one-dimensional ones, but a constant source of confusion is the fact
that their indices always count from 0. To declare an array of
5 int something[5]; In array declarations C uses square brackets, as you can see. There is
no support for arrays with indices whose ranges do not start at 0 and go
up; in the example, the valid array elements are This program reads some characters from its input, sorts them into the order suggested by their representation, then writes them back out. Work out what it does for yourself; the algorithm won't be given much attention in the explanation which follows.
#include <stdio>
#include <stdlib.h>
#define ARSIZE 10
main(){
int ch_arr[ARSIZE],count1;
int count2, stop, lastchar;
lastchar = 0;
stop = 0;
/*
* Read characters into array.
* Stop if end of line, or array full.
*/
while(stop != 1){
ch_arr[lastchar] = getchar();
if(ch_arr[lastchar] == '\n')
stop = 1;
else
lastchar = lastchar + 1;
if(lastchar == ARSIZE)
stop = 1;
}
lastchar = lastchar-1;
/*
* Now the traditional bubble sort.
*/
count1 = 0;
while(count1 < lastchar){
count2 = count1 + 1;
while(count2 <= lastchar){
if(ch_arr[count1] > ch_arr[count2]){
/* swap */
int temp;
temp = ch_arr[count1];
ch_arr[count1] = ch_arr[count2];
ch_arr[count2] = temp;
}
count2 = count2 + 1;
}
count1 = count1 + 1;
}
count1 = 0;
while(count1 <= lastchar){
printf("%c\n", ch_arr[count1]);
count1 = count1 + 1;
}
exit(EXIT_SUCCESS);
}Example 1.4You might note that the defined constant Unlike some other languages it is unlikely that you will be told if you ‘run off’ the end of an array in C. It results in what is known as undefined behaviour on the part of your program, this generally being to produce obscure errors in the future. Most skilled programmers avoid this happening by rigorous testing to make sure either that it can't happen given the particular algorithm in use, or by putting in an explicit test before accessing a particular member of an array. This is a common source of run-time errors in C; you have been warned. SummaryArrays always number from A |
The C BookThis book is published as a matter of historical interest. Please read the copyright and disclaimer information. GBdirect Ltd provides up-to-date training and consultancy in C, Embedded C, C++ and a wide range of other subjects based on open standards if you happen to be interested. |
|
West Yorkshire Office
GBdirect Ltd
Training: 0800 651 0338 Please call between 0900 and 1700 (UK time) on Monday to Friday South East Regional Office
GBdirect Ltd
Training: 0800 651 0338 Please call between 0900 and 1700 (UK time) on Monday to Friday Please note: |