1Learning Outcomes¶
Differentiate between an array of characters and a C string.
Know how to use standard C library functions in
string.h.
No video.
2C Strings vs. char arrays¶
A C string is just an array of characters, followed by a null terminator. A null terminator is the byte of all 0’s, i.e., the ‘\0’ character.
The null terminator lets us determine the length of a C string from just a pointer to the beginning of the string. Consider the following code, which is a reasonable implementation of strlen, the standard C library function that computes the length of a string, minus the null terminator.
1 2 3 4 5int strlen(char s[]) { size_t n = 0; while (*(s++) != 0) { n++; } return n; }