In this tutorial section we will learn how to declare 1D array in C Language?.
Let Start to Learn how to Declarare different types of array in C Language.
a. Integer Array.
b. Float Array.
c. Character Array.
Following is the Sysntax to Declare different type of one dimensional array in C language.
Syntax:
data_type arr_name[array_size];
We need to keep in mind the following rules while array declarations.
i. while Declaring an array they must have a data type(int, float, char, double, etc.), variable name, and subscript [].
ii. An array variable must be declared before being used in a program.
iii. The Size of the array is represented by the subscript [].
iv. If the size of a array is declared as 10, then programmers can store 10 elements.
v. Each array element in array is stored in a separate memory location.
vi. The index of an array in C language is always start from 0.
For example, if an array variable is declared as int n[10], then it ranges from 0 to 9.
One Dimensional (1D) Array Declaration in C Language
Example: Array Declaration in C Programming
Example: int N[10];
The above declaration defines an array called N of size 10. This means N can store 10 integer values in contiguous memory locations. All elements in the array are of the same data type (int).
The elements occupy consecutive memory locations, forming an ordered set of elements. Each element can be identified by its position or index in the array, also referred to as the subscript.
The first element is at index position 0, and the nth element can be accessed at the (n - 1)th index position.
The name of the array, N, represents the address of the first element (i.e., &N[0]) in the array.
Declaring Single or One-Dimensional Arrays in C Programming
The following examples demonstrate how to declare 1D arrays of type char, float, and int in C programming.
In C, an array must be declared like any other variable before it is used in the program.
Syntax of Array Declaration:
data_type array_name[size];
i. Character Array Declaration
char a[5];
Here, char is the data type, a is the name of the array, and 5 is its size.
The name a is followed by a pair of square brackets [] containing the array size. This array can hold 5 characters. The semicolon ; ends the statement.
ii. Float Array Declaration
float salary[5];
float is the data type, salary is the array name, and the size is 5. The array salary can store 5 floating-point numbers, e.g., 4569.98, 4567.90, etc.
iii. Integer Array Declaration
int srno[5];
int is the data type, srno is the name of the array, and the size is 5. This means the srno array can store 5 integer elements.
iv. Double Array Declaration
double balance[5];
Here, double is the data type and balance is the array name. This array can hold up to 5 double-precision numbers.
1. Array of Pointers to Characters
extern char *name[];
The declaration above defines an array of pointers to char. Each element of the array name points to a character or a string.
2. Array of Structures
struct {
float salary, bonus;
} employee[10];
This declares an array of structures. The array employee[10] has 10 elements, and each element is a structure containing two members: salary and bonus.
/* Character array Declaration and initialization program */
#include<stdio.h>
#include<string.h>
int main()
{
char vowels[] = {'a','e','i','o','u'};
int c = puts(vowels); // print array
if(c > 0){
printf("\nVowels printed successfully");
}
printf("\nLength of character array is = %d", strlen(vowels));
return(0);
}
Output:
aeiou
Vowels printed successfully
Length of character array is = 5
1. The puts() function is used to print a character string or array to the output.
Its prototype is available in the string.h header file.
2. The strlen() function is also present in string.h and is used to find the length of a string.
Common Questions About Declaring 1D Arrays in C
1. What does declaring a 1D array in C mean?
Declaring a 1D array in C means reserving a fixed block of memory that can store multiple values of the same data type. For example, when you write int n[10];, the compiler creates space for 10 integer values.
2. Why do we need to specify the array size during declaration?
The array size tells the compiler how many elements the array can store. Based on this size, memory is allocated. For example, float salary[5]; can store exactly five float values.
3. Why does array indexing start from 0 in C?
In C, array indexing starts from 0 because the first element is stored at the starting memory location of the array. So an array of size 5 has indexes from 0 to 4.
4. Can a 1D array store different data types?
No, a one dimensional array can store only one type of data. For example, an integer array stores only integers, and a character array stores only characters.
5. What happens if we access an index outside the array size?
Accessing an index outside the declared size may lead to incorrect output or runtime errors. That is why it is important to stay within valid index limits.
Previous Topic:-->> 1D Array in C || Next topic:-->>Initialize 1D Array in C.
Other Topics:
while oop do while loop for loop