34
C Strings

Given the choice, an Objective-C programmer will always choose to work with NSString objects rather than C strings. However, sometimes we don’t have a choice. The most common reason we end up using C strings? When we access a C library from within our Objective-C code. For example, there is a library of C functions that lets your program talk to a PostgreSQL database server. The functions in that library use C strings, not instances of NSString.

char

In the last section, we talked about how a byte could be treated as a number. We can also treat a byte as a character. As mentioned earlier, there are many different string encodings. The oldest and most famous is ASCII. ASCII (American Standard Code for Information Interchange) defines a different character for each byte. For example, 0x4b is the character ‘K’.

Create a new C Command Line Tool and name it yostring. In this program, you are going to list some of the characters in the ASCII standard. Edit main.c:

#​i​n​c​l​u​d​e​ ​<​s​t​d​i​o​.​h​>​

i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​c​h​a​r​ ​x​ ​=​ ​0​x​2​1​;​ ​/​/​ ​T​h​e​ ​c​h​a​r​a​c​t​e​r​ ​'​!​'​

 ​ ​ ​ ​w​h​i​l​e​ ​(​x​ ​<​=​ ​0​x​7​e​)​ ​{​ ​/​/​ ​T​h​e​ ​c​h​a​r​a​c​t​e​r​ ​'​~​'​
 ​ ​ ​ ​ ​ ​ ​ ​p​r​i​n​t​f​(​"​%​x​ ​i​s​ ​%​c​​n​"​,​ ​x​,​ ​x​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​x​+​+​;​
 ​ ​ ​ ​}​

 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

You may be wondering Hey, a byte can hold any one of 256 numbers. You just printed out 94 characters. What happened to the to the rest? It is important to realize that ASCII was written to drive old teletype-style terminals that printed to paper instead of to a screen. For example, the number 7 in ASCII makes the terminal bell ring. In fact, the characters 0 - 31 in ASCII are unprintable control codes. Number 32 is the space character. Number 127 is the delete – it causes the previous character to disappear. What about characters 128 – 255? ASCII only uses 7 bits. There is no ASCII character for the number 128.

You can use ASCII characters as literals in code. Just put them inside single quotes. Change your code to use these:

i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​c​h​a​r​ ​x​ ​=​ ​'​!​'​;​ ​/​/​ ​T​h​e​ ​c​h​a​r​a​c​t​e​r​ ​'​!​'​

 ​ ​ ​ ​w​h​i​l​e​ ​(​x​ ​<​=​ ​'​~​'​)​ ​{​ ​/​/​ ​T​h​e​ ​c​h​a​r​a​c​t​e​r​ ​'​~​'​
 ​ ​ ​ ​ ​ ​ ​ ​p​r​i​n​t​f​(​"​%​x​ ​i​s​ ​%​c​​n​"​,​ ​x​,​ ​x​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​x​+​+​;​
 ​ ​ ​ ​}​

 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

Build it and run it.

The non-printable characters can be expressed using escape sequences that start with . You’ve already used for the newline character. Here are some common ones:

Table 34.1  Common escape sequences

new line
tab
'single-quote
"double-quote
null byte (0x00)
\backslash

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset