Initialize array to 0 in 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 simple way. The declaration and initialization of the array is as follows.
char ZEROARRAY[1024] = {0};
If an array is partially initialized, the uninitialized elements will receive values of the relevant data type 0. The compiler will fill the unwritten elements with zeros.
If no initializer is specified, then an object with static storage is initialized to 0, as declared below.
static int myArray[10];
If the initializer list is empty or 0 is specified in the initializer list, the array will be initialized to 0. The declaration is as follows:
int number[5] = { };
int number[5] = { 0 };
The simplest way to initialize an array is to iterate over all elements and set each one to 0.
#include <stdio.h>
int main(void)
{
int numberArray[10], counter;
for(counter = 0 ; counter < 5 ; counter++)
{
numberArray[counter] = 0;
}
printf("Array elements are:\n");
for(counter=0; counter<5; counter++)
{
printf("%d",numberArray[counter]);
}
return 0;
}
Output:
Array elements are:
00000
Using C library functionsmemset()
The function memset()is string.ha library function in . It is used to fill a memory block with a specific value.
memset()The syntax of the function is as follows.
void *memset(void *pointerVariable, int anyValue, size_t numberOfBytes);
in,
-
pointerVariableis a pointer variable pointing to the memory block to be filled. -
anyValueIs the value to be set. This is an integer value, but the function uses the unsigned char conversion of this value to fill the memory block. -
numberOfBytesis the number of bytes of the value to be set.
The function returns a pointerVariablepointer to the memory area.
The complete procedure is as follows.
#include <stdio.h>
#include <string.h>
void printArrayvalues(int anyArray[], int anyNumber)
{
int index;
for (index=0; index<anyNumber; index++)
printf("%d ", anyArray[index]);
}
int main(void)
{
int number = 10;
int arrayValues[number];
memset(arrayValues, 0, number*sizeof(arrayValues[0]));
printf("Array after memset()\n");
printArrayvalues(arrayValues, number);
return 0;
}
Output:
Array after memset()
0 0 0 0 0 0 0 0 0 0
Initializing an array in C to 0a value other than
Use gccto initialize an array to 0a value other than , as shown below.
int myArrayValues[1024] = { [ 0 ... 1023 ] = -1 };
Each member of an array can be explicitly initialized by omitting the dimension. The declaration is as follows.
int myArrayValues[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
The compiler will infer the dimensions from the initializer list, and for multidimensional arrays only the outermost dimension can be omitted.
int myPoints[][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9} };
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
在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 变量在函数调用之间保存变量值
C 语言中生成随机数
Publish Date:2023/05/07 Views:159 Category:C语言
-
本文演示了如何在 C 语言中生成随机数。使用 rand 和 srand 函数在 C 语言中生成随机数
C 语言中的 i++ 与++i
Publish Date:2023/05/07 Views:130 Category:C语言
-
本文演示了如何在 C 语言中使用前缀增量与后缀增量运算符。C 语言中++i 和 i++ 记号的主要区别

