1 Dimensional Array in C
1D Array
1D Array is defined as the continuous block of memory in which a similar data type is stored. 1D Array indexing starts with size 0 and ends with size -1. The size of the 1D array is fixed and it is stored in static memory.
Syntax:
[arr_size]={value 1, value 2, value 3,…,value n-1};
Representation of 1D Array
Array Declaration
Declaration of 1D Array consists of any data type and it can be declared by any random name or any random variable.
Syntax:
int arr[10];
// Here int is the data type of array.
// arr is the name of array.
// 10 is the size of array.
Array Initialization
In 1D Array, All the elements consist of garbage value at initial time but it can be explicitly initialized during declaration.
Syntax:
[arr_size]={value 1, value 2, value 3,…,value n-1};
Accessing of elements in 1D Array:
In 1D Array elements are accessed by putting the array name and index value inside square brackets.
Syntax:
<arr_name>[index];
Rules for 1D Array Declaration:
- In 1D array we must have to declare the array variable.
- 1D Array stores the data in contiguous memory locations.
- 1D Array should be a list of similar data types.
- The size of the 1D array should be fixed.
- 1D Array should be stored in static memory.
- 1D Array indexing starts with size 0 and ends with size -1.
- In 1D Array declaration of array size within the bracket is not necessary.
- In 1D Array it must include the variable name and data type while declaring any arrays .
Example 1
#include <stdio.h> int main() { int arr[5] = {1,3,5,7,9}; printf("The elements of arrays are :"); for(int i=0; i<5; i++){ printf(" %d",arr[i]); } return 0; }
Output:
The elements of arrays are : 1 3 5 7 9
Example 2
#include <stdio.h> int main() { int arr[12] = {1,3,5,7,9,10,12,14,23,22,33,35}; printf("The even elements in arrays are :"); for(int i=0; i<12; i++){ if(arr[i]%2==0) printf(" %d",arr[i]); } return 0; }
Output
The even elements in arrays are : 10 12 14 22
Example 3
#include int main() { int arr[11] = {3,5,7,9,10,12,14,23,22,33,35}; for(int i=0; i<11; i++){ if(i==0){ printf("First element of array : %d\n",arr[i]); } else if(i==4) { printf("Fifth element of array : %d\n", arr[i]); } else if(i==10){ printf("Last element of the array : %d", arr[i]); } } return 0; }
Output
First element of array : 3 Fifth element of array : 10 Last element of the array : 35
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Dear PrepInsta Team,
I appreciate your informative videos. Could you please provide notes with the same details as in the videos? This would enhance our learning experience and serve as a valuable reference. Thank you for considering this request
Hey Nilima! Thank you so much for your feedback. We will look into it.