Home > Backend Development > C++ > Array in C language

Array in C language

WBOY
Release: 2023-08-27 16:05:09
forward
972 people have browsed it

Array in C language

An array is a collection of elements of the same type at consecutive memory locations. The lowest address corresponds to the first element, and the highest address corresponds to the last element.

Array indexing starts with zero (0) and ends with the array size minus one (array size - 1). Array size must be an integer greater than zero.

Let us look at an example,

If array size = 10
First index of array = 0
Last index of array = array size - 1 = 10-1 = 9
Copy after login

This is the syntax of an array in C language,

type array_name[array_size ];
Copy after login

The following is the method of initializing an array.

type array_name[array_size] = { elements of array }
Copy after login

This is an example of C language array,

Example

Live demonstration

#include <stdio.h>
int main () {
   int a[5];
   int i,j;
   for (i=0;i<5;i++) {
      a[i] = i+100;
   }
   for (j=0;j<5;j++) {
      printf("Element[%d] = %d</p><p>", j, a[j] );
   }
   return 0;
}
Copy after login

Output

Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Copy after login

The above is the detailed content of Array in C language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template