AnyCompiler

Strings

Unlike every other course on this site, C has no String type at all. A C string is a char array terminated by a null byte ('\0'), and functions like strlen, strcat, and strcpy from <string.h> all work by scanning forward until they find that terminator.

#include <stdio.h>
#include <string.h>

int main() {
    char greeting[20] = "Hello";
    strcat(greeting, ", world!");
    printf("%s\n", greeting);
    printf("Length: %zu\n", strlen(greeting));
    return 0;
}
Output
Hello, world!
Length: 13

strcat appended onto greeting in place — it assumes the destination buffer (declared as char[20] here) has enough room left for both the existing content and the addition, plus the null terminator. C never checks that for you; writing past the end of a buffer is the single most common category of C bug, and it's why the earlier warning about undefined behavior matters here especially.

Next: structs — grouping related fields together, C's only mechanism for user-defined data types.

Practice exercise

Given char s[] = "rust lang", print its length using strlen from <string.h>.