MCS-201 · Unit 4

Arrays and Strings

Section 2 of 9

Block 1 · Unit 4Arrays and Strings8 min · Section 2 of 9

4.2Array Initialisation and Subscripts

Covers IGNOU MCS-201 §4.3, 4.3.1, 4.3.2, 4.4

An array’s elements need values before they are used. C lets you give all of them at once in the declaration. This section covers the initialisation rules, what uninitialised elements hold, and how subscripts select elements, including what goes wrong when a subscript is out of range.

Initialising in the declaration

Syntax
data-type array-name[size] = { value1, value2, ..., valueN };
DeclarationResult
int digits[5] = {1, 2, 3, 4, 5};All five elements set
int digits[] = {1, 2, 3, 4, 5};Size omitted: the compiler counts 5
int height[10] = {60, 70, 68};First three set; the other seven are 0
int zero[100] = {0};Every element is 0
int a[3] = {1, 2, 3, 4};Error or warning: too many values

The partial-initialisation rule gives an easy way to clear a whole array: = {0}.

What an array holds when it is not initialised depends on where it is declared:

Kind of arrayInitial contents
Local (inside a function), no initialiserIndeterminate (“garbage”) values
Global or static, no initialiserAll zeros
Any array with a partial initialiserListed values, then zeros

An array cannot be assigned as a whole after its declaration: a = {1, 2, 3}; and a = b; are errors. Assign element by element, usually in a loop.

Character array initialisation

A character array can be initialised with a string constant, which adds the null character '\0' at the end:

DeclarationSizeContentsA string?
char t[] = "TIN";4'T' 'I' 'N' '\0'Yes
char t[4] = "TIN";4'T' 'I' 'N' '\0'Yes
char t[3] = "TIN";3'T' 'I' 'N'No: no room for '\0'
char t[] = {'T', 'I', 'N'};3'T' 'I' 'N'No

Strings are covered fully later in this unit.

Subscripts

A subscript may be a constant, a variable or any integer expression: a[3], a[i], a[i + 1], a[2 * k].

An int array of five elements in memory

Out-of-range subscripts

C does not check subscripts when the program runs. Using an index outside 0 to size − 1 is undefined behaviour: the program may read garbage, overwrite another variable, crash, or appear to work. This is the most common source of serious bugs in C programs.

Key takeaways

  • type a[n] = {…}; initialises from index 0; missing values become 0; the size may be omitted.
  • Uninitialised local arrays hold garbage; global and static arrays hold zeros.
  • A string constant initialiser adds '\0', so it needs one extra element.
  • Subscripts run from 0 to size − 1; C does not check them.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

After int a[6] = {4, 8};, what is a[3]?

Show answer

Answer: D. 0

With a partial initialiser, the remaining elements are set to 0. Garbage applies only when there is no initialiser at all.

Question 2 · multiple choice

What is sizeof s for char s[] = "HELLO";?

Show answer

Answer: B. 6

Five letters plus the null character '\0' added by the string constant make 6 bytes.

Question 3 · multiple choice

What happens when a program executes a[10] = 7; for int a[10];?

Show answer

Answer: C. The behaviour is undefined, because C does not check array bounds

Index 10 is one past the last element (9). C performs no run-time check, so memory outside the array is overwritten with unpredictable results.

Question 4 · multiple choice

What does this program print?

C · sum_array.c
#include <stdio.h>

int main(void)
{
    int a[4] = {5, 10, 15};
    int i, s = 0;
    for (i = 0; i < 4; i++)
        s += a[i];
    printf("%d\n", s);
    return 0;
}
Show answer

Answer: A. 30

The fourth element is 0 because of partial initialisation, so the sum is 5 + 10 + 15 + 0 = 30. It is not unpredictable, since an initialiser is present.

Question 5 · multiple choice

Which statement is valid after int a[3], b[3] = {1, 2, 3};?

Show answer

Answer: C. a[0] = b[0];

Arrays cannot be assigned as a whole; elements are copied one at a time, usually in a loop.

Question 6 · short answer

Write a loop that sets every element of int sq[10]; to the square of its index, and state the value of sq[7].

Show worked answer

for (i = 0; i < 10; i++) sq[i] = i * i;. The loop runs with i from 0 to 9, which are exactly the valid subscripts, and stores i × i in element i. Therefore sq[7] is 7 × 7 = 49.

↑ ↓ move↵ openTab switch type