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.
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.
//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
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
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions