C Array

Used to store multiple elements of the same data type under a single identifier.
They can be accessed using an index that starts from 0

Syntax:
data_type array_name[size];

  1. data_type - Declaring the type of data in an array i.e; int, float, char, etc.
  2. array_name - Declaring the name.
  3. size - Declaring the number of elements in an array
Example
#include <stdio.h>

int main() {
    int numbers[5] = {1, 2, 3, 4, 5};

    printf("Element at index 0: %d\n", numbers[0]);
    printf("Element at index 2: %d\n", numbers[2]);

    return 0;
}

See how the array works

  1. int numbers[5] declares 5 the size of the array.
  2. numbers[0] gets the first value in the array.

Modifying an Array

Using the index we can change or modify the values in an array.

Example
#include <stdio.h>

int main() {
    int numbers[5] = {1, 2, 3, 4, 5};

    numbers[1] = 6;
    numbers[2] = numbers[1];
    
    printf(numbers[1]);
    printf(numbers[2]);

    return 0;
}

Using Loops

We can access, modify the array using loops.
Using iterations in for and while loops we can access them through index.

Example
#include <stdio.h>

int main() {
    int numbers[5] = {1, 2, 3, 4, 5};

    for (int i = 0; i < 5; i++) {
        printf("%d ", numbers[i]);
    }

    return 0;
}

Quick Recap - Topics Covered

C Arrays
Modifying an Array
Using loops in an Array

Practice With Examples in Compilers

The Concepts and codes you leart practice in Compilers till you are confident of doing on your own. A Various methods of examples, concepts, codes availble in our websites. Don't know where to start Down some code examples are given for this page topic use the code and compiler.


Example 1
Example 1 Example 2 Example 3 Example 4 Example 5


Quiz


FEEDBACK