×

Multilevel Feedback Queue Scheduling (MLFQ) CPU Scheduling

In this article, you will learn how to initialise an array to 0 in C

In C, an array is declared as:

char ZEROARRAY[2022];

The global scope changes at runtime to all zeros. If the array is local, there is a shortcut method. As shown below, the declaration and initialization.

char ZEROARRAY[1024] = {0};

When an array is partially initialised, the value 0 of the appropriate data type will be applied to any items that are not initialised. The compiler will insert zeros into any unwritten entries.

The static storage-equipped objects initialise to 0 if no initializer is given. The declaration reads as follows:

static int myArray[20];

An array is initialized to 0 if the initializer list is empty or 0 is specified in the initializer list. The declaration is as given below:

int number[10] =  { };
int number[10] =  { 0 };

However, the array is given all useless values by merely making this declaration. There are a variety of circumstances and cases in which we must initialise each element to ZERO (0) before doing any additional calculations.

The most simplistic initialization method is to cycle through all the components and set them all to 0.

int arr[100];
int k = 0;
for(k = 0 ; k < 50 ; k++)
    arr[k] = 0;  // All will then be zero as a result.

We also have the following 3 basic methods.

1) Static and global variables are both initialised automatically to zero. At runtime, an array that is in the global scope will be empty.

int arr[2022]; // Global in scope
int main(void)
{
    // the statements
}

2) If you had a local array, there is also a shorthand syntax available. If an array is partially initialised, the value 0 of the proper type is assigned to any uninitialized items. The unwritten entries would be filled with zeros by the compiler. You might type:

int main(void)
{
    int arr[2022] = {0};  // All will thereafter become ZERO.
    // the statements
}

As an alternative, you may populate the array using memset when the programme launches. If you altered it and wished to reset it to zeros, this command is quite helpful.

int arr[2022];
arr[10] = 60;
memset(zeroarray, 0, 2022); // This will reset everything to zero.

We can include the two contents here:

  • Use memset() from the C library
  • Set the Array's initial values to something other than 0.

Looping through every element and setting them all to 0 is the simplest way to initialise an array.

#include <stdio.h>
#include<conio.h>
int main(void)
{
    int numberArray[20], kamar;
    for(kamar = 0 ; kamar < 10 ; kamar++)
    {
        numberArray[kamar] = 0;
    }
    printf("Elements of the Array are:\n");
    for(kamar=0; kamar<10; kamar++)
    {
        printf("%d",numberArray[kamar]);
    }
    return 0;
}

Output

How to initialize array to zero in C

Use memset() from the C library

The string.h library contains the function memset(). It is used to store a specific value in a block of memory.

The memset() function's syntax is as follows:

void *memset(void *pointerVariable, int anyValue, size_t numberOfBytes);

where,

  • a pointer Variable is a variable that points to the memory block that needs to be filled. Simply it can be called ptr for easy understanding.
  • The value that must be set is any Value. This number is an integer, but the function converts it to an unsigned char to fill the block of memory.
  • The amount of bytes that will be used to store the value is given by the number Of Bytes.

An access point to the memory location pointerVariable is returned by this function.

Let us see an example program for the above-mentioned category.

// C programme to illustrate the memset functionality ()
#include <stdio.h>
#include <conio.h>
#include <string.h>
void printArrayvalues(int anyArray[], int anyNumber)
{
    int literature;
    for (literature=0; literature<anyNumber; literature++)
        printf("%d ", anyArray[literature]);
}
int main(void)
{
    int num = 12;
    int arrayValues[num];
    // / Add zeros to the entire array.  
    memset(arrayValues, 0, num*sizeof(arrayValues[0]));
    printf("Array after the memset() is \n");
    printArrayvalues(arrayValues, num);
    return 0;
}

Output:

How to initialize array to zero in C

We also have that facility in which initialling the array to values other than 0 :

With gcc, initialising an array to something other than 0 looks like this:

int theArrayValues[2022] = { [ 0 ... 2021 ] = -1 };

By leaving off the dimension, an array can have every element directly populated. Here is the declaration:

Int theArrayValues[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };

Only the outermost dimension may be skipped for multidimensional arrays because the compiler will infer the dimension from the initializer list.

int thePoint[][3] = { { 9, 8, 7 }, { 6, 5, 4 }, { 3, 2, 1} };

Related Topics

Bigint (BIG INTEGERS) in C with Example

The maximum number of digits that a long, long int can have in C/C++ is 20. The issue is how to store the 22-digit number, which is difficult to do...

9 minutes read.

Command line arguments in C

Command line arguments in C The arguments that are generally passed from the line of command are referred to as command line arguments. These command line arguments are always handled by...

3 minutes read.

Union Program in C

 How should a union be defined?  In C programming, a Union is a method used to organize different data types into groups in the same way that Structures are used. We...

4 minutes read.

GCD program of two numbers in C

GCD stands for Greatest Common Divisor, which is also known as Highest Common Factor, which can be abbreviated as HCF. Mathematically it can be defined as any two positive integers...

3 minutes read.

fork() in C

Introduction To create a new function in the system, there is a need for a system call, i.e. fork system call. It is also known as the child process. These child...

2 minutes read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.

Two-Dimensional array in C

What is an Array? The collection of a fixed number of elements of homogeneous data types that are stored in contiguous locations in the memory is known as an array. Only...

6 minutes read.

Typedef vs define in C

Typedef VS define in C Typedef In the C programming language, a keyword called typedef can be used to give a type a new name. In other words, it is used to...

5 minutes read.

Results of Comparison Operations in C and C++

In this tutorial, we will explore comparison operators and how the system should compare an incoming value supplied as a context parameter to a given value or range of values. A...

2 minutes read.

Pointer to pointer in C

A pointer to another pointer is another type of multiple indirections and a chain of many pointers. Generally, a pointer consists of the address of the variable. Once a pointer to...

4 minutes read.

C Program for Mean and Median of an Unsorted Array

In this tutorial, we will look at how to determine the mean and median of a given unsorted array. To determine the Mean: To get the average, mean is determined. The formula...

2 minutes read.

Positioning of file in C

Positioning() function in c The fseek() function is utilized to change the document position of the stream . The fully extent of value from the source should be one of the...

3 minutes read.

Array Example in C

An array is a collection of similar types of data elements arranged in such a way that any number of values can be assigned to it. It can store values that...

4 minutes read.

Difference between while and do-while loop in C

Introduction Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes. Both loops process the program/code in the...

3 minutes read.

Do WHILE LOOP in C Programming Examples

Before understanding the programming examples of the Do-While loop, we have to know what is Do-While loop in C. So, let's start with the definition of the Do-While loop. What is...

5 minutes read.

Memory leak in C

What is memory leak in C? Memory leak occurs when we keep allocating memory in the heap without freeing it, i.e., the allocated memory in heap is not released back to...

3 minutes read.

Reverse a Stack using Recursion in C

In this tutorial we will learn how recursion will be used in this case to reverse a stack. For loops, while loops, do-while loops, and similar constructions are not permitted....

5 minutes read.

How to open a C file on android mobile

A C file is a file that has a .c extension and contains code written in the C language. C is a computer programming language developed by Dennis Ritchie at...

4 minutes read.

Armstrong program in C using function

In this article we will learn how to find Armstrong of a number using function in C. An Armstrong number is a three-digit number that is the sum of its separate...

1 minute read.