Hardware Reference
In-Depth Information
TLimit 5 FindSqr(xval); // call FIndSqr() to find the limit for test divide
for (ptest 5 2; ptest ,5 TLimit; ptest 11 ){
if ((xval % ptest) 55 0) // is remainder zero?
return 0;
}
return 1;
}
Example 5.6
Write a program to find out the number of prime numbers between 100 and 1000.
Solution: One can find the number of prime numbers between 100 and 1000 by calling the
function written in Example 5.5.
#include “c:\cwHCS12\include\hcs12.h”
unsigned long int FindSqr (unsigned long int xval);
unsigned int PrimeTest(unsigned long int xval);
unsigned int prime_count;
void main(void) {
unsigned long int i;
prime_count 5 0;
for (i 5 100; i ,5 1000; i 11 ) {
if (PrimeTest(i))
prime_count 11 ;
}
while(1);
}
unsigned int PrimeTest(unsigned long int xval)
{
unsigned long int TLimit;
unsigned int ptest;
if (xval 5 1)
return 0;
TLimit 5 FindSqr(xval);
for (ptest 5 2; ptest ,5 TLimit; ptest 11 ){
if ((xval % ptest) 55 0)
return 0;
}
return 1;
}
// include FindSqr() function here
This program consists of three functions. It is very common for a C program to consist of
many functions. Functions of the same nature can be placed in the same file and be reused.
5.6.1 Function Prototype
A function cannot be called before it has been defined. This dilemma is solved by using the
function prototype statement. The syntax for a function prototype statement is
return_type function_name (declarations of arguments);
 
Search WWH ::




Custom Search