- How to declare one dimensional (1D) arrays in C language
- The syntax for declaring integer, float, character, and double arrays
- Important rules to follow when declaring arrays
- Memory allocation and indexing concepts
- Real-world examples of array declarations
Introduction to 1D Array Declaration
In this tutorial section, we will learn how to declare 1D arrays in C Language. Let's start to learn how to declare different types of arrays in C Language:
- a. Integer Array — stores integer values
- b. Float Array — stores floating-point numbers
- c. Character Array — stores characters or strings
- d. Double Array — stores double-precision numbers
Syntax of Array Declaration
data_type array_name[array_size];
Rules for Array Declaration
We need to keep in mind the following rules while declaring arrays:
- While declaring an array, they must have a data type (int, float, char, double, etc.), variable name, and subscript [].
- An array variable must be declared before being used in a program.
- The size of the array is represented by the subscript [].
- If the size of an array is declared as 10, then programmers can store 10 elements.
- Each array element is stored in a separate memory location.
- The index of an array in C language always starts from 0.
📝 Note: If an array variable is declared as int n[10], then it ranges from 0 to 9.
Examples: 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.
More Examples of Array Declarations in C Programming
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.
Example Program: Character Array Declaration and Initialization
/* 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
Explanation:
- The puts() function is used to print a character string or array to the output. Its prototype is available in the
string.hheader file. - The strlen() function is also present in
string.hand 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.
💡 Tip: Always ensure that you declare your array with the correct size and never access elements beyond the declared range.