Notes / C / Strings

Strings in C — Revision Notes

A quick-reference cheat sheet: definition, syntax, example, common mistakes and an interview tip.

beginner 📖 2 min revision

Definition

📌 Strings C has no built-in string type.

Syntax

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

Example

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;
}

Output

outputName: CodeyHub Length: 8

Important Points

Common Mistakes

Interview Tip

🎯 Before your interview A very common interview question: "why can't you compare C strings with ==?" — because that compares pointer addresses, not string contents; use strcmp instead.
Back to notes Read full lesson →