C Array

Array definition and declaration in C.

Array

Array is a collection of elements of the same data type arranged in contiguous memory locations. Each element in the array is accessed by its index or position within the array. Arrays in C are of fixed size, meaning that once you define an array with a certain size, you cannot change its size during runtime. Here's how you declare and define an array in C:

Declaration:

To declare an array, you specify its data type, followed by the array name, and the number of elements it can hold in square brackets ('[]'). This is called the array's size or dimension.

int myArray[5]

* Declares an integer array with 5 elements.

Initialization:

You can also initialize an array at the time of declaration by providing a list of values enclosed in curly braces ('{}'). The number of elements in the initialization list should match the size of the array.

int myArray[5] = {10, 20, 30, 40, 50};

* Initializes myArray with specific values.

If you omit the size of the array during initialization, the compiler will automatically determine the size based on the number of elements provided.

int myArray[] = {10, 20, 30, 40, 50};

* The size is automatically determined as 5

Accessing Elements:

You can access individual elements of the array using square brackets and the index of the element. Remember that array indices start at 0.

int myArray[] = {10, 20, 30, 40, 50};

* Accesses the third element (30) of myArray

Multidimensional Arrays:

You can also create multidimensional arrays in C, which are essentially arrays of arrays. For example, a 2D array can be declared and defined as follows:

int twoDArray[3][4];

* Declares a 2D integer array with 3 rows and 4 columns

Syntax:

The syntax for declaring and using arrays in C is as follows:

data-type array-name [size];

data-type: This specifies the data type of the elements that the array will hold. It can be any valid C data type, such as int, char, float, etc.

array-name: This is the name you give to the array, which you'll use to refer to it in your code.

size: This indicates the number of elements the array can hold. It must be a non-negative integer.

Example:

Here's an example of declaring and using arrays in C:

c Copy Code
#include <stdio.h>

int main() {

  int data[5];
  printf("Enter 5 numbers: \n");

  for(int i = 0; i < 5; ++i) {
     scanf("%d", &data[i]); // Storing the numbers
  }

  printf("\nStored numbers: \n");

  for(int i = 0; i < 5; ++i) {
     printf("%d\n", data[i]);// Printing the numbers
  }
  return 0;
}

It's also important to learn how to handle character arrays using null ('\0') characters.

Output:
Enter 5 numbers: 
-5
-100
10
25
50

Stored numbers:
-5
-100
10
25
50

In this example, we employ a 'for' loop to gather 5 user inputs, which are subsequently stored within an array. Following this, a separate 'for' loop is employed to present these collected elements on the screen.

Note: Remember that C does not perform boundary checks on array indices, so it's important to ensure that you do not access elements outside the bounds of the array to avoid undefined behavior and potential memory corruption.

What's Next?

We've now entered the finance section on this platform, where you can enhance your financial literacy.