1D Array With a Example C Program

Subject: PPS (Programming for Problem Solving)

Contributed By: Sanjay

Created At: February 3, 2025

Question:


Explain One-Dimensional (1D) Array Array With a Example C Program

Explanation Video:

Custom Image

Explanation:

One-Dimensional (1D) Array

what is an Array?
An array is a collection of elements of the same data type, stored in contiguous memory locations. It allows storing multiple values under a single variable name, reducing the need for multiple separate variables.

What is 1D Array?

A 1D array is a linear collection of elements, similar to a list. It is used when we need to store multiple values but in a single row or single sequence.

Declaration and Initialization:

  • Declaration tells the compiler to allocate memory for the array.
  • Initialization assigns values to the array.

Syntax:

data_type array_name[size];  // Declaration

data_type array_name[size] = {value1, value2, ...}; // Initialization

Key Points:

 All elements are stored contiguously in memory.
 Elements can be accessed using indexing, starting from 0.

 Can be used to store marks of students, temperatures, or any sequential data.

Source Code:
#include <stdio.h>

int main() {
    int arr[5] = {10, 20, 30, 40, 50};  // Storing 5 integers

    // Accessing and printing array elements
    for(int i = 0; i < 5; i++) {
        printf("arr[%d] = %d\n", i, arr[i]);
    }

    return 0;
}
Output:
arr[0] = 10
arr[1] = 20
arr[2] = 30
arr[3] = 40
arr[4] = 50
Share this Article & Support Us:
Status
printf('Loading...');