First, make sure you read the spec thoroughly! It’s worth your time to make sure you’ve understood what’s going there as much as possible. Also make sure you read through the
.h files as they desrcibe in detail what we’re expecting from each function.
Keep an eye out for what your functions should be returning, and how they should behave in the case of an error.
void *memcpy(void *dst, const void *src, size_t n):
void *is a generic pointer.
size_t strlen(const char *s):
const char *means you can’t change the value that the pointer points to. Makes sense because we just need to get the length so no need to change anything.
int strcmp(const char *s1, const char *s2):
chars directly! (treat them like 1-byte integers and subtract to get a neg/pos/zero result).
unsigned int strtonum(const char *str, const char **endptr):
**endptris a double pointer that we can use to let the caller know which
charthe conversion stopped at.
charis going to be either the first invalid
*stror the null terminator if all characters were valid.
char} which lives somewhere with them (the caller).
charwhere the conversion stopped} in a place where the caller knows to look.
size_t strlcat(char *dst, const char *src, size_t maxsize)
maxsizeaccounts for null terminator
maxsize = 8
src = "89"
dst = "1234567"
dstshould now be pointing to a string that reads “1234567” followed by a null terminator.
maxsize = 15
src = "89"
dst = "1234567"
dstshould now be pointing to a string that reads “123456789” followed by a null terminator.
int unsigned_to_base(char *buf, size_t bufsize, unsigned int val, int base, int min_width)
min_widthis the minimum number of characters (excluding null terminator) your string should be (pad with zeros if too short).
bufsizeis how much space you have to write to (including null terminator).
min_widthfirst, then use
bufsizeto chop off the end of the string if needed
unsigned_to_base(buf, 20, 128, 16, 6)
unsigned_to_base(buf, 20, 128, 4, 6)
unsigned_to_base(buf, 20, 128, 16, 0)
strlcatin this case!)
va_listto retrieve arguments, pass
va_arg(ap, type)(What would be the type for printf?).
snprintfshould be calling
sizeis referring to the buffer size: it includes the null terminator so make space for it / keep it in mind!
widthis referring to the length of the string, i.e. the number of characters NOT including the null terminator.
'\0'somewhere down the line.
printfmodule, so make sure it works properly before moving on.
In the spec, there are some instructions on how to use our
printf implementation to help debug your
printf implementation. It involves temporarily renaming your functions (so that there aren’t two functions with the same name). However, you have to be vigilant and make sure you don’t get the two mixed up (e.g. if my_printf calls library vsnprintf, you can mistakenly believe your code is working when it really isn’t being tested).