Skip to content

C Strings

Strings in C are arrays of characters terminated by a null character (\0).

1. String Declaration

c
char str1[] = "Hello";           // automatically adds \0
char str2[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
char *str3 = "Hello";            // string literal

2. String Functions

c
#include <string.h>

size_t len = strlen(str);        // get length
char *dest = strcpy(dest, src);  // copy string
int cmp = strcmp(s1, s2);        // compare strings
char *pos = strstr(haystack, needle); // find substring

3. String Input/Output

c
char name[50];
scanf("%49s", name);  // limit input size
printf("Hello, %s!\n", name);

4. Common Operations

c
// Count characters
int count = 0;
while (str[count] != '\0') {
    count++;
}

Content is for learning and research only.