Javatpoint Logo
Javatpoint Logo

Add Element in Array in C

An array is a data structure that stores a collection of items in a single location in memory. It is important because it allows for efficient storage and retrieval of data, making it a common building block for many algorithms and data structures. Arrays are used in a wide variety of applications including:

Scientific Simulations and Numerical analysis: arrays are used to store large amounts of data generated by simulations and used for analyzing and visualizing the results.

Game Development: arrays are used to store game elements such as characters, objects, and level layouts.

Database Management Systems: arrays are used to store data in table format and used for retrieving data based on specific conditions.

Graphics and Image processing: arrays are used to represent images as a 2D grid of pixels and used for applying image processing algorithms.

Compilers and Interpreters: arrays are used to store symbol tables and intermediate code.

Operating Systems: arrays are used to store system data such as process and memory management tables.

Artificial Intelligence and Machine Learning: arrays are used to store large amounts of training data used to train models and make predictions.

Overall, arrays are a fundamental data structure that is widely used in computer science and software engineering. They provide an efficient way to store and access data, making them essential for many algorithms and applications.

C Code

Output

myArray[0] = 0
myArray[1] = 1
myArray[2] = 2
myArray[3] = 3
myArray[4] = 4
myArray[5] = 5
myArray[6] = 6
myArray[7] = 7
myArray[8] = 8
myArray[9] = 9

Explanation:

The above code is a simple C program that demonstrates how to create and initialize an array in C. The program starts by including the stdio.h header file, which contains the declaration for the printf function used later in the program. The main function starts by declaring two variables: i and myArray. The i variable is used as a counter in the for loops, while the myArray variable is the array itself. The array is declared with a size of 1000, which means it can store up to 1000 integers.

The first for loop, using the variable i starts at 0 and runs until i is less than 1000. In each iteration of the loop, the value of i is assigned to the corresponding element of the array. This initializes the array with the values 0 to 999.

Add Element in Array in C

In C, there are several ways to add an element to an array. Here are a few examples:

Using a for loop: You can use a for loop to iterate through the array and add the new element to the next available position in the array. For example, you can use a variable to keep track of the current size of the array and add the new element to the next position after the last occupied position.

C Code

Output

5 0 0 0 0 0 0 0 0 0

Using the Memcpy function: The memcpy function can be used to copy a block of memory, including an array. You can use this function to create a new array that is one element larger than the original array and copy the elements from the original array to the new array, then add the new element to the last position of the new array.

C Code

Output

0 0 0 0 0 0 0 0 0 0 5

Next TopicAdd String in C





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA