out: Friday, April 11, 2025
due: Monday, April 14, 2025 by 10:00 am,
No late exercises accepted.
Goals: Continue exploring multi-file C programs by adding details not part of the previous exercise: header guards, internal linkage for "private" helper functions, and using a Makefile to control building the program.
Description: In this exercise we will modify the implementation of the multi-file C program from the previous exercise. The specification remains exactly the same. The program should have the following three files (specification copied with no changes from the previous exercise, except that ex4.c should be renamed ex5.c):
NthPrime.h
: a header file, containing a single function
prototype declaration for a function called NthPrime()
, as well as comments
above the prototype documenting how to use the function. The
function should accept a single int16_t
parameter, and it
should return an int64_t
. The function should return the nth
prime number, where n
is the function's parameter. Note that
NthPrime(1)
should return 2, NthPrime(2)
should return 3,
NthPrime(3)
should return 5, and so on.
The result of NthPrime(n)
is not defined if n
<= 0, and
implementations do not need to check for this possibility.
NthPrime.c
: a file containing the implementation of
NthPrime
. Feel free to use the simplest possible primality
testing algorithm (hint: "x is not prime if it is divisible by ...").
You may also want to define some helper functions here as well.
ex5.c
: a file containing a main()
function that tests
NthPrime
by printing the input and output output of NthPrime
for at least two different, non-trivial arguments (but you do not need to
print an enormous number of test cases - keep it reasonable).
The differences from the previous exercise are that this time your implementation must:
NthPrime.h
header file,static
to ensure that "private" helper functions (if any) in NthPrime.c
have internal, not external linkage and thus are not visible to other files, andMakefile
as described below.
The Makefile
must have the following properties:
make
should compile source file(s) as needed and then link the resulting object files to create an
executable program named ex5
.Makefile
should only recompile
individual files and rebuild the program when needed and should reuse any
existing .o
object files or other files that are already up to date. -Wall -g -std=c17
options, plus any other options needed to separately compile
files (e.g., -c
)
and later combine them into an executable program. make clean
should remove the ex5
executable file, all .o
files, and any editor or other backup files
whose names end with ~
(for example, ex5.c~
).Makefile
from lecture to use here.
Your code must:
Makefile
as described above that compiles code using the gcc options
-Wall -std=c17 -g
.cpplint --clint
reports any problems.
However, you may ignore warnings from cpplint --clint
about
the header guard #define
name not including a full file path
as long the identifier has an otherwise appropriate name.)You should submit your exercise using the Gradescope dropbox linked on the course resources web page.