JIYIK CN >

Current Location:Home > Learning > PROGRAM > C语言 >

Printing Character Array in C

Author:JIYIK Last Updated:2025/04/17 Views:

This article will introduce various methods on how to print character array in C language.


forHow to use loop to print character array in C language

If we want to print array elements individually and format the output with more details, fora loop is the most obvious solution. The key premise of this method is that we should know the length of the array in advance.

It should be noted that we can use other iteration methods, such as whileloops, but we should know the value at which point the iteration should stop, otherwise, the iteration will go out of bounds and throw an error.

In the following example, we demonstrate forthe loop method and iterate over an array of six characters exactly six times.

#include <stdio.h>
#include <stdlib.h>

#define STR(num) #num

int main(void) {
    char arr1[] = { 'a', 'b', 'c', 'd', 'e', 'f' };

    printf(STR(arr1)": ");
    for (int i = 0; i < 6; ++i) {
        printf("%c, ", arr1[i]);
    }
    printf("\b\b\n");

    exit(EXIT_SUCCESS);
}

Output:

arr1: a, b, c, d, e, f

Printing character arrays in C using the printfand specifiers%s

printfThe function is a powerful formatted output function. It can operate the type specifier on the input variable and process the variable accordingly.

That is, the internal structure of a character array is the same as a C-style string, except that the characters of a C-style string always \0end with a byte, indicating the end point. If we add a byte to the end of the character array null, we can printfprint the entire array with a single call.

If the ending nullbyte is not specified and this method is called printf, the program may try to access the memory area, which will most likely cause a segmentation fault.

#include <stdio.h>
#include <stdlib.h>

#define STR(num) #num

int main(void) {
    char arr1[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
    char arr2[] = { 't', 'r', 'n', 'm', 'b', 'v', '\0' };

    printf("%s\n", arr1);
    printf("%s\n", arr2);

    exit(EXIT_SUCCESS);
}

Output:

abcdeftrnmbv
trnmbv

As you can see, when we print without nullthe terminating character arr1, we get more characters until we iterate to a nullterminating character - \0.

Another printfway to specialize the function is %sto pass the number of characters in the string in the specifier. One way is to hard-code the length of the string with an integer between the sign %and , or you can replace the sign with another integer argument from the parameter. Note that both methods put the character before the number or asterisk .s*printf.

#include <stdio.h>
#include <stdlib.h>

#define STR(num) #num

int main(void) {
    char arr1[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
    char arr2[] = { 't', 'r', 'n', 'm', 'b', 'v', '\0' };

    printf("%.6s\n", arr1);
    printf("%.*s\n", (int)sizeof arr1, arr2);

    exit(EXIT_SUCCESS);
}

Output:

abcdef
trnmbv

Previous: None

Next:Structure arrays in C

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Flushing stdout output stream in C

Publish Date:2025/04/17 Views:200 Category:C语言

stdout This article will demonstrate various methods on how to flush an output stream in C language . Use function in C language fflush to refresh stdout output stream I/O The C standard library provides a stdio buffered version of the I/O

Using the feof function in C language

Publish Date:2025/04/17 Views:87 Category:C语言

feof This article will introduce several ways to use functions in C language . Use feof the function to check the end-of-file indicator on a file stream in C language feof The function is part of the C standard input/output library and is d

Use the C language setenv function to access environment variables

Publish Date:2025/04/17 Views:122 Category:C语言

setenv This article will introduce several methods of using functions to export environment variables in C language . setenv Use function to export environment variables in C language Every program running on a Unix-base system has an envir

How to get the size of an array in C

Publish Date:2025/04/17 Views:62 Category:C语言

This tutorial explains how to determine the length of an array in C. sizeof() The operator is used to get the size/length of an array. sizeof() Operator determines the size of an array in C sizeof() The operator is a compile time unary oper

Initialize array to 0 in C

Publish Date:2025/04/17 Views:133 Category:C语言

This article explains how to initialize an array to 0 in C language. The declaration of an array in C language is as follows. char ZEROARRAY[ 1024 ]; It becomes all zeros at runtime in the global scope. If it is a local array, there is a si

Structure arrays in C

Publish Date:2025/04/17 Views:188 Category:C语言

This tutorial explains how to create a structure array in C language, which is a collection of multiple structure variables, each of which contains information about a different entity. Structure arrays in C An array is a sequential collect

在C中将整数转换为字符

Publish Date:2024/01/03 Views:135 Category:C语言

本教程介绍了在C中将整数转换为字符的不同方法。在C编程语言中,将整数转换为字符在各种情况下都很重要。在C中,字符是以ASCII值表示的,因此转换过程相对简单。

在 C 语言中使用 typedef enum

Publish Date:2023/05/07 Views:372 Category:C语言

本文介绍了如何在 C 语言中使用 typedef enum。使用 enum 在 C 语言中定义命名整数常量 enum 关键字定义了一种叫做枚举的特殊类型。

C 语言中的静态变量

Publish Date:2023/05/07 Views:170 Category:C语言

本文介绍了如何在 C 语言中使用静态变量。在 C 语言中使用 static 变量在函数调用之间保存变量值

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial