# Strings in C – Arrays of Characters

In the previous lessons, you learned about:

*   Arrays
    
*   Pointers
    
*   How arrays and pointers are related
    

Now we will apply that knowledge to something very common in programming: **strings**.

In C, strings are not a special data type like in some other languages.  
Instead, **strings are simply arrays of characters**.

Understanding this will help you work with text in C programs.

## 1\. What Is a String in C?

A string in C is:

> A sequence of characters stored in an array and terminated by a special character called the **null terminator** (`'\0'`).

Example:

```c
char name[6] = "Hello";
```

This string contains:

```text
H  e  l  l  o  \0
```

There are **6 characters** in memory:

*   5 letters
    
*   1 null terminator
    

The null terminator marks the **end of the string**.

## 2\. Why the Null Terminator Is Important

The null character (`'\0'`) tells C where the string ends.

Without it, C would keep reading memory until it accidentally finds a zero.

Example in memory:

```text
Index   Value
0       H
1       e
2       l
3       l
4       o
5       \0
```

That `\0` is essential.

## 3\. Declaring Strings

### Method 1: Using a String Literal

```c
char name[6] = "Hello";
```

C automatically adds the null terminator.

### Method 2: Using Character List

```c
char name[6] = {'H','e','l','l','o','\0'};
```

You must include `'\0'` yourself.

### Method 3: Letting C Determine Size

```c
char name[] = "Hello";
```

C automatically allocates the correct size.

## 4\. Printing Strings

You can print strings using `%s`.

Example:

```c
#include <stdio.h>

int main() {
    char name[] = "Hello";
    printf("%s\n", name);
    return 0;
}
```

Output:

```plaintext
Hello
```

`%s` tells `printf` to print characters until it reaches `'\0'`.

## 5\. Looping Through a String

Since strings are arrays, we can use loops.

Example:

```c
#include <stdio.h>

int main() {
    char word[] = "School";

    for (int i = 0; word[i] != '\0'; i++) {
        printf("%c\n", word[i]);
    }

    return 0;
}
```

This prints each character one by one.

The loop stops when it reaches the null terminator.

## 6\. Common String Functions

C provides useful string functions in `<string.h>`.

### 1\. strlen – String Length

```c
strlen(word);
```

Returns number of characters **excluding** `'\0'`.

Example:

```plaintext
strlen("Hello") → 5
```

### 2\. strcpy – Copy String

```c
strcpy(dest, source);
```

Example:

```c
char a[10];
strcpy(a, "Hello");
```

Copies `"Hello"` into `a`.

### 3\. strcat – Join Strings

```c
strcat(dest, source);
```

Example:

```c
char a[20] = "Hello ";
strcat(a, "World");
```

Result:

```plaintext
Hello World
```

### 4\. strcmp – Compare Strings

```c
strcmp(a, b);
```

Returns:

*   0 → strings equal
    
*   <0 → first string smaller
    
*   > 0 → first string larger
    

Example:

```c
strcmp("cat", "cat") → 0
```

## 7\. Strings and Pointers

Because strings are arrays, they are closely related to pointers.

Example:

```c
char *s = "Hello";
```

Here:

*   `s` points to the first character of the string
    
*   `"Hello"` is stored in memory
    

However, this string is usually **read-only**.

Trying to modify it may cause an error.

Example:

```c
s[0] = 'Y';  // Dangerous
```

Instead, use a character array if you need to modify the string.

## 8\. Example Program

```c
#include <stdio.h>
#include <string.h>

int main() {
    char name[20] = "C Programming";

    printf("String: %s\n", name);
    printf("Length: %lu\n", strlen(name));

    return 0;
}
```

Output:

```plaintext
String: C Programming
Length: 13
```

## 9\. Common Beginner Mistakes

### ❌ Forgetting the Null Terminator

If you manually create a string and forget `'\0'`, functions may behave incorrectly.

### ❌ Array Too Small

```c
char name[5] = "Hello"; // Wrong
```

You need space for `\0`.

Correct:

```c
char name[6] = "Hello";
```

### ❌ Modifying String Literals

```c
char *s = "Hello";
s[0] = 'J';  // unsafe
```

Use a character array instead.

## 10\. Practice Exercises

1.  Create a string `"Programming"` and print each character using a loop.
    
2.  Ask the user for a name and print it.
    
3.  Write a program that:
    
    *   copies one string into another
        
    *   prints both.
        
4.  Compare two strings using `strcmp`.
    

## Final Thoughts

Strings in C are simply **arrays of characters**.

Key ideas to remember:

*   Strings end with `'\0'`
    
*   `%s` prints strings
    
*   Strings can be looped through like arrays
    
*   `<string.h>` provides helpful functions
    

Understanding strings is important because text processing appears in almost every program.

In the next lesson, we will step back and look at **data structures**, where arrays, pointers, and structures combine to organize complex data.
