C Program to Implement a Single-Dimensional Array

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.

🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.

▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube

1. Introduction

A single-dimensional array is the simplest form of an array that enables you to organize a series of items sequentially. This can be thought of as defining a single line with a number of slots that you can use to store values. In C, the most common type of array is the array of integers. In this blog post, we'll take a look at how to declare, initialize, and access data in a single-dimensional array in C.

2. Program Overview

In this program, we will:

1. Declare a single-dimensional array.

2. Initialize the array with values.

3. Print out each value of the array.

3. Code Program

#include <stdio.h>

int main() {
    int arr[5] = {10, 20, 30, 40, 50};  // Declare and initialize an array of size 5
    int i;  // Loop counter variable

    // Print each value of the array
    for(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

4. Step By Step Explanation

1. We start by declaring and initializing a single-dimensional array arr with 5 integers.

2. We then use a for loop to iterate through each element of the array.

3. Inside the loop, we print the index and the value of the current array element using the printf function.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare