![]() |
| 1-D Array |One Dimensional Array | in C Complete Beginner Guide |
One Dimensional Array
1-D Array in C: A Array is a collection of data of same data types. It can store data of same data types means an integer array can store integer value and character array can store character value. Array can be used to group of data simultaneously. We can not fetch data from array value directly therefore we can use index point value.
The indexing of array always start with 0 and index value is always integer value. Arrays is allow to store multiple values of the same data type in a single variable. We do not creating separate variables like student1, student2, student3, you can create one array to hold all students.
Syntax of One Dimensional Array in C
- Data_type: The array data type of data that the array will hold (int, float, char, etc.).
- Array_name: The name you choose for the array.
- Array_size: The number of elements the array can hold.
int a[5]={10.40,60,80,70}; value=10,40,60,80,70; index=a[0][1][2][3][4][5]; a[0]=10 a[1]=40 a[2]=60 a[3]=80 a[4]=70 |
Array Initialization in C
Array is the are several ways to initialize arrays. The choice of initialization method depends on the requirements of your C program. Here are some common methods for initializing arrays in C.
Syntax for Array Initialization at Declaration
- Array_Data_Type: The type of data that the array will hold (integer, float, character etc.).
- Array_Name: The name you choose for the array.
- Array_Size: The number of elements the array can hold. {data1, data2,data3 ... dataN}:
value=10,40,60,80,70; index=a[0][1][2][3][4][5]; a[0]=10 a[1]=40 a[2]=60 a[3]=80 a[4]=70 |
The values to initialize the array with the number of elements inside the curly braces must match the size of the array.
We declare and initialize an integer array numbers with a size of 5 and we provide five values. If you don't specify all the values during initialization. C will automatically initialize the remaining elements to zero or garbage for numeric types (integer, float, character) or null characters for character arrays.
Example of Array in C Programming
| #include<stdio.h> void main(){ clrscr(); int a[5]={10,40,60,80,70}; printf("Value of a[0]:%d\n",a[0]); printf("Value of a[1]:%d\n",a[1]); printf("Value of a[2]:%d\n",a[2]); printf("Value of a[3]:%d",a[3]); printf("Value of a[4]:%d",a[4]); getch(); } ============OUTPUT============ Value of a[0]:10 Value of a[1]:40 Value of a[2]:60 Value of a[3]:80 Value of a[4]:70 |
Compile Time Array Initialization in C
- This is typically done using constant values or expressions that can be evaluated at compile time.
- Arrays initialized at compile time are often referred to as "static" or "constant" arrays because their values cannot change during program execution.
- Compile-time initialized arrays are placed in the program's data segment or read-only memory, depending on whether they are marked as const.
Print Array Element Using For Loop
| #include<stdio.h> void main(){ clrscr(); int a[5]={10,40,60,80,70}; for(int i=0;i<5;i++) { printf("Value of a[%d]:%d\n",i,a[i]); } getch(); } ============OUTPUT============ Value of a[0]:10 Value of a[1]:40 Value of a[2]:60 Value of a[3]:80 Value of a[4]:70 |
Array Length in C Programming
How To Get Array Length in C
| #include<stdio.h> void main(){ clrscr(); int a[10] = {20, 48, 87, 56, 19,10,89}; int array_size = sizeof(a)/sizeof(a[0]); printf("Total Array_Size:%d", array_size); getch(); } ============OUTPUT============ Total Array_Size:10 |
Runtime Initialization of Array in C
How to Print User input Array in C
| #include<stdio.h> void main(){ clrscr(); int a[10],n,i; printf("Enter Array Size:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter Array Element[%d]:",i); scanf("%d",&a[i]); } for(i=0;i<n;i++) { printf("Value of a[%d]:%d\n",i,a[i]); } getch(); } ============OUTPUT============ Enter Array Size:5 Enter Array Element[0]:67 Enter Array Element[1]:86 Enter Array Element[2]:45 Enter Array Element[3]:89 Enter Array Element[4]:43 Value of a[0]:67 Value of a[1]:86 Value of a[2]:45 Value of a[3]:89 Value of a[4]:43 |
Modifying Array Elements in C
How To Get Array Length in C
| #include<stdio.h> void main(){ clrscr(); int i,a[5] = {20, 48, 87, 56, 19}; for(i=0;i<5;i++){ printf("Before Array Element[%d]:%d\n", i,a[i]); } a[2]=100; for(i=0;i<5;i++){ printf("After Change Array Element[%d]:%d\n", i,a[i]); } getch(); } ============OUTPUT============ Before Array Element[0]:20 Before Array Element[1]:48 Before Array Element[2]:87 Before Array Element[3]:56 Before Array Element[4]:19 After Change Array Element[0]:20 After Change Array Element[1]:48 After Change Array Element[2]:100 After Change Array Element[3]:56 After Change Array Element[4]:19 |

