out: Wednesday, July 02, 2025
due: Monday, July 07, 2025 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
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).
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, andYour code must:
bash$ gcc -Wall -g -std=c17 -c -o NthPrime.o NthPrime.c bash$ gcc -Wall -g -std=c17 -c -o ex5.o ex5.c bash$ gcc -Wall -g -std=c17 -o ex5 ex5.o NthPrime.o
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 must not submit any generated or editor files, just the .c and .h files.
You should submit your exercise using the Gradescope dropbox linked on the course resources web page.