Submitted by Anonymous (not verified) on Sun, 03/10/2013 - 00:02

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.

In Listing 12.4, you see various ways to display an array of characters on the screen.

TYPE
Listing 12.4. Printing out an array of characters.


1:  /* 12L04.c: Printing out an array of characters */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     char array_ch[7] = {`H', `e', `l', `l', `o', `!', `\0'};
7:     int i;
8:
9:     for (i=0; i<7; i++)
10:       printf("array_ch[%d] contains: %c\n", i, array_ch[i]);
11:    /*---  method I ---*/
12:    printf( "Put all elements together(Method I):\n");
13:    for (i=0; i<7; i++)
14:       printf("%c", array_ch[i]);
15:    /*---  method II ---*/
16:    printf( "\nPut all elements together(Method II):\n");
17:    printf( "%s\n", array_ch);
18:
19:    return 0;
20: }


OUTPUT

The following output is a copy from my screen (I obtained these results by running the executable 12L04.exe):

ANALYSIS

C:\app>12L04
array_ch[0] contains: H
array_ch[1] contains: e
array_ch[2] contains: l
array_ch[3] contains: l
array_ch[4] contains: o
array_ch[5] contains: !
array_ch[6] contains:
Put all elements together(Method I):
Hello!
Put all elements together(Method II):
Hello!
C:\app>

As you can see from Listing 12.4, a character array, array_ch, is declared and initialized in line 6. Each element in the character array is printed out by the printf() function in a for loop shown in lines 9 and 10. There are a total of seven elements in the array; they contain the following character constants: `H', `e', `l', `l', `o', `!', and `\0'.

There are two ways to display all characters in the array, and to treat them as a character string.

Lines 12_14 show the first way, which fetches each individual element, array_ch[i], consecutively in a loop, and prints out one character next to another by using the character format specifier %c in the printf() function in line 14.

The second way is simpler. You tell the printf() function where to find the first element to start with. Also, you need to use the string format specifier %s in the printf() function as shown in line 17. Note that the array_ch expression in line 17 contains the address of the first element in the array—that is, the start address of the array.

You may be wondering how the printf() function knows where the end of the character array is. Do you remember that the last element in the character array array_ch is a \0 character? It's this null character that marks the end of the character array. As I mentioned earlier, a character array ended with a null character is called a character string in C.

The Null Character (\0)
The null character (\0) is treated as one character in C; it is a special character that marks the end of a string. Therefore, when functions like printf() act on a character string, they process one character after another until they encounter the null character. (You'll learn more about strings in Hour 13.)

The null character (\0), which is always evaluated as FALSE, can also be used for a logical test in a control-flow statement. Listing 12.5 gives an example of using the null character in a for loop.

TYPE
Listing 12.5. Stopping printing at the null character.


1:  /* 12L05.c: Stopping at the null character */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     char array_ch[15] = {`C', ` `,
7:                          `i', `s', ` `,
8:                          `p', `o', `w', `e', `r',
9:                          `f', `u', `l', `!', `\0'};
10:    int i;
11:    /*  array_ch[i] in logical test */
12:    for (i=0; array_ch[i] != `\0'; i++)
13:       printf("%c", array_ch[i]);
14:    return 0;
15: }


OUTPUT

By running the executable 12L05.exe, I obtain the following output:

ANALYSIS

C:\app>12L05
C is powerful!
C:\app>

In Listing 12.5, a character array, array_ch, is declared and initialized with
the characters (including the space characters) from the string C is powerful!, in lines 6_9.

Note that the last element in the array contains the null character (\0), which is needed later in a for loop.

The for loop in lines 12 and 13 tries to print out each element in the array array_ch to show the string C is powerful! on the screen. So in the first expression field of the for statement (loop), the integer variable i, which is used as the index to the array, is initialized with 0.

Then, the expression in the second field, array_ch[i] != `\0', is evaluated. If the expression returns logical TRUE, the for loop iterates; otherwise, the loop stops. Starting at the first element in the array, the array_ch[i] expression keeps returning TRUE until the null character is encountered. Therefore, the for loop can put all characters of the array on the screen, and stop printing right after the array_ch[i] returns logical FALSE. In fact, you can simplify the array_ch[i] != `\0' expression in the second field of the for statement to array_ch[i] because `\0' is evaluated as FALSE anyway.

Related Items

মডুলার C প্রোগ্রামিং (Modular C Programming)

কেবল মাত্র একটি ফাংশন দিয়ে কোনো বড়ো জটিল সমস্যা সমাধানের চেষ্টা করা ভাল প্রোগ্রামিংয়ের পদ্ধতি নয়। সঠিক পদ্ধতি হ'ল সমস্যাটিকে কয়েকটি ছোট ছোট এবং সরল টুকরো করে ফেলা যাতে তা আরও বিশদে বোঝা যায় । তারপরে এই ছোট এবং সরল সমস্যাগুলি সমাধান করার জন্য ছোট ছোট ফাংশন ব্লক তৈরি করা এবং পরে সেগুলি নিয়মানুযায়ী সংযোজিত করা ।

Programming Style

Programming Style

In this section, I'd like to briefly highlight some points that will help you write clean programs that can easily be read, understood, and maintained.

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.

Question and Answer

    Q Is the C preprocessor part of the C compiler?

    A No. The C preprocessor is not part of the C compiler. With its own line-oriented grammar and syntax, the C preprocessor runs before the compiler in order to handle named constants, macros, and inclusion of files.

Compiling Your Code Under Conditions

Compiling Your Code Under Conditions

You can select portions of your C program that you want to compile by using a set of preprocessor directives. This is useful, especially when you're testing a piece of new code or debugging a portion of code.