C programming

Declaring Strings

Declaring Strings

This section teaches you how to declare and initialize strings, as well as the difference between string constants and character constants. First, let’s review the definition of a string.

What Is a String?

As introduced in Hour 12, “Storing Similar Data Items,” a string is a character array terminated by a null character (\0).

For instance, a character array, array_ch, declared in the following statement, is considered a character string:

Exercises : Answer the following Question

To help solidify your understanding of this hour’s lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson. The answers and hints to the questions and exercises are given in Appendix E, “Answers to Quiz Questions and Exercises.”
Quiz

What does the following statement do?

int array_int[4] = {12, 23, 9, 56};

Given an array, int data[3], what’s wrong with the following initialization?

Question and Answer

Question and Answer

Q Why do you need to use arrays?

A In many cases, you need to declare a set of variables that are of the same data type. Instead of declaring each variable separately, you can declare all variables collectively in the format of an array. Each variable, as an element of the array, can be accessed either through the array element reference or through a pointer that references the array.

Q What is the minimum index in an array?

Unsized Arrays

Unsized Arrays

As you’ve seen, the size of a dimension is normally given during the declaration of an array. It means that you have to count each element in an array. It could be tedious to do so, though, especially if there are many elements in an array.

The good news is that the C compiler can actually calculate a dimension size of an array automatically if an array is declared as an unsized array. For example, when the compiler sees the following unsized array:

int list_int[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90};

Multidimensional Arrays

Multidimensional Arrays

So far, all the arrays you’ve seen have been one-dimensional arrays, in which the dimension sizes are placed within a pair of brackets ([ and ]).

In addition to one-dimensional arrays, the C language also supports multidimensional arrays. You can declare arrays with as many dimensions as your compiler allows.

The general form of declaring a N-dimensional array is

data-type Array-Name[Array-Size1][Array-Size2]…
[Array-SizeN];

where N can be any positive integer.

Displaying Arrays of Characters

Displaying Arrays of Characters

This subsection focuses on arrays of characters. On most machines, the char data type takes one byte. Therefore, each element in a character array is one byte long. The total number of elements in a character array is the total number of bytes the array takes in the memory.

More importantly in C, a character string is defined as a character array whose last element is the null character (\0). Hour 13, “Manipulating Strings,” introduces more details about strings.