Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara What is a String in C? | Introduction to Strings in C
Working with Strings in C

bookWhat is a String in C?

Note
Definition

Definition: In C, a string is a sequence of characters stored in memory and terminated by a special null character ('\0'). This null character marks the end of the string and distinguishes it from a regular array of characters.

When working with strings in C, you need to remember that C does not have a dedicated string type like some other programming languages. Instead, strings are represented as arrays of characters. Each character in the array occupies one element, and the sequence always ends with the null character ('\0'). This design means you are responsible for managing the memory and ensuring the string is properly terminated.

Because C lacks a built-in string type, you cannot use strings as first-class objects. Instead, you manipulate arrays of type char, and functions that operate on strings expect pointers to these arrays. This approach gives you more control but also requires more care to avoid errors such as missing null terminators or accessing memory out of bounds.

main.c

main.c

copy
1234567
#include <stdio.h> int main() { char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; printf("%s\n", greeting); return 0; }

In this code, you declare a character array named greeting with space for six elements. The first five elements store the characters of the word "Hello", and the sixth element is the null character ('\0'). When you use printf with the %s format specifier, it prints characters from the array until it encounters the null terminator. This is how C recognizes where the string ends, even though the array could be larger than the string itself.

main.c

main.c

copy
12345678
#include <stdio.h> int main() { const char *greeting = "Hello"; printf("%s\n", greeting); return 0; }

The second example uses a pointer to a string literal. Here, greeting is a pointer to the first character of the string "Hello", which is stored as a string literal in read-only memory. This approach is more concise and lets you avoid explicitly managing the null terminator, since string literals are always null-terminated by the compiler. However, modifying the contents of a string literal through the pointer is not allowed and can cause undefined behavior, while character arrays can be modified safely as long as you stay within their bounds. Both approaches store strings as sequences of characters ending with a null character, but the way you interact with them and their mutability differ.

question mark

Which of the following best describes a string in C?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 1

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

bookWhat is a String in C?

Scorri per mostrare il menu

Note
Definition

Definition: In C, a string is a sequence of characters stored in memory and terminated by a special null character ('\0'). This null character marks the end of the string and distinguishes it from a regular array of characters.

When working with strings in C, you need to remember that C does not have a dedicated string type like some other programming languages. Instead, strings are represented as arrays of characters. Each character in the array occupies one element, and the sequence always ends with the null character ('\0'). This design means you are responsible for managing the memory and ensuring the string is properly terminated.

Because C lacks a built-in string type, you cannot use strings as first-class objects. Instead, you manipulate arrays of type char, and functions that operate on strings expect pointers to these arrays. This approach gives you more control but also requires more care to avoid errors such as missing null terminators or accessing memory out of bounds.

main.c

main.c

copy
1234567
#include <stdio.h> int main() { char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; printf("%s\n", greeting); return 0; }

In this code, you declare a character array named greeting with space for six elements. The first five elements store the characters of the word "Hello", and the sixth element is the null character ('\0'). When you use printf with the %s format specifier, it prints characters from the array until it encounters the null terminator. This is how C recognizes where the string ends, even though the array could be larger than the string itself.

main.c

main.c

copy
12345678
#include <stdio.h> int main() { const char *greeting = "Hello"; printf("%s\n", greeting); return 0; }

The second example uses a pointer to a string literal. Here, greeting is a pointer to the first character of the string "Hello", which is stored as a string literal in read-only memory. This approach is more concise and lets you avoid explicitly managing the null terminator, since string literals are always null-terminated by the compiler. However, modifying the contents of a string literal through the pointer is not allowed and can cause undefined behavior, while character arrays can be modified safely as long as you stay within their bounds. Both approaches store strings as sequences of characters ending with a null character, but the way you interact with them and their mutability differ.

question mark

Which of the following best describes a string in C?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 1
some-alt