C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1

6. Adding Words to Your Programs


In This Chapter


  • Understanding the string terminator

  • Determining the length of strings

  • Using character arrays: listing characters

  • Initializing strings


Although C doesn’t have string variables, you do have a way to store string data. This chapter
explains how. You already know that you must enclose string data in quotation marks. Even a single
character enclosed in quotation marks is a string. You also know how to print strings with
printf().


The only task left is to see how to use a special type of character variable to hold string data so that
your program can input, process, and output that string data.


Understanding the String Terminator


C does the strangest thing to strings: It adds a zero to the end of every string. The zero at the end of
strings has several names:



  • Null zero

  • Binary zero

  • String terminator

  • ASCII 0

  • \0


Warning

About the only thing you don’t call the string-terminating zero is zero! C programmers
use the special names for the string-terminating zero so that you’ll know that a regular
numeric zero or a character '0' is not being used at the end of the string; only the
special null zero appears at the end of a string.

C marks the end of all strings with the string-terminating zero. You never have to do anything special
when entering a string literal such as "My name is Julie." C automatically adds the null zero.
You’ll never see the null zero, but it is there. In memory, C knows when it gets to the end of a string
only when it finds the null zero.

Free download pdf