Character Strings in C: Arrays of Characters

If you want to be able to deal with variables that can hold more than a single character, this is precisely where the array of characters comes into play.

In Program 7.6, you defined an array of characters called word as follows:

char word [] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘!’ };

Remembering that in the absence of a particular array size, the C compiler automatically computes the number of elements in the array based upon the number of initializers, this statement reserves space in memory for exactly six characters,  as shown in Figure 10.1.

To print out the contents of the array word, you ran through each element in the array and displayed it using the %c format characters.

With this technique, you can begin to build an assortment of useful functions for dealing with character strings. Some of the more commonly performed operations on character strings include combining two character strings together (concatenation), copying one character string to another, extracting a portion of a character string (sub- string), and determining if two character strings are equal (that is, if they contain the same characters). Take the first mentioned operation, concatenation, and develop a func- tion to perform this task.You can define a call to your concat function as follows:

concat (result, str1, n1, str2, n2);

where str1 and str2 represent the two character arrays that are to be concatenated and n1 and n2 represent the number of characters in the respective arrays. This makes the function flexible enough so that you can concatenate two character arrays of arbitrary length. The argument result represents the character array that is to be the destination of the concatenated character arrays str1 followed by str2. See Program 10.1.


Program 10.1   Concatenating  Character Arrays

// Function to concatenate two character arrays

#include <stdio.h>

void concat (char result[], const char str1[], int n1, const char str2[], int n2)


int i, j;

// copy str1 to result

for ( i = 0; i < n1; ++i )

result[i] = str1[i];

// copy str2 to result

for ( j = 0; j < n2; ++j )

result[n1 + j] = str2[j];


int main (void)


void  concat (char result[], const char str1[], int n1, const char str2[], int n2);

const char s1[5] = { ‘T’, ‘e’, ‘s’, ‘t’, ‘ ‘};

const char s2[6] = { ‘w’, ‘o’, ‘r’, ‘k’, ‘s’, ‘.’ };

char  s3[11];

int   i;

concat (s3, s1, 5, s2, 6);

for ( i = 0; i < 11; ++i )

printf (“%c”, s3[i]);

printf (“\n”);

return 0;


Program 10.1   Output

Test works.

The first for loop inside the concat function copies the characters from the str1 array into the result array. This loop is executed n1 times, which is the number of characters contained inside the str1 array.

The second for loop copies str2 into the result array. Because str1 was n1 charac- ters long, copying into result begins at result[n1]—the position immediately follow- ing the one occupied by the last character of str1. After this for loop is done, the result array contains the n1+n2 characters representing str2 concatenated to the end of str1.

Inside the main routine, two const character arrays, s1 and s2, are defined. The first array is initialized to the characters ‘T’, ‘e’, ‘s’, ‘t’, and ‘ ‘. This last character represents a blank space and is a perfectly valid character constant. The second array is initially set to the characters ‘w’, ‘o’, ‘r’, ‘k’, ‘s’, and ‘.’.A third character array, s3, is defined with enough space to hold s1 concatenated to s2, or 11 characters. It is not declared as a const array because its contents will be changed.

The function call

concat (s3, s1, 5, s2, 6);

calls the concat function to concatenate the character arrays s1 and s2, with the desti- nation array s3. The arguments 5 and 6 are passed to the function to indicate the num- ber of characters in s1 and s2, respectively.

After the concat function has completed execution and returns to main,a for loop is set up to display the results of the function call. The 11 elements of s3 are displayed at the terminal, and as can be seen from the program’s output, the concat function seems to be working properly. In the preceding program example, it is assumed that the first argument to the concat function—the result array—contains enough space to hold the resulting concatenated character arrays. Failure to do so can produce unpredictable results when the program is run.

Source: Kochan Stephen G. (2004), Programming in C: A Complete Introduction to the C Programming Language, Sams; Subsequent edition.

Leave a Reply

Your email address will not be published. Required fields are marked *