C Programming: Memory, Pointers and the Stack ยท Arrays and Strings
C strings are char arrays ending in a zero byte. Measure, copy, join and compare them with string.h.
#include <stdio.h>
#include <string.h>
int main(void) {
char name[16] = "Ada";
int len = (int)strlen(name); /* counts characters before the '\0' */
char copy[16];
strcpy(copy, name);
strcat(name, " L.");
int same = strcmp(copy, "Ada") == 0;
char first = name[0];
printf("%s %d %s %d %c\n", name, len, copy, same, first);
return 0;
}first is created on the stack with the value 'A'.Ada L. 3 Ada 1 A.0.