Getting Started with C programming--arrays (top)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.