One Dimensional Array in C with Examples

What is a One-Dimensional Array?

A one-dimensional array is a linear collection of elements, all of the same data type, stored in contiguous memory locations. Each element is accessed using a single index, starting from 0.​

For example, if you want to store the marks of 5 students, instead of declaring five different variables like this:

int mark1, mark2, mark3, mark4, mark5;

You can simply use an array:

int marks[5];

Now, marks[0] represents the first student’s mark, marks[1] the second, and so on.

Syntax of a One-Dimensional Array

datatype array_name[size];
  • datatype: The type of data you want to store (e.g., int, float, char).
  • array_name: The name you give to the array.
  • size: Number of elements the array can hold.

Example:

advertisement
int numbers[8]; // Array of 8 integers

Initializing a One-Dimensional Array

1. Static Initialization

You can initialize the array at the time of declaration:

int scores[5] = {85, 90, 75, 60, 95};

If you provide fewer elements than the size, the remaining ones are automatically initialized to 0:

int scores[5] = {85, 90}; // Remaining elements are initialized to 0

If you don’t specify the size, C will count the elements for you:

int scores[] = {85, 90, 75, 60, 95}; // Size inferred as 5

2. Dynamic Initialization

You can also assign values later using loops or directly:

int i, arr[3];
arr[0] = 5;
arr[1] = 10;
arr[2] = 15;

Accessing and Modifying Array Elements

Elements in an array are accessed using their index. Remember, indexing starts at 0.​

Example:​

advertisement
#include <stdio.h>
 
int main()
{
    int scores[5] = {85, 90, 75, 60, 95};
    printf("First score: %d\n", scores[0]); // Outputs 85
    scores[2] = 80; // Modifies the third element
    printf("Updated third score: %d\n", scores[2]); // Outputs 80
    return 0;
}

This C program demonstrates how to work with arrays. It starts by declaring an array named scores with five integer values. The program then prints the first score in the array, which is 85. After that, it updates the third element (index 2) of the array from 75 to 80. Finally, it prints the updated third score. This example shows how to access and modify array elements using their index positions.

Traversing a One-Dimensional Array

Looping through an array allows processing each element.​

Example:​

#include <stdio.h>
 
int main() {
    int scores[5] = {85, 90, 75, 60, 95};
    for(int i = 0; i < 5; i++) {
        printf("Score %d: %d\n", i + 1, scores[i]);
    }
    return 0;
}

This C program prints all values in an array using a loop. It stores five scores in an array called scores. A for loop goes through each element and prints it. The loop adds 1 to the index to show the score number starting from 1. This makes it easy to handle and display multiple values.

Common Operations on One-Dimensional Arrays

1. Finding the Sum and Average

#include <stdio.h>
 
int main() {
    int scores[5] = {85, 90, 75, 60, 95};
    int sum = 0;
    for(int i = 0; i < 5; i++) {
        sum += scores[i];
    }
    float average = sum / 5.0;
    printf("Sum: %d, Average: %.2f\n", sum, average);
    return 0;
}

Output:

Sum: 405, Average: 81.00

This C program calculates the sum and average of five test scores stored in an array. It first initializes the array scores with five values. Using a for loop, it adds up all the scores and stores the total in the variable sum. Then, it calculates the average by dividing the sum by 5.0 to ensure a floating-point result. Finally, it prints both the sum and the average.

2. Finding the Maximum Element

#include <stdio.h>
 
int main() {
    int scores[5] = {85, 90, 75, 60, 95};
    int max = scores[0];
    for(int i = 1; i < 5; i++) {
        if(scores[i] > max) {
            max = scores[i];
        }
    }
    printf("Maximum score: %d\n", max);
    return 0;
}

Output:

Maximum score: 95

This C program finds the maximum score from an array of five integers. It first initializes the array scores with values and sets max to the first element. Then, it uses a for loop starting from the second element to compare each score with the current maximum. If a larger value is found, it updates max. Finally, the program prints the highest score.

Limitations of One-Dimensional Arrays

  • Fixed Size: Once declared, the size cannot be changed during runtime.
  • Homogeneous Data: All elements must be of the same data type.
  • No Built-in Bounds Checking: Accessing out-of-bound indices can lead to undefined behavior.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.