Arrays in C Programming

An array stores a fixed number of values of the same type in contiguous memory. Array indexing starts at 0, so an array of five elements uses indexes 0 through 4.

Declare and Initialize an Array Top ↑

int numbers[5];
int values[] = {9, 8, 7, 6, 5};
int scores[5] = {19, 34, 21, 55, 41};
float percentages[4] = {12.4f, 13.2f, 15.6f, 8.3f};

Complete Array Traversal Example Top ↑

#include <stdio.h>
#include <stddef.h>

int main(void) {
    int values[] = {9, 8, 7, 6, 5};
    size_t count = sizeof values / sizeof values[0];

    for (size_t i = 0; i < count; i++) {
        printf("Index %zu = %d\n", i, values[i]);
    }

    return 0;
}

Character Array Top ↑

A C string is a character array terminated by the null character '\0'.

char name[] = "plus2net";

size_t i = 0;
while (name[i] != '\0') {
    printf("%c ", name[i]);
    i++;
}

Output

p l u s 2 n e t

Read Five Integers Top ↑

After the full program above, only the changed input logic is shown here.

int numbers[5];

for (size_t i = 0; i < 5; i++) {
    printf("Enter number %zu: ", i + 1);
    if (scanf("%d", &numbers[i]) != 1) {
        printf("Invalid input.\n");
        return 1;
    }
}

Search for a Value Top ↑

int target = 21;
int found = 0;

for (size_t i = 0; i < count; i++) {
    if (values[i] == target) {
        printf("Found at index %zu\n", i);
        found = 1;
        break;
    }
}

if (!found) {
    printf("Value not found.\n");
}

Sum and Average Top ↑

#include <stdio.h>
#include <stddef.h>

int main(void) {
    float values[] = {3.0f, 6.0f, 2.0f, 8.0f, 10.0f};
    size_t count = sizeof values / sizeof values[0];
    float sum = 0.0f;

    for (size_t i = 0; i < count; i++) {
        sum += values[i];
    }

    printf("Sum = %.2f\n", sum);
    printf("Average = %.2f\n", sum / (float)count);
    return 0;
}

Highest and Lowest Values Top ↑

#include <stdio.h>
#include <stddef.h>

int main(void) {
    int values[] = {12, 14, 11, 15, 11, 10};
    size_t count = sizeof values / sizeof values[0];
    int highest = values[0];
    int lowest = values[0];

    for (size_t i = 1; i < count; i++) {
        if (values[i] > highest) {
            highest = values[i];
        }
        if (values[i] < lowest) {
            lowest = values[i];
        }
    }

    printf("Highest = %d\n", highest);
    printf("Lowest = %d\n", lowest);
    return 0;
}
Important: never access an index outside the declared array bounds. If an array has five elements, valid indexes are 0 through 4.

Practice with Arrays Top ↑

  1. Read values into an array.
  2. Display all elements.
  3. Display only even values.
  4. Find the sum.
  5. Find the highest and lowest values.
  6. Display the array in reverse order.
  7. Find duplicates and unique elements.
  8. Copy one array to another.
  9. Sort ascending and descending.
  10. Count the frequency of each value.

Array practice solutions → Two-dimensional arrays

Preserved Example: Traverse a Floating-Point Array Top ↑

#include <stdio.h>

int main(void) {
    float percentages[] = {12.4f, 13.2f, 15.6f, 8.3f};
    size_t count = sizeof percentages / sizeof percentages[0];

    for (size_t i = 0; i < count; i++) {
        printf("Index %zu = %.1f\n", i, percentages[i]);
    }

    return 0;
}

Preserved Example: Character Array with an Explicit Null Terminator Top ↑

#include <stdio.h>

int main(void) {
    char name[] = {'p','l','u','s','2','n','e','t','\\0'};
    size_t i = 0;

    while (name[i] != '\\0') {
        printf("%c ", name[i]);
        i++;
    }

    putchar('\\n');
    return 0;
}




plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer