C Tutorial
C String Functions
strlen, strcpy, strcat, and strcmp work on null-terminated arrays. Always leave room for the null.
Four functions from string.h
Include string.h. These functions walk a char array until they find a null byte. They do not know the array size. If you copy or append past the end, you overwrite memory. Use destination arrays that are large enough for the text plus the terminator.
| Function | Job |
|---|---|
strlen(s) | Count characters before the null. Does not include the null. |
strcpy(dest, src) | Copy src into dest, including the null. |
strcat(dest, src) | Append src onto dest. |
strcmp(a, b) | Compare. 0 means equal. Negative or positive means ordering. |
strlen and strcpy
dest is 64 characters. That is plenty for a short word. strcpy writes every byte of the source, then a null. After the copy, strlen(dest) matches the source length.
Example
#include <stdio.h>
#include <string.h>
int main(void) {
char dest[64];
strcpy(dest, "compiler");
printf("%s\n", dest);
printf("length: %d\n", (int)strlen(dest));
return 0;
}strcpy does not check that dest is big enough. A 4-byte array cannot hold"compiler". The extra bytes spill into neighboring memory. That is undefined behavior. Size the array for the longest text you will store, plus one for '\0'.
strcat
strcat finds the null in dest and writes the source there. The destination must already hold a valid string, and must still have unused bytes after it. This program starts with"Hello" and appends " C".
Example
#include <stdio.h>
#include <string.h>
int main(void) {
char dest[64];
strcpy(dest, "Hello");
strcat(dest, " C");
printf("%s\n", dest);
printf("length: %d\n", (int)strlen(dest));
return 0;
}Compile at /c/try. gcc. Try a secondstrcat with another short piece. Keep using a large dest.
strcmp
strcmp returns 0 when the strings are equal, a negative value when the first is less, and a positive value when the first is greater. “Less” means earlier in the character set, byte by byte. Test equality with strcmp(a, b) == 0, not with == on the pointers.
Example
#include <stdio.h>
#include <string.h>
int main(void) {
char a[64];
char b[64];
strcpy(a, "cat");
strcpy(b, "cat");
printf("equal: %d\n", strcmp(a, b));
strcpy(b, "dog");
printf("cat vs dog: %d\n", strcmp(a, b));
return 0;
}The first print is 0. The second is negative on a typical system because 'c' is less than 'd'. Do not depend on the exact non-zero number, only on its sign (or on equality).
Leave room for the null
strlen never counts the terminator. strcpy and strcat always write one. If you need 5 letters, the array needs 6 bytes. Bounded cousins such as strncpy exist; they have their own traps and are not a drop-in fix. For this tutorial, pick a large array and keep the text short. Next: type conversion, where integer division truncates and casts make the conversion explicit.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the C editor at /c/try. Change one measurement and check whether the result still has the right unit.
Chemistry
Copy a formula into a buffer
NaCl is sodium chloride. strcpy copies the characters including the final 0. dest[16] has room. A 40-character IUPAC name would not. strlen is 4, not 5; it does not count the 0.
Example
#include <stdio.h>
#include <string.h>
int main(void) {
char dest[16];
strcpy(dest, "NaCl");
printf("%s, length %zu\n", dest, strlen(dest));
return 0;
}Biology
Two motifs, compared
strcmp returns 0 when the sequences match. ATGC is a common teaching tetramer, not a start codon (that is ATG in DNA). A real aligner uses scores and gaps. This is only equality.
Example
#include <stdio.h>
#include <string.h>
int main(void) {
char a[] = "ATGC";
char b[] = "ATGC";
printf("%s\n", strcmp(a, b) == 0 ? "same motif" : "different");
return 0;
}