CTC++ example source files
The CTC++ example has the following 5
C source files, 3 .c files and 2 .h files (line numbers added):
1 /* File calc.h ----------------------------------------------- */
2
3 /* Tell if the argument is a prime (ret 0) or not (ret 1) */
4 int is_prime(unsigned val);
1 /* File calc.c ----------------------------------------------- */
2 #include "calc.h"
3 /* Tell if the argument is a prime (ret 1) or not (ret 0) */
4 int is_prime(unsigned val)
5 {
6 unsigned divisor;
7
8 if (val == 1 || val == 2 || val == 3)
9 return 1;
10 if (val % 2 == 0)
11 return 0;
12 for (divisor = 3; divisor < val / 2; divisor += 2)
13 {
14 if (val % divisor == 0)
15 return 0;
16 }
17 return 1;
18 }
1 /* File io.h ------------------------------------------------- */
2
3 /* Prompt for an unsigend int value and return it */
4 unsigned io_ask();
5
6 /* Display an unsigned int value and associated string */
7 void io_report(unsigned val, char* str);
1 /* File io.c ------------------------------------------------- */
2 #include <stdio.h>
3 #include "io.h"
4 /* Prompt for an unsigend int value and return it */
5 unsigned io_ask()
6 {
7 unsigned val;
8 int amount;
9
10 printf("Enter a number (0 for stop program): ");
11 if ((amount = scanf("%u", &val)) <= 0) {
12 val = 0; /* on 'non sense' input force 0 */
13 }
14 return val;
15 }
16
17 /* Display an unsigned int value and associated string */
18 void io_report(unsigned val, char* str)
19 {
20 printf("%u %s\n\n", val, str);
21 }
1 /* File prime.c ---------------------------------------------- */
2 /* This example demonstrates some elementary CTC++ properties. */
3 /* Not meant to be any good solution to the 'prime' problem. */
4
5 #include "io.h"
6 #include "calc.h"
7
8 int main(void)
9 {
10 unsigned prime_candidate;
11
12 while((prime_candidate = io_ask()) > 0)
13 {
14 if (is_prime(prime_candidate))
15 io_report(prime_candidate, "IS a prime.");
16 else
17 io_report(prime_candidate, "IS NOT a prime.");
18 }
19 return 0;
20 }