0
0
CComparisonBeginner · 3 min read

String vs Character Array in C: Key Differences and Usage

In C, a string is a character array terminated by a null character '\0', while a character array is just a sequence of characters without guaranteed termination. Strings are used for text handling with standard library functions, whereas character arrays can hold any characters and may not represent text unless null-terminated.
⚖️

Quick Comparison

Here is a quick comparison between strings and character arrays in C based on key factors.

AspectStringCharacter Array
DefinitionNull-terminated character array representing textArray of characters, may or may not be null-terminated
TerminationAlways ends with '\0' to mark string endNo automatic termination; programmer must manage
UsageUsed with string handling functions like printf, strlenUsed for general character storage, not necessarily text
MemorySize depends on string length + 1 for '\0'Fixed size defined at declaration
ModificationCan be modified if not a string literalCan be freely modified
InitializationCan be initialized with double quotes (e.g., "hello")Initialized with single quotes or list of chars
⚖️

Key Differences

A string in C is a special kind of character array that always ends with a null character '\0'. This null character signals the end of the string to functions like printf or strlen. Without this, the functions cannot determine where the string ends, which can cause errors or unexpected behavior.

On the other hand, a character array is simply a collection of characters stored in contiguous memory. It does not have to end with a null character, so it may not represent a valid string. You can use character arrays to store any sequence of characters, including binary data or partial strings.

Strings are usually initialized using double quotes, which automatically add the null terminator, while character arrays can be initialized with single characters or a list of characters. Also, string literals are stored in read-only memory, so modifying them is unsafe, but character arrays declared as variables can be changed freely.

⚖️

Code Comparison

This example shows how to declare and print a string in C using a null-terminated character array.

c
#include <stdio.h>

int main() {
    char str[] = "Hello";  // String with null terminator
    printf("String: %s\n", str);
    return 0;
}
Output
String: Hello
↔️

Character Array Equivalent

This example shows a character array without a null terminator. Printing it as a string may cause undefined behavior.

c
#include <stdio.h>

int main() {
    char arr[] = {'H', 'e', 'l', 'l', 'o'};  // No null terminator
    // Printing as string is unsafe, so print characters manually
    printf("Character array: ");
    for(int i = 0; i < 5; i++) {
        putchar(arr[i]);
    }
    putchar('\n');
    return 0;
}
Output
Character array: Hello
🎯

When to Use Which

Choose string (null-terminated character array) when you need to work with text and use standard string functions like strlen, strcpy, or printf. Strings are safer and easier for text processing.

Choose a character array without null termination when you need to store raw data, fixed-size buffers, or sequences of characters that are not necessarily text. This gives you more control but requires careful handling.

Key Takeaways

Strings in C are null-terminated character arrays used for text handling.
Character arrays may not end with '\0' and can store any characters or data.
Use strings for text and standard library functions; use character arrays for raw or fixed data.
Always ensure strings are null-terminated to avoid undefined behavior.
Modifying string literals is unsafe; use character arrays for mutable data.