Character array in C language

Table of contents

Character array in C language

Initialization of character array



 

Character array in C language


In C language, a character array is a special one-dimensional array used to store character sequences (strings). Character arrays are often used to represent text data. Here is some important information about character arrays in C:

**Define character array:**

To define a character array, you can use the following syntax:

```c
char array_name[size];
```

- `char` represents the character type.
- `array_name` is the name of the character array you define and can be customized.
- `size` represents the size of the array, that is, the number of characters that the character array can store, including the string terminator `'\0''.

For example, the following is the definition of a character array containing 12 characters:

```c
char message[12]; // Define a character array that can store 12
characters```

**Initialize character array:**

Character arrays can be initialized in the following ways:

1. String constant initialization:

   ```c
   char greeting[] = "Hello, World!";
   ```

   In this example, the character array `greeting` is initialized to contain the contents of the "Hello, World!" string. C language will automatically determine the size of the array and add the string terminator `'\0'` at the end.

2. Initialize character by character:

   ```c
   char vowels[] = {'a', 'e', 'i', 'o', 'u'};
   ```

   This will create a character array `vowels`, containing the 5 vowels.

**Access character array elements:**

You can access individual elements in a character array using array subscripts (indexes). The index of the character array starts from 0, for example:

```c
char firstChar = greeting[0]; // Get the first character 'H' of the string
```

**String terminator '\0':**

In C language, a string is terminated by the special character `'\0'` (null character or string terminator). This character indicates the end of the string. When a character array is used to store a string, C language will automatically add `'\0'` to the end of the string. This helps the C function correctly identify the end of the string.

**String functions and operations:**

C language provides many standard library functions for operating character arrays and strings, such as `strlen`, `strcpy`, `strcat`, `strcmp`, etc. These functions help you perform various operations on strings, such as copying, concatenating, comparing, and searching.

The following is a simple example program that demonstrates the definition, initialization, and access of character arrays:

```c
#include <stdio.h>

int main() {
    char greeting[] = "Hello, World!";

    printf("String: %s\n", greeting);
    printf("First character: %c\n", greeting[0]);
    printf("Sixth character: %c\n", greeting [5]);

    return 0;
}
```

This example defines a character array `greeting` and prints the characters in the array and the first and sixth characters in the array.

Initialization of character array


Character arrays can be initialized in different ways, depending on what and how you want to initialize. Here are some common character array initialization methods:

**Method 1: String constant initialization:**

You can use string constants to initialize character arrays. In this case, the C language automatically determines the size of the character array and adds the string terminator `'\0'` at the end.

```c
char message[] = "Hello, World!"; // Initialize the character array and contain the string
content```

**Method 2: Character-by-character initialization:**

You can initialize a character array character by character, placing each character into an element of the character array. In this case you need to specify the size of the character array explicitly.

```c
char vowels[5]; // Define a character array containing 5 characters
vowels[0] = 'a';
vowels[1] = 'e';
vowels[2] = 'i';
vowels[ 3] = 'o';
vowels[4] = 'u';
```

**Method 3: Initialize using the index position of the character array:**

You can initialize elements using the index position of a character array. This is useful when you need to set specific characters for specific positions.

```c
char initials[3];
initials[0] = 'J';
initials[1] = 'D';
initials[2] = 'D';
```

**Method 4: Initialization using string function:**

You can use standard library functions (such as `strcpy`) to initialize a character array, copying the contents of another string into a character array.

```c
#include <string.h>

char destination[20];
char source[] = "Copy me!";
strcpy(destination, source); // Use strcpy to copy the contents of the source string to the destination character
array```

Note that if you use string functions such as `strcpy` to initialize a character array, make sure that the destination character array is large enough to accommodate the source string and its terminator `'\0'`.

These are some common ways to initialize character arrays. Depending on your needs, you can choose the initialization method that suits your task.

Guess you like

Origin blog.csdn.net/qq_50942093/article/details/132913491