We have already seen that every variable must be declared before it is used. The same concept is true in case of array variables also. An array must be declared before being used
DECLARATION OF ARRAYS
We
have already seen that every variable must be declared before it is used. The
same concept is true in case of array variables also. An array must be declared
before being used. Declaring an array means specifying three things:
•
Data type-what kind of values it can store, for example bsint, char, float,
double
•
Name-to identify the array
•
Size-the maximum number of values that the array can hold
Arrays
are declared using the following syntax:
type name [size];
Programming Tip:
To declare and define an array, you must specify its name, type, and size.
Here
the type can be either int, float,
double, char or any other valid data type. The number within brackets
indicates the size of the array, i.e., the maximum number of elements that can
be stored in the array. The size of the array is a constant and must have a
value at compilation time. For example, if we write,
int marks [10];
The
above statement declares marks to be an array containing 10 elements. In C, the
array index (also known as subscript) starts from zero. This means that the
array marks will contain 10 elements in all. The first element will be stored
in marks [0], the second element in marks [1], and so on. Therefore, the last
element, i.e., the 10th element will be stored in marks [9]. Note that 0, 1, 2, 3 written within square
brackets are subscripts/index. In memory, the array will be stored as shown in
Figure 5.2.
Points to
Remember
Note
that C does not allow declaring an array whose number of elements is not known
at the compile time. Therefore, the following array declarations are illegal in
C.

int arr[];
int n, arr [n];
•
Generally it is a good programming practice to define the size of an array as a
symbolic constant as shown in the following code.
#include <stdio.h>
#define N 100
main()
{
int arr [N];
………….
}
The
size of the array can be specified using an expression. However, the components
of the expression must be available for evaluation of the expression when the
program is compiled. Therefore, the following array declarations are valid in C
language.
#include <stdio.h>
#define N 100
main()
{
int i=10;
int arr [N+10], my_arr [i-5*10];
……………
}
Note
Carray
indices start from 0. So for an array with N elements, the index of the last
element is N − 1.
•
C never checks the validity of the array index- neither at compile time nor at
run time. So even if you declare an array as
int arr [N];
The
C compiler will not raise any error but the result of running such code is
totally unpredictable. Even if you declare an array of 10 elements and later on
by mistake try to access the 11th element, no error will be generated. But the
results will be unpredictable as the memory occupied by the (so-called) 11th
element may be storing data of another object.
Programming in C: Unit II (a): Arrays : Tag: : with Example C Programs - Declaration of Array in C
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation
Professional English II
HS3251 2nd Semester 2021 Regulation | 2nd Semester Common to all Dept 2021 Regulation
Statistics and Numerical Methods
MA3251 2nd Semester 2021 Regulation M2 Engineering Mathematics 2 | 2nd Semester Common to all Dept 2021 Regulation
Engineering Graphics
GE3251 eg 2nd semester | 2021 Regulation | 2nd Semester Common to all Dept 2021 Regulation
Physics for Electrical Engineering
PH3202 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation
Basic Civil and Mechanical Engineering
BE3255 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation
Electric Circuit Analysis
EE3251 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation
Physics for Electronics Engineering
PH3254 - Physics II - 2nd Semester - ECE Department - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation
Electrical and Instrumentation Engineering
BE3254 - 2nd Semester - ECE Dept - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation
Circuit Analysis
EC3251 - 2nd Semester - ECE Dept - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation
Materials Science
PH3251 2nd semester Mechanical Dept | 2021 Regulation | 2nd Semester Mechanical Dept 2021 Regulation
Basic Electrical and Electronics Engineering
BE3251 2nd semester Mechanical Dept | 2021 Regulation | 2nd Semester Mechanical Dept 2021 Regulation
Physics for Civil Engineering
PH3201 2021 Regulation | 2nd Semester Civil Dept 2021 Regulation
Basic Electrical, Electronics and Instrumentation Engineering
BE3252 2021 Regulation | 2nd Semester Civil Dept 2021 Regulation
Physics for Information Science
PH3256 2nd Semester CSE Dept | 2021 Regulation | 2nd Semester CSE Dept 2021 Regulation
Basic Electrical and Electronics Engineering
BE3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation