Home>Article>Backend Development> In C language, when referencing an array element, what is the allowed data type of its array subscript?

In C language, when referencing an array element, what is the allowed data type of its array subscript?

青灯夜游
青灯夜游 Original
2020-07-28 15:16:44 25217browse

In C language, when referencing array elements, the data type of the array subscript is allowed to be: integer constant or integer expression. Arrays are used to store a series of data. The number used to distinguish each element of the array is called a subscript; the subscript can only be an integer constant or an integer expression. If it is a decimal, it will be automatically rounded.

In C language, when referencing an array element, what is the allowed data type of its array subscript?

The C language supports an array data structure, which can store a fixed-size sequential collection of elements of the same type. An array is used to store a series of data, but it is often thought of as a series of variables of the same type.

The numerical number used to distinguish the individual elements of the array is called a subscript.

Representation method

Array elements are the basic units that make up the array. Array elements are also variables, identified by the array name followed by a subscript. The subscript represents the sequence number of the element in the array.

The general form of one-dimensional array elements is:

数组名[下标]

The general form of two-dimensional array elements is:

数组名[下标][下标]

The subscript can only be an integer Constant or integer expression.If it is a decimal, the C compiler will automatically round it.

For example,a[5],a[i j],a[i ]are all legal array elements.

Array elements are also often called subscript variables. An array must be defined before subscripted variables can be used. In C language, subscript variables can only be used one by one, and the entire array cannot be referenced at once.

Subscripted variables and array declarations are somewhat similar in form, but the two have completely different meanings. What is given in the square brackets of the array description is the length of a certain dimension, that is, the maximum value of the subscript; and the subscript in the array element is the position identifier of the element in the array. The former can only be a constant, while the latter can be a constant, variable or expression.

There are 5 people in a study group, and each person has test scores for three courses. Find the average score of the entire group in each subject and the overall average score of each subject.

Course Grade NameMath C DBASE

Zhang 80·75·92

王61·65·71

李59·63·70

赵85 87 90

zhou76 77 85

A two-dimensional array a[5][3] can be set up to store the grades of five people in three courses. Then set a one-dimensional array v[3] to store the average score of each subject, and let the variable l be the total average score of each subject of the whole group. The programming is as follows:

void main() { int i,j,s=0,l,v[3],a[5][3]; printf("input score\n"); for(i=0;i<3;i++){ for(j=0;j<5;j++) { scanf("%d",&a[j][i]); s=s+a[j][i];} v[i]=s/5; s=0; } l=(v[0]+v[1]+v[2])/3; printf("math:%d\nc languag:%d\ndbase:%d\n",v[0],v[1],v[2]); printf("total:%d\n",l); }

A double loop is first used in the program. In the inner loop, the scores of each student in a certain course are read in sequence, and these scores are accumulated. After exiting the inner loop, the accumulated scores are divided by 5 and sent to v. This is the average score of the course. .

The outer loop loops three times in total, and calculates the average scores of each of the three courses and stores them in the v array. After exiting the outer loop, add v[0], v[1], v[2] and divide by 3 to get the overall average score of each subject. Finally, output each score according to the meaning of the question.

Recommended: "c Language Tutorial"

The above is the detailed content of In C language, when referencing an array element, what is the allowed data type of its array subscript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What file is pdb? Next article:What file is pdb?