Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn String Literals vs. Character Arrays | String Initialization and Input/Output
C Strings

bookString Literals vs. Character Arrays

Swipe to show menu

When working with strings in C, you will often encounter both string literals and character arrays. Understanding the distinction between these two is essential for writing safe and predictable code. A string literal is a sequence of characters enclosed in double quotes, such as "hello". In C, string literals are stored in a read-only section of memory, meaning you should not attempt to modify their contents. On the other hand, a character array is an array of type char that can be initialized with a string literal but occupies its own space in memory, allowing you to change its contents after initialization. The key differences between string literals and character arrays are found in their storage location and mutability: string literals are typically immutable and stored in read-only memory, while character arrays are mutable and reside in writable memory.

main.c

main.c

copy
1234567891011121314151617
#include <stdio.h> int main() { char *str_literal = "hello"; char str_array[] = "hello"; // Attempt to modify the string literal str_literal[0] = 'H'; // This is undefined behavior! // Modify the character array str_array[0] = 'H'; // This is allowed printf("String literal: %s\n", str_literal); printf("Character array: %s\n", str_array); return 0; }

In this example, you see two different ways to represent the string "hello": as a pointer to a string literal and as a character array. Attempting to change the first character of the string literal (str_literal[0] = 'H';) leads to undefined behavior. This is because string literals are stored in a read-only section of memory, and modifying them can cause your program to crash or behave unpredictably. In contrast, modifying the character array (str_array[0] = 'H';) works as expected because the array is stored in writable memory allocated for your program. This difference is crucial when deciding how to store and manipulate strings in your C programs.

main.c

main.c

copy
12345678
#include <stdio.h> int main() { char greeting[] = "hello"; greeting[0] = 'H'; printf("%s\n", greeting); // Output: Hello return 0; }

Character arrays are mutable because they are allocated in writable memory, such as the stack or heap, depending on their declaration. As shown in the previous example, you can safely change the contents of a character array after it has been created. This mutability makes character arrays the preferred choice when you need to modify string data during the execution of your program.

question mark

What happens if you try to modify a string literal in C?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

SectionΒ 2. ChapterΒ 3
some-alt