Skip to content

[Edit] C Strings #6566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 65 additions & 32 deletions content/c/concepts/strings/strings.md
Original file line number Diff line number Diff line change
@@ -1,88 +1,121 @@
---
Title: 'Strings'
Description: 'Strings are character sequences that are either expressed in double quotes or as arrays that end with a null character \0.'
Description: 'Strings in C programming are used to store and manipulate sequences of characters.'
Subjects:
- 'Code Foundations'
- 'Computer Science'
Tags:
- 'Strings'
- 'Data Types'
- 'Characters'
- 'Arrays'
- 'Characters'
- 'Data Types'
- 'Strings'
CatalogContent:
- 'learn-c'
- 'paths/computer-science'
---

**Strings** are character sequences that are either expressed in double quotes or as [arrays](https://www.codecademy.com/resources/docs/c/arrays) that end with a `null` character `'\0'`.
**Strings** in C are one-dimensional arrays of characters terminated by a null character `'\0'`. They are used to store and manipulate sequences of characters such as words or sentences in C programming.

## Syntax

To declare a string without initialization:

```pseudo
char string-name[];
char string_name[] = "abcd";
char string_name[size] = {'a', 'b', 'c', 'd', '\0'};
char string_name[];
```

Strings in C are first declared with the `char` data type, followed by the `string_name`, and then immediately followed by square brackets `[]`. The snippet above showcases the two ways that string values are initialized:
To declare and initialize a string using a string literal:

```pseudo
char string_name[] = "text";
```

To declare and initialize a string character by character (must include `\0` at the end):

```pseudo
char string_name[size] = {'c', 'h', 'a', 'r', 's', '\0'};
```

Strings in C are declared using the `char` data type, followed by the string name and square brackets `[]`. String values can be initialized in two ways:

- Zero or more characters, digits, and escape sequences surrounded in double quotes.
- An array of comma-separated characters, surrounded in curly brackets `{ }`, and ending with a `null` character `'\0'`\*.
- An array of comma-separated characters, surrounded in curly brackets `{}`, and ending with a null character `'\0'`.

\* This is required when an optional `size` is passed to square brackets to specify the number of characters in the string.
> **Note:** The null character `'\0'` is important as it marks the end of the string.

## Example
## Example: String Declaration

The following declaration and initialization create a string of `"Howdy"`:
The following declaration and initialization create a string of "Howdy":

```c
char message[6] = {'H', 'o', 'w', 'd', 'y', '\0'};
```

Even though `"Howdy"` has only 5 characters, `message` has 6 characters due to the `null` character at the end of the `message` array.
Even though "Howdy" has only 5 characters, `message` has 6 characters due to the null character at the end of the array.

The above statement can be rewritten as:

```c
char message[] = "Howdy";
```

### Memory
## Memory Representation

Here's the memory presentation:
Here's how a string is stored in memory:

```pseudo
Character | 'H' 'o' 'w' 'd' 'y' '\0'
Index | 0 1 2 3 4 5
Address | 23451 23452 23453 23454 23455 23456
```
| Character | 'H' | 'o' | 'w' | 'd' | 'y' | '\0' |
| --------- | ----- | ----- | ----- | ----- | ----- | ----- |
| Index | 0 | 1 | 2 | 3 | 4 | 5 |
| Address | 23451 | 23452 | 23453 | 23454 | 23455 | 23456 |

## Displaying a String

To display a string in C, the [`printf()`](https://www.codecademy.com/resources/docs/c/basic-output) function from the **stdio.h** header file can be used along with the `%s` character to format strings, followed by the string name.

### Display Example

The following is an example of displaying a string in C:
To display a string in C, the `printf()` function from the `stdio.h` header file can be used along with the `%s` format specifier:

```c
#include <stdio.h>

int main() {
char message[] = "Hi y'all!";
printf("Bot: %s\n", message);

return 0;
}

```

The output would be:
This code produces the following output:

```shell
```
Bot: Hi y'all!
```

## String Functions

The **string.h** header defines a handful of string functions for manipulating arrays of characters.
The `string.h` header defines several string functions for manipulating arrays of characters:

- [`strchr()`](https://www.codecademy.com/resources/docs/c/strings/strchr) - Finds the first occurrence of a given character.
- [`strcmp()`](https://www.codecademy.com/resources/docs/c/strings/strcmp) - Compares two strings and returns an integer value.
- [`strcpy()`](https://www.codecademy.com/resources/docs/c/strings/strcpy) - Copies one string into another string.
- [`strlen()`](https://www.codecademy.com/resources/docs/c/strings/strlen) - Returns the length of the string, excluding the terminating null character.
- [`strtok()`](https://www.codecademy.com/resources/docs/c/strings/strtok) - Breaks a string into a series of tokens using a list of delimiters.

## Frequently Asked Questions

### 1. How many types of strings are there in C?

In C, there is only one type of string: a null-terminated array of characters. However, strings can be implemented in two ways:

- As character arrays (e.g., `char str[10] = "Hello";`)
- As character pointers (e.g., `char *str = "Hello";`)

### 2. What is the main difference between Java strings and C strings?

The main differences between [Java strings](https://www.codecademy.com/resources/docs/java/strings) and C strings are:

- Java strings are objects of the String class, while C strings are character arrays
- Java strings are immutable, while C strings can be modified
- Java strings have built-in methods like [`length()`](https://www.codecademy.com/resources/docs/java/strings/length), [`concat()`](https://www.codecademy.com/resources/docs/java/strings/concat), while C strings require external functions from libraries like `string.h`
- Java handles memory management automatically, while C requires manual memory allocation and deallocation for dynamic strings
- Java strings don't need a terminating character, but C strings must end with the null character `'\0'`

### 3. What is a string literal in C?

A string literal in C is a sequence of characters enclosed in double quotes (e.g., `"Hello"`) that represents a string constant. When a string literal appears in code, it is stored in read-only memory with an automatically appended null character `'\0'` at the end. String literals cannot be modified, and attempting to modify them results in undefined behavior.