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:
#include<stdio.h> 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:
7 8 4 5
Additional Resources:
- How to Pass a 1D Array as a Function Arguments in C?
- C Program to Find Sum of two One-Dimensional Arrays using Malloc
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Apply for Computer Science Internship
- Practice BCA MCQs
- Apply for C Internship
- Buy C Books
- Watch Advanced C Programming Videos