# Programming Tools: Prime Numbers



## Maelstorm (Jul 20, 2012)

Here's a tool that I wrote that I would like to share with the community.  It takes a file with a bunch of prime numbers in it, picks a random set from the list, and formats that set into a C style array that one can read into their C source code.

Hope you find it useful.


Use the following command to build:

[CMD=""]gcc -std=c99 -pedantic -Wall -Wextra -o primes primes.c[/CMD]

The big 6 digit file is in 2 parts.  Just do this:

[CMD=""]cat primesin6.1 primesin6.2 > primesin6[/CMD]


----------



## fluca1978 (Jul 23, 2012)

Good job. Just a few notes: 
- calloc(3) already fills with zero the memory, so you don't need a wrapper
- strncpy(3) is a safe copy string (the difference with yours is that yours is always terminated)
- getopt(3) can be useful for processing command line arguments.


----------



## Maelstorm (Jul 23, 2012)

fluca1978 said:
			
		

> Good job. Just a few notes:
> - calloc(3) already fills with zero the memory, so you don't need a wrapper
> - strncpy(3) is a safe copy string (the difference with yours is that yours is always terminated)
> - getopt(3) can be useful for processing command line arguments.



I'm glad that someone though so.  I am aware of the other malloc based functions, but calloc requires different parameters than malloc.  From the man page:



> The calloc() function allocates space for number objects, each size bytes
> in length.  The result is identical to calling malloc() with an argument
> of ``number * size'', with the exception that the allocated memory is
> explicitly initialized to zero bytes.



I didn't want to deal with it so I just did it the way that I've always done it.  There is also a strlcpy(3) which does what I do for termination of strings, but it's not portable while strncpy is.  I did look at getopt but I wasn't entirely impressed with it, so I used my own function that's worked for many years (I originally developed it while I was taking a C language class in college 20 years ago on a MS-DOS system. :stud).  I am aware, but I made design decisions based on what I already know.

:e:e Don't reinvent the wheel if you don't have too and if it ain't broke, don't fix it.  (Took me forever to finally learn that second one.):e:e


----------

