One Dimensional Array in C
One Dimensional Array in C: In the C programming language, an array is defined as a collection of one or more values of the same type. Every value present in an array is referred to element of the array. The elements of the array share the same variable name but are unique in their own way and also have a different index number which is also called a subscript.
An array can of any data type such as int, float, double, and so on. If an array declared is of the data type int, then all the elements within that array should be of int data type only. This rule is the same with other data types as well.
The array comes into use when a programmer is creating an application to store the roll numbers of 100 students. As we all know, a variable allows us to store only one value at a time, and it cannot be used for cases like this, as declaring many variables for a single entity (student) is not a good idea.
In a situation like this, arrays give a better way to store the data. Arrays can be either a single dimensional array or a multidimensional array. The number of a subscript or the index elements usually determines the dimension of an array.
One dimensional array
In simple words, a one-dimensional array can be thought of as a row where the elements are stored one after the other. It is also defined as an array that has only one subscript that is used to specify the particular element of an array. It is a structured collection of the components that are accessed individually by indicating the position of the elements within a single index value.
Syntax
datatype array_name [size_of_array];
Where;
- datatype: It is the type of data for the elements within an array. It follows the C standard rules of data type.
- array_name:It is used to represent the name of the array and is user defined and must be a valid identifier.
- size_of_array:It indicated the number of the elements of an array that will be able to hold.
E.g.:
int a[100]; /*a is of the array data type integer(int) and can store only 100 elements in it*/ float b[10];/*b is of the array data type float and can store only 10 elements in it*/ char c[20];/*c is of the array data type character(char) and can store 20 elements in it*/
When an array is declared at the beginning, it consists of garbage values.
Initializing an array
The array can be initialized in two situations such as;
- Compile time initialization (static initialization)
- Runtime initialization(dynamic initialization)
Rules for declaring a one-dimensional array
- An array variable must be declared before being used in a program.
- The declaration must have a data type (int, float, char, double, etc.), variable name, and subscript.
- The subscript represents the size of the array. If the size is declared as 10, programmers can store 10 elements.
- Every array index number always begins from 0 such as if an array variable is declared as s[100], then it ranges from 0 to 99.
- Every array element will be stored in a separate memory location.
Compile time initialization
The array declared at the beginning in which the elements will be declared to it during the compile time is written.
Syntax
datatype array_name [array_size] = (list of the elements of an array);
E.g.:
int n[3] = {0, 1, 2}; #include<stdio.h> int main() { int n[10]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; printf("%d", n[0]); printf("%d", n[1]); printf("%d", n[2]); printf("%d", n[3]); printf("%d", n[4]); printf("%d", n[5]); printf("%d", n[6]); printf("%d", n[7]); printf("%d", n[8]); printf("%d", n[9]); }
Output
Runtime initialization of an array
Runtime initialization in the array concept means it can be initialized at the runtime. It means that the array elements are initialized after the compilation of the program.
E.g.:
#include<stdio.h> int main() { int arr[5], i; for(i = 0; i< 5; i++) { printf("Enter a[%d]: ", i); scanf("%d", &arr[i]); } printf("\nPrinting elements of the array: \n\n"); for(i = 0; i< 5; i++) { printf("%d ", arr[i]); } /* signal to operating system program ran fine */ return 0; }
Output
Declaration of an array of integer 5 and variable i, which is of type int. Then ‘a’ loop is used to enter the elements into an array at run time.
In scanf() statements, ‘&’ operator will be used on the element ‘arr[i]’ of an array; hence we have int, similar to float, char, and so on.
The printf() statement prints the elements in the console.