strlen, strcpy, strcat
Core string functions.
Strings in C
In C, a string is just an array of char that ends with a special null terminator character '\0'.
The functions in <string.h> rely on this terminator to know where a string ends. Without it, they read past the end of your array.
#include <stdio.h>
int main(void) {
char name[] = "Alice";
printf("%s\n", name);
printf("Bytes used: %lu\n", sizeof(name));
return 0;
}Including string.h
To use string functions you must include the header:
#include <string.h>
This gives you strlen, strcpy, strcat, strcmp and more.
#include <stdio.h>
#include <string.h>
int main(void) {
char greeting[] = "Hello";
printf("Length is %lu\n", strlen(greeting));
return 0;
}All lessons in this course
- strlen, strcpy, strcat
- strcmp and Comparison
- strtok and Tokenizing
- memcpy and memset