Tuesday 12 February 2013

Defining Arrays in C Programming Language



Consider the following definitions :

int numbers[10];

This defines an integer array called numbers which can hold 10 integers which will be stored in the elements numbers[0] to numbers[9].

float fnums[10];

This defines a float array called fnums which can hold 10 float values which will be stored in the elements fnums[0] to fnums[9].

char string[10];

This defines an character array called string which can hold 10 characters which will be stored in the elements string[0] to string[9]. However, the last element in a character array is reserved, by convention, to store the string terminator character '\0' (NULL). As a result the dimension of a character string is always defined to be one larger than the maximum number of characters required to be stored in the array, so as to take care of the string terminator. When a string is input using any of the string based functions like scanf() or gets(), the NULL('\0') character is automatically appended to the string as a string terminator.

0 comments:

Post a Comment

Powered by Blogger.