One Dimensional Array in C with Examples

This C Tutorial Explains One Dimensional Array in C and its initialization with Example(s).

What is One Dimensional Array in C?

An array with just one dimension is called one-dimensional array. Dimension of an array is specified by a pair of square brackets called subscript immediately after the name of array.

Syntax of One Dimensional Array:

datatype array_name [size];

For example,

    int new[n];    /* new, a one dimensional array */

Expression ‘n’ in square brackets specifies no. of elements in the array ‘new’ meaning that new contains ‘n’ integers.

    int champions[12][10];    /* champions, an array, have two dimensions */

Initialization of One Dimensional Array:

An array is allocated block of memory for the specified no. of elements in contiguous or continuous locations by the compiler.

For example,

    int num[10];    /* allocates enough memory to hold 10 integers */

Let’s see what happens when declaration is as below:

    int count[];	/* no. of elements not specified! */

Certainly, this is an error! How would compiler know about what amount of memory to allocate? So, specifying no. of elements is must unless you declare the array and initialize it. For example:

    int mugs[] = {1,2,3,4,5,6,7,8,9,10};
    /* no. of elements not specified but mugs[] initialized */

Remember, specifying elements isn’t needed during initialization of an array changes its size most often. O key! we consider one more example:

    int students[5] = {35, 44, 55, 80, 40, 20}; 
                 /* more elements than size? */

Well! We can’t pack 6 integers into an array of 5 integers. This is an error! What about

    int hours[10] = {1,2,3,4,5};      /* less no. of elements than size */

When during declaration and initialization of an array with specified size, if no. of initializers in the list is less than size specified, given initializers are assigned to successive locations beginning with index ZERO and rest of uninitialized locations are each automatically assigned with value ZERO.


The above initialization of array hours[10] can be though of as the successive assignments below,

    hours[0] = 1;
    hours[1] = 2;
    hours[2] = 3;
    hours[3] = 4;
    hours[4] = 5;
    hours[5] = 0;   
    hours[6] = 0;
    hours[7] = 0;
    hours[8] = 0;
    hours[9] = 0;
    hours[10] = 0;

Compiling Time Initialization Example:

int main()
    int a[4]={7, 8, 4, 5};
    printf("%d\n", a[0]);
    printf("%d\n", a[1]);
    printf("%d\n", a[2]);
    printf("%d\n", a[3]);

Program Output:


Additional Resources:

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.