C Character Arrays

Beginner
6 min read
Updated: Nov 2026
1 example

What Is Character Arrays?

A character array is an array of char. When you store text and end it with '\0', that array is a C string. You can still treat it as an array: index, loop, and overwrite elements.

Remember
char buf[8];
buf[0] = 'C';
buf[1] = ' ';
buf[2] = '\0';   /* now a string "C " */

Cheat Sheet

TopicDetail
Declarechar a[N]; — N bytes of storage
Indexa[i] — 0-based
As stringPut \0 at the end of the used chars
MutableArray contents can be changed

C Examples

Compile with gcc file.c -std=c11 -Wall -o out. Next: Declaring Strings.

Example 1

C
#include <stdio.h>

int main(void) {
    char word[6] = { 'H', 'e', 'l', 'l', 'o', '\0' };
    printf("%s\n", word);
    word[0] = 'J';
    printf("%s\n", word);
    return 0;
}

How It Works

  1. The brace list builds a string manually, including \0.
  2. printf("%s") prints until the terminator.
  3. Assigning word[0] = 'J' mutates the first character.

Edge Cases & Pitfalls

bound

Off-by-one

Writing past the last index (or forgetting room for \0) overflows the array.

init

Partial init

char a[5] = {'A'}; zeroes the rest — useful, but know the rules.

Key Takeaways

  • Storage: a string lives in a char array (or heap buffer).
  • Access: index like any array.
  • End: the terminator makes array data a string.

One line: Character arrays hold the bytes; the null makes them strings.

Frequently Asked Questions

No — string literals are typically in read-only memory. Use a char array if you need to modify characters.
a[] is an array object with size. p is a pointer that may point at a literal, an array, or heap memory.
Declaring Strings shows the common ways to create string storage.

Next: Declaring Strings

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