C Tutorial (6) : String

Although C doesn’t have string variables, you do have a way to store string data. You already know that you must enclose string data in quotation marks. Even a single character enclosed in quotation marks is a string.

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

Note : 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 Verma.” 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.

Note :  In ASCII Table, The very first entry is labeled null, and the ASCII number for null is 0. Look further down at ASCII 48, and you’ll see a 0. ASCII 48 is the character ‘0’, whereas the first ASCII value is the null zero. C puts the null zero at the end of strings. Even the string “I am 20” ends in an ASCII 0 directly after the character 0 in 20.

The string terminator is sometimes called \0 (backslash zero) because you can represent the null zero by enclosing \0 in single quotes. Therefore, ‘0’ is the character zero, and ‘\0’ is the string terminator.

Figure shows how the string “Crazy” is stored in memory. As you can see, it takes 6 bytes (a byte is a single memory location) to store the string, even though the string has only five letters. The null zero that is part of the string “Crazy” takes one of those six memory locations.

c_string_1

A string always ends with a null zero in memory.

The Length of Strings

The length of a string is always the number of characters up to, but not including, the null zero. The null zero is never counted when determining the length of a string.

Given the definition of the string length, the following strings all have lengths of nine characters:

Wednesday
August 10
I am here

When counting the length of strings, remember that you must account for every space. So although the second string has eight letters and numbers, as well as a space in the middle, and the third string has seven letters, as well as two spaces in the middle, are all considered nine-character strings.

Tip : All single characters of data have a length of 1. Therefore, both ‘X’ and “X” have lengths of one, but the “X” consumes two characters of memory because of its null zero.

Character Arrays: Lists of Characters

Character arrays hold strings in memory. An array is a special type of variable that you’ll hear much more about in upcoming tutorials. All the data types—int, float, char, and the rest—have corresponding array types. An array is nothing more than a list of variables of the same data type.

Before you use a character array to hold a string, you must tell C that you need a character array in the same place you would tell C that you need any other kind of variable. Use brackets ([ and ]) after the array name, along with a number indicating the maximum number of characters the string will hold.

char month[10];                 /* Defines a character array */

Note : Array definitions are easy. Take away the 10 and the brackets, and you have a regular character variable. Adding the brackets with the 10 tells C that you need 10 character variables, each following the other in a list named month.

The reason 10 was used when defining the array is that the longest month name, September, has nine characters. The tenth character is for, you guessed it, the null zero.

Note : You always have to reserve enough character array space to hold the longest string you will need to hold, plus the string terminator. You can define more array characters than needed, but not fewer than you need.

If you want, you can store a string value in the array at the same time you define the array:

char month[10] = "January";             /* Defines a character array */

Figure shows you what this array looks like. Because nothing was put in the last two places of the array (January takes only seven characters plus an eighth place for the null zero), you don’t know what’s in the last two places.

c_string_2

Defining and initializing an array named month that holds string data.

Each individual piece of an array is called an element. The month array has 10 elements. You can distinguish between them with subscripts. Subscripts are numbers that you specify inside brackets that refer to each of the array elements.

All array subscripts begin with 0. The first element in the month array is called month[0]. The last is called month[9] because there are 10 elements altogether, and when you begin at 0, the last is 9.

Each element in a character array is a character. The combination of characters—the array or list of characters—holds the entire string. If you wanted to, you could change the contents of the array from January to March one element at a time, like this:

Month[0] = 'M';
Month[1] = 'a';
Month[2] = 'r';
Month[3] = 'c';
Month[4] = 'h';
Month[5] = '\0'; //You must add this

It is vital that you insert the null zero at the end of the string. If you don’t, the month array would still have a null zero three places later at Month[7]; when you attempted to print the string, you would get this:

Marchry

Tip : Printing strings in arrays is easy. You use the %s conversion character:

printf("The month is %s", month);

If you define an array and initialize the array at the same time, you don’t have to put the number in brackets. Both of the following do exactly the same thing:

char month[8] = "January"; 
char month[] = "January";

In the second example, C counts the number of characters in January and adds one for the null zero. You can’t store a string larger than eight characters later, however. If you want to define a string’s character array and initialize it but leave extra padding for a longer string later, you would do this:

char month[25] = "January";    /* Leaves room for longer strings */

Initializing Strings

Unlike with regular nonarray variables, you can’t assign a new string to the array like this:

month = "April";                    /* NOT allowed */

You can assign a string to a month with the equals sign only at the time you define the string. If later in the program you want to put a new string into the array, you must either assign it one character at a time or use C’s strcpy() (string copy) function that comes with your C compiler. The following statement assigns a new string to the month:

strcpy(month, "April");          /* Puts new string in month array */

Note : In your programs that use strcpy(), you must put this line after the #include <stdio.h>:

#include <string.h>

Tip : Don’t worry: strcpy() automatically adds a null zero to the end of the string it creates.

Hero1 will be 7 characters (adding null 0!)
char Hero1[] = "Batman";

 

Variables < Prev                                       Next > Preprocessor directives & Header files

Advertisements

2 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s