Curriculum / C / Lesson 10

Strings in C

C has no built-in string type.

10 of 11
beginner 📖 5 min read 🎯 1 question

Strings

C has no built-in string type. A string is simply an array of char that ends with a special null character '\0', which marks where the string stops.

The string.h header provides helper functions like strlen, strcpy, and strcat for working with strings.

💡 Key insight Forgetting the null terminator is one of the most common sources of bugs (and security issues) in C string handling.

Syntax

syntax
char str[] = "text";
#include <string.h>
strlen(str);
strcpy(dest, src);
strcat(dest, src);

Example

%s prints a string; strlen counts characters up to (but not including) the null terminator.

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

int main() {
    char name[20] = "CodeyHub";
    printf("Name: %s\n", name);
    printf("Length: %lu\n", strlen(name));
    return 0;
}
outputName: CodeyHub Length: 8

Try it yourself

Copy this example into the C compiler and run it — try changing a value and see what happens.

Quick Quiz

1. What character marks the end of a string in C?

Back to curriculum