Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Character Arrays vs String Literals | Introduction to Strings in C
Working with Strings in C

bookCharacter Arrays vs String Literals

Note
Definition

Definition:
A character array is a sequence of characters stored in contiguous memory locations and is typically used to store strings in C. A string literal is a sequence of characters enclosed in double quotes, such as "hello", which the compiler stores in a read-only section of memory. The key difference is that character arrays can be modified (if not declared const), while string literals must not be changed during program execution.

When you work with text in C, you often encounter both character arrays and string literals, but they behave differently in several important ways.

First, consider storage: character arrays are stored in the part of memory allocated for variables, such as the stack or static data segment, depending on their declaration. String literals, however, are stored in a read-only section of memory, which is usually protected against writing.

Next, think about mutability: you can change the contents of a character array after it is created, unless it is explicitly declared as const. In contrast, string literals are immutable; attempting to modify them leads to undefined behavior.

Finally, consider lifetime: character arrays declared locally exist for the duration of their scope (such as a function), while string literals persist for the entire lifetime of the program.

main.c

main.c

copy
123456789101112131415161718
#include <stdio.h> int main() { char arr[] = "hello"; char *lit = "world"; // Attempt to modify character array arr[0] = 'H'; printf("Modified array: %s\n", arr); // Attempt to modify string literal (undefined behavior) // Uncommenting the next two lines may cause a crash or unexpected result // lit[0] = 'W'; // printf("Modified literal: %s\n", lit); return 0; }

If you try to modify a string literal, you invoke undefined behavior because string literals are stored in read-only memory. Many systems will cause your program to crash if you attempt this. On the other hand, modifying a character array is allowed because the array is stored in writable memory allocated for your variables.

main.c

main.c

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

To avoid undefined behavior, always use character arrays when you need to modify string contents. Use string literals only for read-only strings. If you are initializing a string that will not change, a string literal is convenient and efficient. If you plan to alter the string, declare a character array and copy the literal into it if needed. This approach helps ensure your code is safe and portable across different systems.

question mark

Which statement about string literals and character arrays in C is correct?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 3

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

bookCharacter Arrays vs String Literals

Sveip for å vise menyen

Note
Definition

Definition:
A character array is a sequence of characters stored in contiguous memory locations and is typically used to store strings in C. A string literal is a sequence of characters enclosed in double quotes, such as "hello", which the compiler stores in a read-only section of memory. The key difference is that character arrays can be modified (if not declared const), while string literals must not be changed during program execution.

When you work with text in C, you often encounter both character arrays and string literals, but they behave differently in several important ways.

First, consider storage: character arrays are stored in the part of memory allocated for variables, such as the stack or static data segment, depending on their declaration. String literals, however, are stored in a read-only section of memory, which is usually protected against writing.

Next, think about mutability: you can change the contents of a character array after it is created, unless it is explicitly declared as const. In contrast, string literals are immutable; attempting to modify them leads to undefined behavior.

Finally, consider lifetime: character arrays declared locally exist for the duration of their scope (such as a function), while string literals persist for the entire lifetime of the program.

main.c

main.c

copy
123456789101112131415161718
#include <stdio.h> int main() { char arr[] = "hello"; char *lit = "world"; // Attempt to modify character array arr[0] = 'H'; printf("Modified array: %s\n", arr); // Attempt to modify string literal (undefined behavior) // Uncommenting the next two lines may cause a crash or unexpected result // lit[0] = 'W'; // printf("Modified literal: %s\n", lit); return 0; }

If you try to modify a string literal, you invoke undefined behavior because string literals are stored in read-only memory. Many systems will cause your program to crash if you attempt this. On the other hand, modifying a character array is allowed because the array is stored in writable memory allocated for your variables.

main.c

main.c

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

To avoid undefined behavior, always use character arrays when you need to modify string contents. Use string literals only for read-only strings. If you are initializing a string that will not change, a string literal is convenient and efficient. If you plan to alter the string, declare a character array and copy the literal into it if needed. This approach helps ensure your code is safe and portable across different systems.

question mark

Which statement about string literals and character arrays in C is correct?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 3
some-alt