Simple Breakdown of the Array Concept in C

Simple Breakdown of the Array Concept in C

Understand the Basics of an Array as a C developer

Is there a way values or items can be grouped together under a name in the C language?

Yes!!! That concept is called Arrays.

In this article, I'll be explaining to you what I understand about arrays in C so you can grasp this concept if you haven't.

What are Arrays?

Arrays allows items or values of the same datatype to be grouped together with a single name(variable name).

Arrays can store a fixed number of items of the same datatype. If you want to store 50 elements in your array - just indicate it.

Array diagram size.png

Array Syntax in C - Array Declaration

How are arrays declared in C so the compiler will compile successfully?

Here's how to declare arrays as a local variable in the main function.

datatype variableName[array_size];

NOTE: The size and datatype of an array can't be changed or altered after declaration. Example

    int scores[5];
    float weight[4];
#include <stdio.h>

int main()
{
    int scores[5];
    float weight[4];

    return (0);
}

From the above examples, you can deduce that the array scores is of the int datatype with 5 elements

Can you explain the array weight?

So, what's the datatype of the array and the array size for weight?

Types of Arrays in C

There are two types of arrays in C - They're

One-dimensional arrays: Examples given above or previous examples are all one-dimensional arrays.

Multi-dimensional arrays: These type of arrays consists of rows and columns. Typical example of multi-dimensional arrays are Matrices which will be discussed in later articles.

Initializing an Array

During the declaration of an array, the array can also be initialized with values. The size of the array can also be indicated. If the size wasn't indicated by you in the array, the compiler will assign it during compilation.

int weight[5] = {45,34,56,32,40};  //5 elements in the array
or
int weight[] = {45,34,56,32,40};  //5 elements in the array

Note: No array size was indicated in 2nd example but the compiler knows the size = 5

Accessing the Elements/Items in the Array

Items in an array can be accessed through it's index. The index of elements starts from zero(0).

#include <stdio.h>

int main()
{
    int weight[5] = {45,34,56,32,40};

    printf("%d\n",weight[3]);
    printf("%d",weight[0]);
}

Changing or Replacing the Values in an Array

Here's an example on how you can change or replace a value in an array...

#include <stdio.h>

int main()
{
    int weight[5] = {45,34,56,32,40};
    weight[3] = 4;
    weight[0] = 20;

    printf("%d\n",weight[3]);
    printf("%d",weight[0]);
}
//The 1st and 3rd elements have been changed in the above example

Printing(input/output) the items in an Array

A common way of printing the items on the terminal or output of an array is through the *for loop

The keyword scanf is used to take input from the user and prints the stored input of the user on the terminal.

Let's access the elements in weight

#include <stido.h>

//print the elements in the array

int main(void)
{
    int values[5] = {45,5,7,34,5};

    printf("Here are the 5 integers displayed below\n");

    for(int i = 0; i<5; ++i)
    {
        printf("The %d integer is %d\n",(i+1), values[i]);
    }

    return(0);
}

##ouput

Here are the 5 integers displayed below
The 1 integer is 45
The 2 integer is 5
The 3 integer is 7
The 4 integer is 34
The 5 integer is 5

##Taking Input from the User and Printing the Values

#include <stdio.h>

//print the elements stored from the input in the array

int main(void)
{
    int values[5];

    printf("Enter your 5 integers below\n>>> ");

    //take the input and store them
    for(int i = 0; i<5; ++i)
    {
        printf("Enter the %i number\n>>> ",i+1);
        scanf("%d",&values[i]);
    }


    for(int k = 0; k<5; ++k)
        printf("value %d is %d\n",k+1,values[k]);

    return (0);
}



##output

Enter your 5 integers below
>>> Enter the 1 number
>>> 3
Enter the 2 number
>>> 4
Enter the 3 number
>>> 5
Enter the 4 number
>>> 6
Enter the 5 number
>>> 7
value 1 is 3
value 2 is 4
value 3 is 5
value 4 is 6
value 5 is 7

Conclusion

Array is a concept every C developer must understand to carry ou tasks effectively. The enum datatype can also be used with arrays.

Arrays can be declared with the variable and datatype attached ;also they can be initialized with or without the array size. Expects later articles on a coding challenge on multidimensional arrays.

Learn. Build. Grow.

More C concepts on the way!!!