Single-Dimensional Arrays in C Programming


In C, an array is a collection of variables of the same data type that are stored in contiguous memory locations. A single-dimensional array is a linear array, also known as a one-dimensional array, that stores a fixed number of elements in a single row. Here is an example of how to declare and initialize a single-dimensional array in C:

Syntax :
     datetype arrayName [ arraySize ] ;
Example :
     int myArray[5]; // Declaring an array of 5 integers
     int myArray[] = {1, 2, 3, 4, 5}; // Declaring and initializing an array of 5 integers

In the first example, an array of 5 integers named "myArray" is declared. The elements of the array are not initialized and will have garbage values. In the second example, an array of 5 integers named "myArray" is declared and initialized with the values 1, 2, 3, 4, 5.

You can access the elements of the array using the array name followed by the index of the element in square brackets. For example, to access the first element of the array "myArray", you would use the expression myArray[0]. In C, arrays are zero-indexed, so the first element is at index 0, the second element is at index 1, and so on.

Single dimensional arrays are useful in situations where you need to store a fixed number of elements of the same data type and you want to access them using a single variable name and an index. They can be passed as arguments to functions, returned from functions, and used in various control structures such as loops and conditional statements.

  • This program is a simple C program that demonstrates the use of a single-dimensional array. The program starts by declaring an integer array "a" of size 100 and an integer variable "n" which will be used to store the number of elements in the array. It then prompts the user to enter the limit of the array using the printf() and scanf() functions.
  • The program then uses a for loop to iterate from 0 to the value of n-1. In each iteration of the loop, the program prompts the user to enter a value and stores it in the array at the current index using the scanf() function.
  • The program then uses another for loop to iterate from 0 to n-1. In each iteration of the loop, the program prints out the value of the array at the current index using the printf() function. This way the program takes n integers from the user and print them out.
  • Finally, the program returns 0 to indicate that the program has executed successfully.

Overall, this program demonstrates how to declare and use a single-dimensional array in C and how to access the elements of an array using a for loop and an index variable. It also demonstrates the use of scanf and printf function to take input and display output respectively.

Source Code

//Arrays
#include<stdio.h>
int main()
{
    int i,a[100],n;
    printf("\nEnter The Limit : ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("\nEnter The Value : ");
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
    {
        printf("\n%d",a[i]);
 
    }
    return 0;
}
 
To download raw file Click Here

Output

Enter The Limit : 5

Enter The Value : 1

Enter The Value : 2

Enter The Value : 3

Enter The Value : 4

Enter The Value : 5

1
2
3
4
5

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C