Book
Collection
Click for Table of Contents
© 2025 by Rance D. Necaise
C Primer for Python Programmers
Copyright © 2025
Rance D. Necaise

7.2 Defining Arrays

An array in C can be either static or dynamic, depending on when it is created. A static array is one in which the size is known at compile-time. Thus, its size must be a literal or the value of a constant variable that is specified when the program is compiled. A dynamic array, on the other hand, is created at run-time and its size can be determined during execution of the program. For now, we focus on static arrays and defer consideration of dynamic arrays until later in the chapter.

Array Declarations

Arrays in C can only store values of a specified data type, similar to variables. To create a static array you must specify three pieces of information:

  • the name of the array
  • the number of elements in the array, and
  • the type of data stored in each element.

For example, the declaration:

  1.   int grades[11];

creates an array with the name grades that contains 11 elements, all of which store integer values.

Note that the elements of the array are not initialized to any given value, but will contain the values that are currently stored in the memory location where the array is created.

Syntax
Defining an Array
type name[size];
name

The name must be a valid identifier that specifies the name of the array variable.

type

The specified type can be any valid data type which is used to indicate the type of data stored in each element of the array.

size

The size value indicates the number of elements contained in the array. Once created, the size of an array can not be changed. The size value must be a constant, it must be a literal value or the value of a constant variable.

An array can be created to store any type of data. For example, we can create an array to store real values or individual characters:

  1.   float scores[5];
  2.   char code[8];
Question 7.2.1

Provide the C code to declare an integer array named states that contains 50 elements.

  1. int states[50];

Array Size

As indicated earlier, when a static array is created, the size of the array must be known at compile time. Thus, the array size must be either a literal integer value or a constant integer variable. For example,

  1. const int MAX_SIZE = 100;
  2.  
  3. int main()
  4. {
  5.   int grades[11];
  6.   int values[MAX_SIZE]
  7.   float temp[MAX_SIZE + 1];
  8.  
  9. }
Warning
Warning

Do not use a non-constant variable to specify the size of an array. While the following will compile and execute,

  1. int main()
  2. {
  3.   int n;
  4.  
  5.   scanf("%d", &n);
  6.   int values[n];
  7. }
  8.  

the results will be unpredictable because the array would have been created at compile time with a size of 0, which can be accessed and used.

Question 7.2.2

Consider the array declaration below. How many elements does the array contain?

  1.   float xcoords[4];
Select the correct answer below by clicking on the appropriate box.
  1. 1
  2. 3
  3. 4
  4. 5