One dimensional array in C
0 178
One-dimensional arrays in C provide a powerful tool for storing and manipulating sequential data elements of the same type.
Here's how you can declare, access, and modify elements in a one-dimensional array in C:
Syntax for Declaring a One-Dimensional Array:
datatype arrayName[arraySize];
datatype: Specifies the type of data the array will hold (e.g., int, float, char).
arrayName: The name of the array.
arraySize: The number of elements in the array.
Accessing Elements in a One-Dimensional Array:
You can access individual elements of the array using their index.
Array indexing in C starts from 0.
arrayName[index];
Modifying Elements in a One-Dimensional Array:
To modify an element in the array, simply assign a new value to the desired index.
arrayName[index] = newValue;
Example:
// Program for one dimensionl array in C #include<stdio.h>int main() { // Declaration and Initialization int numbers[5] = {1, 2, 3, 4, 5}; // Accessing and Printing Elements printf("Elements of the array: "); for(int i = 0; i < 5; i++) { printf("%d ", numbers[i]); } // Modifying an Element numbers[2] = 10; printf("\nArray after modification: "); for(int i = 0; i < 5; i++) { printf("%d ", numbers[i]); } return 0; }
Output:
Elements of the array: 1 2 3 4 5 Array after modification: 1 2 10 4 5
In this program:
We access and print all elements of the array.
We modify the element at index 2.
Finally, we print the modified array.
Share:
Comments
Waiting for your comments