C Language Learning Tutorials Fourth-arrays (1)

Source: Internet
Author: User
Tags arrays constant expression numeric

Array

In the design of the array, some variables with the same type are organized in an orderly manner for the convenience of processing. These ordered sets of similar data elements are called arrays. In the C language, an array belongs to the constructed data type. An array can be decomposed into multiple array elements, which can be basic data types or constructed types. Thus, depending on the type of the array element, the array can be categorized into numeric, character, array, and structure arrays.

This chapter describes numeric arrays and character arrays, and the rest is introduced in subsequent chapters. Array type Description the use of arrays in the C language must be preceded by a type description. General form of Array description
is: type descriptor array name [constant expression], ... Where the type descriptor is either a basic data type or a constructed data type. The array name is a user-defined array identifier. A constant expression in square brackets represents the number of data elements, also known as the length of an array.
For example:
int a[10]; Description integer array A, with 10 elements.
float B[10],C[20]; Description of the real array B, there are 10 elements, the real array C, there are 20 elements.
Char ch[20]; Description character Array ch, with 20 elements.

The following points should be noted for the array type description:
1. The type of the array is actually the value type of the exponential group element. For the same array, all of its elements have the same data type.
2. The writing rules of array names shall conform to the written provisions of identifiers.
3. Array names cannot be the same as other variable names, for example:
void Main ()
{
int A;
float A[10];
......
}
Is wrong.
4. The constant expression in square brackets represents the number of array elements, such as a[5], which indicates that array a has 5 elements. But its subscript is calculated from 0. Therefore the 5 elements are a[0],a[1],a[2],a[3],a[4] respectively.
5. You cannot use variables in brackets to represent the number of elements, but it can be a symbolic constant or a constant expression. For example:
#define FD 5
void Main ()
{
int A[3+2],B[7+FD];
......
}
is legal. But the way it is described below is wrong.
void Main ()
{
int n=5;
int a[n];
......
}
6. Allow multiple arrays and multiple variables to be described in the same type description.
For example: int a,b,c,d,k1[10],k2[20];

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.