C Strings
C has no string type. A string is an array of characters ending with the special character \0. The <string.h> library provides helpers.
ExampleC
#include <stdio.h>
#include <string.h>
int main(void) {
char name[] = "Ada";
printf("%s has %zu letters\n", name, strlen(name));
char greeting[30] = "Hello, ";
strcat(greeting, name);
printf("%s\n", greeting);
printf("%d\n", strcmp("apple", "apple") == 0); // 1: same text
printf("%c\n", name[1]); // d
char copy[10];
strcpy(copy, "copy");
printf("%s\n", copy);
return 0;
}Output
Ada has 3 letters Hello, Ada 1 d copy
Quick check
How does a C string end?
Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.
Tip: use the left and right arrow keys to move between lessons.