How to print a character array in C language?

In C language, you can print each character in an array using a loop until the null-terminator ‘\0’ is encountered. Below is an example code:

#include <stdio.h>

int main() {
    char str[] = "Hello, world!";
    int i = 0;

    // 通过循环打印字符数组中的每个字符
    while (str[i] != '\0') {
        printf("%c", str[i]);
        i++;
    }

    return 0;
}

The output result is:

Hello, world!

In the example code above, we declare a character array ‘str’ and set it to “Hello, world!”. We then iterate through each character in the array using a loop and use the printf function to print each character. The loop stops when it encounters the string termination symbol ‘\0’.

bannerAds