Size of the array has to be constant.
(i) int a[10]; //valid
(ii) const int n=10; //valid
int a[n];
(iii) int n=10; //invalid
int a[n];
(iv)
#include<stdio.h> //valid
#include<conio.h>
#define n 10 //macro definition
int main()
{
int a[n];
statements
return(0);
}
Constant variable:
A constant variable is a variable whose value cannot be changed during the execution of the program. And if we try to change the value, an error will get generated.
A constant is declared using the keyword “const“.
Example:
const int a=10;
const a=10;
A constant has to be initialized at the time of declaration.
(i) const int a=10; //valid
(ii) const int a; //invalid
a=10;
If an array is declared as int a[10] then
a[2] : It represents an individual element ie. Third element.
a :It represents the entire array. i.e. address of the base element i.e. address of the 1st element of the array i.e. address of a[0].
In general in a[i]
a is called as array name or single subscripted variable.
i is called subscript or index.
Initialization of array element
If required the array elements can be initialized at the time of declaration or later.
int a[5];
a[0]=10;
a[1]=-20;
a[2]=30;
a[3]=40;
a[4]=50;
or
int a[5]={10,-20,30,40,50};
10 |
-20 |
30 |
40 |
50 |
A[0] |
A[1] |
A[2] |
A[3] |
A[4] |
Char N[9]={‘C’,’A’,’T’,’A’,’L’,’Y’,’S’,’T’,’\0’};
Char N[9]=”CATALYST”;
‘C’ |
‘A’ |
‘T’ |
‘A’ |
‘L’ |
‘Y’ |
‘S’ |
‘T’ |
‘\0’ |
N[0] |
N[1] |
N[2] |
N[3] |
N[4] |
N[5] |
N[6] |
N[7] |
N[8] |
float D[3]={10.23,45.67,76.89};
10.23 |
45.67 |
76.89 |
D[0] |
D[1] |
D[2] |
String:
- A group of characters terminated by a special character ‘\0’ (NULL) is known as a string.
- A group of characters not terminated by NULL character is not a string but just a bunch/group of characters.
‘\0’ (NULL Character)
- NULL character is the terminating character of the string.
- The ASCII value of NULL character is 0 (zero).
Note : ASCII value of
A to Z : 65 to 90
a to z : 97 to 122
0 to 9 : 48 to 57
- NULL character is not included in the length of the string.
- The size of the character array should always be one more than the length of the string, this extra location is reserved for NULL character.
- If array locations are initialized at the time of declaration then mentioning the size of the array is optional.
int a[]={10,20,30,40,50};
char n[]={‘c’,’a’,’t’,’a’,’l’,’y’,’s’,’t’,’\0’};
char n[]=”catalyst”;
float d[]={10.23,45.67,76.89};
int a[]; //error
a[]={10,20,30,40,50};
Note: Technical Question
if the number of elements in the list are less than the number of elements , then only the specified elements are initialized and rest of the elements are initialized by zero(0).
int a[5]={2,3};
In the above array
A[0]=2;A[1]=3;A[2]=0;A[3]=0;A[4]=0;
float d[5]={2.3,45.76,87.56};
In the above array
d[0]=2.3;d[1]=45.76;d[2]=87.56;d[3]=0.0;d[4]=0.0;
char n[6]={‘C’,’T’};
In the above array
n[0]=’C’;
n[1]=’T’;
n[2]=’\0’;(NULL)
Note : In case of numeric array the left locations are filled by zero(0) and in case of character array they are filled by NULL values.