C String Basics

Beginner
6 min read
Updated: Nov 2026
1 example

What Is String Basics?

In C, a string is a sequence of characters stored in a char array and ended by a '\0' null terminator. Library functions like strlen and printf("%s") stop at that terminator.

Remember
char s[] = "Hi";   /* {'H','i','\0'} — 3 bytes */
size_t n = strlen(s); /* 2 — does not count '\0' */
printf("%s\n", s);   /* prints Hi */

Cheat Sheet

TopicDetail
FormNull-terminated char sequence
End mark'\0' (byte value 0)
Literal"text" — compiler adds \0
Lengthstrlen(s) counts chars before \0

C Examples

Compile with gcc file.c -std=c11 -Wall -o out. Next: Character Arrays.

Example 1

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

int main(void) {
    char msg[] = "CodeToFun";
    printf("text: %s\n", msg);
    printf("strlen: %zu\n", strlen(msg));
    printf("sizeof: %zu\n", sizeof msg);
    return 0;
}

How It Works

  1. "CodeToFun" is copied into msg with a trailing \0.
  2. strlen walks until \0 and returns 9.
  3. sizeof msg is 10 because it includes the terminator.

Edge Cases & Pitfalls

null

Missing terminator

Without \0, %s and strlen read past the array — undefined behavior.

size

strlen vs sizeof

strlen is runtime length; sizeof on an array is storage size including \0.

Key Takeaways

  • Shape: chars plus a trailing \0.
  • Length: use strlen, not sizeof, for logical length of a string.
  • Safety: always leave room for the terminator.

One line: A C string is characters plus a null terminator.

Frequently Asked Questions

No. Strings are conventions on char arrays (or pointers to char) ended by \0. The standard library supplies helpers in string.h.
No. '\0' is a char with value 0. NULL is a null pointer constant used with pointers.
Read Character Arrays, then Declaring Strings and String Input and Output.

Next: Character Arrays

Continue the C strings track.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful