Unit 3: Declaring Arrays
Unit 3: Declaring Arrays
Declaring Arrays
To declare an array in C, the type of the elements and the number of elements required
by an array as follows −
type arrayName [ arraySize ];
Initializing Arrays
You can initialize an array in C either one by one or using a single statement as follows
−
int arr[5] = {100, 20, 34, 70, 500};
The number of values between braces { } cannot be larger than the number of elements
that we declare for the array between square brackets [ ].
If you omit the size of the array, an array just big enough to hold the initialization is
created. Therefore, if you write −
int arr[5] = {100, 20, 34, 70, 500};
You will create exactly the same array as you did in the previous example. Following is
an example to assign a single element of the array −
arr[4] = 500;
The above statement assigns the 5th element in the array with a value of 500. All arrays
have 0 as the index of their first element which is also called the base index and the last
index of an array will be total size of the array minus 1. Shown below is the pictorial
representation of the array we discussed above –
arr
Example Program
3.2 Declaration, initialization of array, accessing elements of an array
#include <stdio.h>
int main () {
int main()
{
int arr[5], i;
printf("Resultant array:\n");
return 0;
}
//sort array
for(i=0;i< n;i++)
{
for(j=i+1;j< n;j++)
{
if(arr[i]>arr[j])
{
temp =arr[i];
arr[i] =arr[j];
arr[j] =temp;
}
}
}
printf("\nArray elements after sorting:\n");
for(i=0;i< n;i++)
{
printf("%d\n",arr[i]);
}
return 0;
}
#include<stdio.h>
int main() {
int a[30], ele, num, i;
return (0);
}
Two-dimensional Arrays
The simplest form of multidimensional array is the two-dimensional array. A two-
dimensional array is, in essence, a list of one-dimensional arrays. To declare a two-
dimensional integer array of size [x][y], you would write something as follows −
type arrayName [ x ][ y ];
Where type can be any valid C data type and arrayName will be a valid C identifier. A
two-dimensional array can be considered as a table which will have x number of rows
and y number of columns. A two-dimensional array a, which contains three rows and
four columns can be shown as follows −
Thus, every element in the array a is identified by an element name of the form a[ i ][ j
], where 'a' is the name of the array, and 'i' and 'j' are the subscripts that uniquely identify
each element in 'a'.
The nested braces, which indicate the intended row, are optional. The following
initialization is equivalent to the previous example −
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
Live Demo
#include <stdio.h>
int main () {
return 0;
}
3.3 Introduction to String, Initializing, Declaring and Display of string
Strings are actually one-dimensional array of characters terminated by a null character
'\0'. Thus a null-terminated string contains the characters that comprise the string
followed by a null.
The following declaration and initialization create a string consisting of the word "Hello".
To hold the null character at the end of the array, the size of the character array
containing the string is one more than the number of characters in the word "Hello."
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
If you follow the rule of array initialization then you can write the above statement as
follows −
char greeting[] = "Hello";
Following is the memory presentation of the above defined string in C/C++ −
Actually, you do not place the null character at the end of a string constant. The C
compiler automatically places the '\0' at the end of the string when it initializes the array.
Let us try to print the above mentioned string −
Live Demo
#include <stdio.h>
int main () {
2 strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
3 strlen(s1);
Returns the length of string s1.
4
strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0
if s1>s2.
5 strchr(s1, ch);
Returns a pointer to the first occurrence of character ch in string s1.
6 strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in string s1.
#include <stdio.h>
#include <string.h>
int main () {
return 0;
}
strcpy()
#include <stdio.h>
#include <string.h>
int main () {
char str1[15];
char str2[15];
int ret;
strcpy(str1, "abcdef");
strcpy(str2, "ABCDEF");
if(ret < 0) {
printf("str1 is less than str2");
} else if(ret > 0) {
printf("str2 is less than str1");
} else {
printf("str1 is equal to str2");
}
return(0);
}