You are on page 1of 11

Arrays in C Programming

Array is a collection of variables belongings to the same data type. You


can store group of data of same data type in an array.

 Array might be belonging to any of the data types


 Array size must be a constant value.
 Always, Contiguous (adjacent) memory locations are used to store
array elements in memory.
 It is a best practice to initialize an array to zero or null while
declaring, if we do not assign any values to array.

EXAMPLE FOR ARRAYS:


 int a[10]; // integer array
 char b[10]; // character array i.e. string

DECLARING AN ARRAY:

Like any other variable, arrays must be declared before they are used.
General form of array declaration is:-

Here int is the data type, arr is the name of the array and 10 is the size of
array. It means array arr can only contain 10 elements of int type.
Index of an array starts from 0 to size-1 i.e first element of arr array will
be stored at arr[0]address and the last element will occupy arr[9].
INITIALIZATION OF AN ARRAY:

After an array is declared it must be initialized. Otherwise, it will


contain garbage value(any random value). An array can be initialized at
either compile time or at run time.

COMPILE TIME ARRAY INITIALIZATION:


Compile time initialization of array elements is same as ordinary variable
initialization. The general form of initialization of array is:-

One important thing to remember is that when you will give more
initialization(array elements) than the declared array size than the compiler will
give an error.
RUNTIME ARRAY INITIALIZATION:
An array can also be initialized at run time using scanf() function. This approach is
usually used for initializing large arrays, or to initialize arrays with user specified
values. Example,

TYPES OF ARRAYS:

There are 2 types of C arrays. They are,

1. One dimensional Array


2. Multi dimensional Array

 Two dimensional array

 Three dimensional array

 Four dimensional array etc.

ONE DIMENSIONAL ARRAY:

The arrays which consists of only single data in each array are known as one
dimensional array.
The examples shown above are One Dimensional Arrays.

MULTI DIMENSIONAL ARRAY:

 Multi-dimensional arrays are declared by providing more than one set of


square [ ] brackets after the variable name in the declaration statement.

 One dimensional arrays do not require the dimension to be given if the


array is to be completely initialized. By analogy, multi-dimensional arrays
do not require the first dimension to be given if the array is to be
completely initialized. All dimensions after the first must be given in any
case.

 For two dimensional arrays, the first dimension is commonly considered to


be the number of rows, and the second dimension the number of
columns. We will use this convention when discussing two dimensional
arrays.

 Two dimensional arrays are considered by C/C++ to be an array of ( single


dimensional arrays ). For example, "int numbers[ 5 ][ 6 ]" would refer to a
single dimensional array of 5 elements, wherein each element is a single
dimensional array of 6 integers. By extension, "int numbers[ 12 ][ 5 ][ 6 ]"
would refer to an array of twelve elements, each of which is a two
dimensional array, and so on.

 Another way of looking at this is that C stores two dimensional arrays by


rows, with all elements of a row being stored together as a single unit.
Knowing this can sometimes lead to more efficient programs.

 Multidimensional arrays may be completely initialized by listing all data


elements within a single pair of curly {} braces, as with single dimensional
arrays.

 It is better programming practice to enclose each row within a separate


subset of curly {} braces, to make the program more readable. This is
required if any row other than the last is to be partially initialized. When
subsets of braces are used, the last item within braces is not followed by a
comma, but the subsets are themselves separated by commas.

 Multidimensional arrays may be partially initialized by not providing


complete initialization data. Individual rows of a multidimensional array
may be partially initialized, provided that subset braces are used.

EXAMPLE:

The above example is a Two-Dimensional Array.


STRING AND CHARACTER ARRAYS:

String is a sequence of characters that is treated as a single data item and


terminated by null character '\0'.Remember that C language does not support
strings as a data type.A string is actually one-dimensional array of characters in C
language.These are often used to create meaningful and readable programs.

DECLARING AND INITIALIZING STRING VARIABLES:

String Input and Output:

Input function scanf() can be used with %s format specifier to read a string input
from the terminal. But there is one problem with scanf() function, it terminates its
input on the first white space it encounters.

INPUT:

TO GET OUTPUT:
String Handling Functions

C language supports a large number of string handling functions that can be used
to carry out many of the string manipulations.These functions are packaged in
string.h library.Hence,you must include string.h header file in your programs to
use these functions.

The following are the most common used functions:

 strcat()

It is used to concatenate(combine) two strings.

 strlen()

It is used to show length of a string.

 strrev()

It is used to show reverse of a string.


 strcpy()

Copies one string into another.

 strcmp()

It is used to compare two string.

C Programming Array Exercises

The exercises below are based upon the following declarations/initializations. ( You may assume
that each exercise starts with freshly initialized arrays. )

int square[5][5]={0}, product[5][6];

int table[5][6]={{1,2,3,4,5},
{2,4,6,8,10},
{20,10,5,3,1},
{3,6,9,12,15}};
1. Write the code ( executable statements ) to fill the array square with the identity matrix.

2. Write the code to fill the array square as shown:

1
4

16

25

3. Write the code to fill the array square as shown:

10

20

40

80
4. Write the code to fill the array square as shown:

1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

16 17 18 19 20

21 22 23 24 25

5. The partially initialized array "table" can be viewed as a primitive spreadsheet, in


which the last column and bottom row have been left blank. Write the code to fill
in this row and column with the totals of each column, each row, and the grand
total.

1 2 34 5

2 4 68 10

20 10 53 1

3 6 912 15

6. Write the code to calculate the product of "square" times "table" and put the
result in "product".

You might also like