×

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

What are linker and loader in C

Linker and loader are utility programs that have a significant role in executing a program. Linker: A linker is a program that joins the object files produced by the assembler/ compiler...

3 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

What is a buffer in C?

A buffer is an area of memory set aside for the temporary storage of data. A data buffer (or just buffer) is a region of a physical memory storage used to...

5 minutes read.

Distance Vector Routing Protocol Program in c

A distance-vector routing protocol is one of the foremost instructions of routing protocols in pc conversation principle for packet-switched networks. The hyperlink-nation protocol is the alternative foremost class.The Bellman-Ford set...

4 minutes read.

Pointer in C

Pointer is a variable that is used to contain the address of another variable. We can easily create a pointer in C language. Example: int *ptr Symbol Description & (ampersand sign) Address of an operator...

1 minute read.

How to Calculate Time Complexity in C?

What is time complexity? An algorithm's time complexity measures how long it takes to complete a task in relation to the size of the input. It should be noted that the...

5 minutes read.

Pre-increment and Post-increment in C

The rich set of operators is supported by the c language. In c there are several operators used. The mathematical operations in the programming language are done with the operators...

4 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

#error #pragma in C

#error, #pragma in C #error, also known as the error directive in the C language. It will not allow you to make any compilation fail and immediately issues a statement which...

4 minutes read.

Queue implementation in C

In the C programming language, the queue is the abstract concept that is similar to stacks. However, it is the part of the data structures that work opposite to that...

4 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.

Flow chart of While loop in C

This is a flowchart that represents the process of executing the while loop in the C programming language.Generally, as we know there are three main components of while loop:1. The...

3 minutes read.

Enumeration (Enum) in C

Enumeration (Enum) in C: In the C programming language, the enum is referred to as an enumerated type. Enum is a user defined data type consisting of integer values and...

3 minutes read.

Find the Largest Three Distinct Elements in an Array using C/C++

In this tutorial, we will demonstrate how to use a C/C++ programme to locate the highest three different elements in an array. C/C++ Program to Find the Largest Three Different Elements...

3 minutes read.

C/C++ Program to Find the Size of int, float, double and char

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable. Program to Determine Variable Size Write a C or C++ program to determine the...

2 minutes read.

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given...

4 minutes read.

Find out Power without using POW function in C

Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in...

3 minutes read.

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

How to return a Pointer from a Function in C?

In the C programming language, pointers are variables that hold the address memory location of another variable. We could also input pointers to a function and return pointers from a...

3 minutes read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.