Skip to content

Commit 25061e1

Browse files
authoredApr 21, 2025··
[Edit] C Strings (#6566)
* Update strings.md * Update strings.md * minor fixes * fixed format and lint ---------
1 parent 9e02b89 commit 25061e1

File tree

1 file changed

+65
-32
lines changed

1 file changed

+65
-32
lines changed
 

‎content/c/concepts/strings/strings.md

+65-32
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,121 @@
11
---
22
Title: 'Strings'
3-
Description: 'Strings are character sequences that are either expressed in double quotes or as arrays that end with a null character \0.'
3+
Description: 'Strings in C programming are used to store and manipulate sequences of characters.'
44
Subjects:
55
- 'Code Foundations'
66
- 'Computer Science'
77
Tags:
8-
- 'Strings'
9-
- 'Data Types'
10-
- 'Characters'
118
- 'Arrays'
9+
- 'Characters'
10+
- 'Data Types'
11+
- 'Strings'
1212
CatalogContent:
1313
- 'learn-c'
1414
- 'paths/computer-science'
1515
---
1616

17-
**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'`.
17+
**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.
1818

1919
## Syntax
2020

21+
To declare a string without initialization:
22+
2123
```pseudo
22-
char string-name[];
23-
char string_name[] = "abcd";
24-
char string_name[size] = {'a', 'b', 'c', 'd', '\0'};
24+
char string_name[];
2525
```
2626

27-
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:
27+
To declare and initialize a string using a string literal:
28+
29+
```pseudo
30+
char string_name[] = "text";
31+
```
32+
33+
To declare and initialize a string character by character (must include `\0` at the end):
34+
35+
```pseudo
36+
char string_name[size] = {'c', 'h', 'a', 'r', 's', '\0'};
37+
```
38+
39+
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:
2840

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

32-
\* This is required when an optional `size` is passed to square brackets to specify the number of characters in the string.
44+
> **Note:** The null character `'\0'` is important as it marks the end of the string.
3345
34-
## Example
46+
## Example: String Declaration
3547

36-
The following declaration and initialization create a string of `"Howdy"`:
48+
The following declaration and initialization create a string of "Howdy":
3749

3850
```c
3951
char message[6] = {'H', 'o', 'w', 'd', 'y', '\0'};
4052
```
4153
42-
Even though `"Howdy"` has only 5 characters, `message` has 6 characters due to the `null` character at the end of the `message` array.
54+
Even though "Howdy" has only 5 characters, `message` has 6 characters due to the null character at the end of the array.
4355
4456
The above statement can be rewritten as:
4557
4658
```c
4759
char message[] = "Howdy";
4860
```
4961

50-
### Memory
62+
## Memory Representation
5163

52-
Here's the memory presentation:
64+
Here's how a string is stored in memory:
5365

54-
```pseudo
55-
Character | 'H' 'o' 'w' 'd' 'y' '\0'
56-
Index | 0 1 2 3 4 5
57-
Address | 23451 23452 23453 23454 23455 23456
58-
```
66+
| Character | 'H' | 'o' | 'w' | 'd' | 'y' | '\0' |
67+
| --------- | ----- | ----- | ----- | ----- | ----- | ----- |
68+
| Index | 0 | 1 | 2 | 3 | 4 | 5 |
69+
| Address | 23451 | 23452 | 23453 | 23454 | 23455 | 23456 |
5970

6071
## Displaying a String
6172

62-
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.
63-
64-
### Display Example
65-
66-
The following is an example of displaying a string in C:
73+
To display a string in C, the `printf()` function from the `stdio.h` header file can be used along with the `%s` format specifier:
6774

6875
```c
6976
#include <stdio.h>
70-
7177
int main() {
7278
char message[] = "Hi y'all!";
7379
printf("Bot: %s\n", message);
74-
7580
return 0;
7681
}
77-
7882
```
7983

80-
The output would be:
84+
This code produces the following output:
8185

82-
```shell
86+
```
8387
Bot: Hi y'all!
8488
```
8589

8690
## String Functions
8791

88-
The **string.h** header defines a handful of string functions for manipulating arrays of characters.
92+
The `string.h` header defines several string functions for manipulating arrays of characters:
93+
94+
- [`strchr()`](https://www.codecademy.com/resources/docs/c/strings/strchr) - Finds the first occurrence of a given character.
95+
- [`strcmp()`](https://www.codecademy.com/resources/docs/c/strings/strcmp) - Compares two strings and returns an integer value.
96+
- [`strcpy()`](https://www.codecademy.com/resources/docs/c/strings/strcpy) - Copies one string into another string.
97+
- [`strlen()`](https://www.codecademy.com/resources/docs/c/strings/strlen) - Returns the length of the string, excluding the terminating null character.
98+
- [`strtok()`](https://www.codecademy.com/resources/docs/c/strings/strtok) - Breaks a string into a series of tokens using a list of delimiters.
99+
100+
## Frequently Asked Questions
101+
102+
### 1. How many types of strings are there in C?
103+
104+
In C, there is only one type of string: a null-terminated array of characters. However, strings can be implemented in two ways:
105+
106+
- As character arrays (e.g., `char str[10] = "Hello";`)
107+
- As character pointers (e.g., `char *str = "Hello";`)
108+
109+
### 2. What is the main difference between Java strings and C strings?
110+
111+
The main differences between [Java strings](https://www.codecademy.com/resources/docs/java/strings) and C strings are:
112+
113+
- Java strings are objects of the String class, while C strings are character arrays
114+
- Java strings are immutable, while C strings can be modified
115+
- 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`
116+
- Java handles memory management automatically, while C requires manual memory allocation and deallocation for dynamic strings
117+
- Java strings don't need a terminating character, but C strings must end with the null character `'\0'`
118+
119+
### 3. What is a string literal in C?
120+
121+
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.

0 commit comments

Comments
 (0)
Please sign in to comment.