An array is one of the most important data structures, the so-called array, which is the set of elements of the same data type arranged in a certain order, that is, a variable of the same type is named with a name, and then the set of their variable is distinguished by a number, which is called the array name, and the number is called subscript. Each variable that makes up an array is called the component of an array, also known as an element of an array, sometimes called a subscript variable.
Assignment and output of an array:
# include <stdio.h>int main (void) {int a[5] = {1, 2, 3, 4, 5};//array name a followed by brackets [], the assignment is in curly braces {}, separated by commas. 5 indicates that there are 5 elements in array a, int i; They are respectively used a[0], a[1], a[2], a[3], A[4] said. Note that the first element starts with 0! for (i=0; i<5; ++i) {printf ("a[%d] =%d\n", I, A[i]);} return 0;} /* Output result: a[0] = 1a[1] = 2a[2] = 3a[3] = 4a[4] = 5Press any key to continue*/
Exercise 1:
# include <stdio.h>int main (void) {int a[5];//If the array is not initialized, its value is garbage value. int i;for (i=0; i<5; ++i) {printf ("a (%d) =%d\n", i+1, A[i]);} return 0;}
Exercise 2:
# include <stdio.h>int main (void) {int a[5] = {100};//Assigned to A[0] element only. printf ("%d\n", A[0]); return 0;}
Artificial assignment of an array:
# include <stdio.h>int main (void) {int a[5];int i;for (i=0; i<5; ++i) {printf ("Please enter");p rintf ("%d", i+1);p rintf (" The value of the array element: "); scanf ("%d ", &a[i]);} for (i=0; i<5; ++i) printf ("a[%d] =%d\n", I, A[i]); return 0;}
To run the example:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Getting Started with C programming--arrays (top)