C Programming Language

One dimensional array in c programming language : ID array

                     The declaration form of one-dimensional array is
 Data_type array_name [size];

                    The following declares an array called ‘numbers’ to hold 5 integers and sets the first and last elements. C arrays are always indexed from 0. So the first integer in ‘numbers’ array is numbers[0] and the last is numbers[4].

int numbers [5];
numbers [0] = 1;       // set first element
numbers [4] = 5;       // set last element

                This array contains 5 elements. Any one of these elements may be referred to by giving the name of the array followed by the position number of the particular element in square brackets ([]). The first element in every array is the zeroth element.Thus, the first element of array ‘numbers’is referred to as numbers[ 0 ], the second element of array ‘numbers’is referred to as numbers[ 1 ], the fifth element of array ‘numbers’is referred to as numbers[ 4 ], and, in general, the n-th element of array ‘numbers’is referred to as numbers[ n - 1 ].


One Dimensional l array

                  It's a very common error to try to refer to non-existent numbers[ 5], element. C does not do much hand holding. It is invariably up to the programmer to make sure that programs are free from errors. This is especially true with arrays. C does not complain if you try to write to elements of an array which do not exist!

For example: If you wrote:
numbers[ 5 ], = 6;
                   C would happily try to write 6 at the location which would have corresponded to the sixth element, had it been declared that way. Unfortunately this would probably be memory taken up by some other variable or perhaps even by the operating system. The result would be either:

  • The value in the incorrect memory location would be corrupted with unpredictable consequences.
  • The value would corrupt the memory and crash the program completely!

                      The second of these tends to be the result on operating systems with proper memory protection. Writing over the bounds of an array is a common source of error. Remember that the array limits run from zero to the size of the array minus one.

Out of bounds access

I would be doing well, that really shows the range of valid indices, please check often. 10 When the number of elements, all the indices of 10 or more is illegal. Also, whether any size of negative indices is also illegal.

These errors are detected at compile time is not. It is an error at run time. However, there is no guarantee that the error reliably. Sometimes works correctly (seems to be moving.) This is the "luck ran" instead of "bad luck worked" so, please, as no doubt.

                   The best way to see these principles is by use of an example, so load the program  and display it on your monitor.

#include <stdio.h>
#include <conio.h>
int main( )
       char name[7]; /* define a string of characters */
       name[0] = 'A';
       name[1] = 's';
       name[2] = 'h';
       name[3] = 'r';
       name[4] = 'a';
       name[5] = 'f';
       name[6] = '\0'; /* Null character - end of text */
       name[7] = ‘X’;
       printf("My name is %s\n",name);
       printf("First letter is %c\n",name[0]);
       printf("Fifth letter is %c\n",name[4]);
       printf("Sixth letter is %c\n",name[5]);
       printf("Seventh letter is %c\n",name[6]);
       printf("Eight letter is %c\n",name[7]);
       return 0;

                 The elements of an array can also be initialized in the array declaration by following the declaration with an equal sign and a comma-separated list (enclosed in braces) of initializers.

                The following program initializes an integer array with five values and prints the array.

#include <stdio.h>
#include <conio.h>
int main()
     int numbers[]={1,2,3,4,5};
     int i;
     printf("Array elements are\n");
     return 0;

                    If there are fewer initializers than elements in the array, the remaining elements are initialized to zero. For example, the elements of the array n could have been initialized to zero with the declaration
int n[ 10 ] = { 0 };
which explicitly initializes the first element to zero and initializes the remaining nine elements to zero because there are fewer initializers than there are elements in the array. It is important to remember that arrays are not automatically initialized to zero. The programmer must at least initialize the first element to zero for the remaining elements to be automatically zeroed. This method of initializing the array elements to 0 is performed at compile time for static arrays and at run time for automatic arrays.

There are more complex uses of arrays, which I will address later along with pointers.

Arrays Home Two Dimensional array
c program stats