Utilizor
Contact Us

CC Arrays

Storing multiple values.

C Arrays

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

To create an array, define the data type (like int) and specify the name of the array followed by square brackets [].

To insert values to it, use a comma-separated list, inside curly braces:

int myNumbers[] = {25, 50, 75, 100};

Examples

Access Array Elements

c example

Accessing the first element (index 0).

#include <stdio.h>

int main() {
  int myNumbers[] = {25, 50, 75, 100};
  printf("%d", myNumbers[0]);
  return 0;
}

Change an Array Element

c example

Modifying values.

#include <stdio.h>

int main() {
  int myNumbers[] = {25, 50, 75, 100};
  myNumbers[0] = 33;
  printf("%d", myNumbers[0]);
  return 0;
}

Loop Through an Array

c example

Iterating through elements.

#include <stdio.h>

int main() {
  int myNumbers[] = {25, 50, 75, 100};
  int i;
  
  for (i = 0; i < 4; i++) {
    printf("%d\n", myNumbers[i]);
  }
  return 0;
}

Test Your Knowledge

1. What is the index of the first element in an array?

2. How do you access the second element of array 'arr'?