Clearing a character array in C
This article will introduce several methods to clear character arrays in C language.
memset
Use function to clear char array
in C language
memset
The _set_storage_region function is commonly used to set a storage area to a constant value. This function is part of the standard library and <string.h>
is defined in the _set_storage_region header file.
memset
It takes three parameters - the first one is void
the pointer to the memory area, the second one is the constant byte value and the last one represents the number of bytes to be filled at the given memory address. Note that we can 0
clear the character array by an integer value.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void printCharArray(char *arr, size_t len)
{
printf("arr: ");
for (size_t i = 0; i < len; ++i) {
printf("%c, ", arr[i]);
}
printf("\n");
}
#define LENGTH 20
int main(){
char c_arr[LENGTH] = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
printCharArray(c_arr, LENGTH);
memset(c_arr, 0, LENGTH);
printCharArray(c_arr, LENGTH);
exit(EXIT_SUCCESS);
}
Output:
arr: a, b, c, d, e, f, g, , , , , , , , , , , , , ,
arr: , , , , , , , , , , , , , , , , , , , ,
Alternatively, memset
it can be called with a specific character as the constant byte parameter, which is useful for initializing each given array element with the same value. In this case, we arbitrarily choose the character 0
to fill the array, resulting in an empty memory area.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void printCharArray(char *arr, size_t len)
{
printf("arr: ");
for (size_t i = 0; i < len; ++i) {
printf("%c, ", arr[i]);
}
printf("\n");
}
#define LENGTH 20
int main(){
char c_arr2[LENGTH] = "techarmp array";
printCharArray(c_arr2, LENGTH);
memset(c_arr2, '0', LENGTH);
printCharArray(c_arr2, LENGTH);
exit(EXIT_SUCCESS);
}
Output:
arr: t, e, m, p, , a, r, r, a, y, , , , , , , , , , ,
arr: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Clearing a character array in C
using the bzero
or functionexplicit_bzero
bzero
is another standard library function that 0
fills a memory area with bytes. It requires only two parameters - a pointer to the memory area and the number of bytes to be overwritten. on the other hand, explicit_bzero
is an alternative that guarantees a write operation regardless of compiler optimizations. The compiler analyzes the code for redundant instructions and removes them if the user instructs it to do so, and explicit_bzero
the function is designed for this special case.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void printCharArray(char *arr, size_t len)
{
printf("arr: ");
for (size_t i = 0; i < len; ++i) {
printf("%c, ", arr[i]);
}
printf("\n");
}
#define LENGTH 20
int main(){
char c_arr[LENGTH] = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
printCharArray(c_arr, LENGTH);
bzero(c_arr, LENGTH);
printCharArray(c_arr, LENGTH);
explicit_bzero(c_arr, LENGTH);
printCharArray(c_arr, LENGTH);
exit(EXIT_SUCCESS);
}
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.
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
Printing Character Array in C
Publish Date:2025/04/17 Views:106 Category:C语言
-
This article will introduce various methods on how to print character array in C language. for How 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, f
在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 关键字定义了一种叫做枚举的特殊类型。