C Primer Plus 5th 翻译 第四章:字符串和格式化输入/输出(三)

放假在家真的效率很低啊,今天扫墓后就没怎么做事。。。

不说了,上文章,然后睡觉。。。

图懒得挂上去,见谅。



原文:

Strings Versus Characters

The string constant "x" is not the same as the character constant'x'. One difference is that 'x' is a basic type (char), but"x" is a derived type, an array of char. A second difference is that"x" really consists of two characters, 'x' and '\0', the null character (seeFigure 4.3).

Figure 4.3. The character 'x' and the string "x".



The strlen() Function

The previous chapter unleashed the sizeof operator, which gives the size of things in bytes. Thestrlen() function gives the length of a string in characters. Because it takes one byte to hold one character, you might suppose that both would give the same result when applied to a string, but they don't. Add a few lines to the example, as shown in Listing 4.3, and see why.

Listing 4.3. The praise2.c Program
/* praise2.c */

#include 

#include       /* provides strlen() prototype */

#define PRAISE "What a super marvelous name!"

int main(void)

{

    char name[40];



    printf("What's your name?\n");

    scanf("%s", name);

    printf("Hello, %s. %s\n", name, PRAISE);

    printf("Your name of %d letters occupies %d memory cells.\n",

         strlen(name), sizeof name);

    printf("The phrase of praise has %d letters ",

         strlen(PRAISE));

    printf("and occupies %d memory cells.\n", sizeof PRAISE);



    return 0;

}


If you are using a pre-ANSI C compiler, you might have to remove the following line:

 
  
#include 


The string.h file contains function prototypes for several string-related functions, includingstrlen(). Chapter 11, "Character Strings and String Functions," discusses this header file more fully. (By the way, some pre-ANSI Unix systems usestrings.h instead of string.h to contain declarations for string functions.)

More generally, C divides the C function library into families of related functions and provides a header file for each family. For example,printf() and scanf() belong to a family of standard input and output functions and use thestdio.h header file. The strlen() function joins several other string-related functions, such as functions to copy strings and to search through strings, in a family served by thestring.h header.

Notice that Listing 4.3 uses two methods to handle longprintf() statements. The first method spreads one printf() statement over two lines. (You can break a line between arguments toprintf() but not in the middle of a string—that is, not between the quotation marks.) The second method uses twoprintf() statements to print just one line. The newline character (\n) appears only in the second statement. Running the program could produce the following interchange:

 
  
What's your name?

Morgan Buttercup

Hello, Morgan. What a super marvelous name!

Your name of 6 letters occupies 40 memory cells.

The phrase of praise has 28 letters and occupies 29 memory cells.


See what happens. The array name has 40 memory cells, and that is what thesizeof operator reports. Only the first six cells are needed to hold Morgan, however, and that is what strlen() reports. The seventh cell in the arrayname contains the null character, and its presence tells strlen() when to stop counting.Figure 4.4 illustrates this concept.

Figure 4.4. The strlen() function knows when to stop.

graphics/04fig04.gif


When you get to PRAISE, you find that strlen() again gives you the exact number of characters (including spaces and punctuation) in the string. Thesizeof operator gives you a number one larger because it also counts the invisible null character used to end the string. You didn't tell the computer how much memory to set aside to store the phrase. It had to count the number of characters between the double quotes itself.

One other point: The preceding chapter used sizeof with parentheses, but this example doesn't. Whether you use parentheses depends on whether you want the size of a type or the size of a particular quantity. Parentheses are required for types but are optional for particular quantities. That is, you would use sizeof(char) or sizeof(float) but can use sizeof name or sizeof 6.28. However, it is all right to use parentheses in these cases, too, as insizeof (6.28).

The last example used strlen() and sizeof for the rather trivial purpose of satisfying a user's potential curiosity. Actually, however,strlen() and sizeof are important programming tools. For example,strlen() is useful in all sorts of character-string programs, as you'll see inChapter 11.

Let's move on to the #define statement.



译文:

 

字符串vs.字符

字符串常量”x”和字符’x’是不一样的。其中一个不同是字符’x’是一个基础类型(char),但是字符串”x”是一个派生出来的类型,一个char型的数组。第二个不同是“x”已经包含了两个字符,’x’和空字符’\0’(见图4.3)。

图4.3 字符‘x’和字符串“x“

 

strlen()函数

上章提到了sizeof运算符,它能够以字节为单位给出数据的长度。strlen()函数则以字符为单位给出了字符串的长度。因为每个字符占用一个字节,所以你可能会认为两个函数对同一个字符串所给出的结果是一样的。但事实并非如此。像Listing 4.3一样,在示例程序里面加上几行看看为什么会这样。

Listing 4.3.程序praise2.c

/* praise2.c*/

 

#include

 

#include      /*提供strlen()原型*/

 

#definePRAISE "What a super marvelous name!"

 

intmain(void)

 

{

 

    char name[40];

 

 

 

    printf("What's your name?\n");

 

    scanf("%s", name);

 

    printf("Hello, %s. %s\n", name,PRAISE);

 

    printf("Your name of %d lettersoccupies %d memory cells.\n",

 

         strlen(name), sizeof name);

 

    printf("The phrase of praise has %dletters ",

 

         strlen(PRAISE));

 

    printf("and occupies %d memorycells.\n", sizeof PRAISE);

 

 

 

    return 0;

 

}

 

如果你使用ANSI C之前的编译器,你可能要删除下面这行:

#include

 

string.h文件包含了一些与字符串有关的函数的原型,包括strlen()。第11章的“字符串与字符串函数“更加全面讨论了这个头文件。(顺便一提,一些ANSI C之前的unix系统使用头文件strings.h而不是string.h来包含字符串函数的声明。)

通常,C把C函数库分成多个相关函数的系列,并为它们各提供了头文件。比如说printf()和scanf()属于标准输入输出函数系列,都使用stdio.h头文件。strlen()函数和其他几个与字符串有关的函数,比如说复制和查找字符串函数,都属于同一个系列,都由string.h提供原型。

注意Listing4.3使用了两个方法来处理很长的printf()语句。第一个方法是把语句分割成两行。(你可以在参数之间断开printf()语句,但你不可以在字符串的中间断开,比如在两个括号间断开。)第二个方法是使用两个printf()语句来输出一行。换行符(\n)只出现在第二个语句。运行程序将产生如下的交互效果:

 

What's yourname?

 

MorganButtercup

 

Hello,Morgan. What a super marvelous name!

 

Your name of6 letters occupies 40 memory cells.

 

The phraseof praise has 28 letters and occupies 29 memory cells.

 

看看发生了什么。sizeof运算符告诉我们数组name占用了40个内存单元。但strlen()告诉我们只需要前6个用来存放Morgan。数组里的第七个内存单元包含了空字符来让strlen()函数停止计算。图4.4描绘了这个概念。

图4.4 strlen()函数知道什么时候停止计算

 

对于PRAISE,你会发现strlen()又给出了字符串里精确的字符数(包括空格和标点符号)。而sizeof则给出数目比strlen()给出的大1,因为它把最后的那个标志着字符串结束的空字符也算了进去。你不需要告诉电脑需要分配多少内存来储存语句。它必须自己计算双引号之间的字符数。

另一点是:之前的章节使用了带有圆括号的sizeof,但本例却没有。是否使用圆括号取决于你要获取一个类型的大小还是具体量的大小。圆括号对于类型是必须的, 而对于具体量则是可选择的。这意味着你可以使用sizeof(char)或者sizeof(float),但也可以使用sizeof name或者sizeof 6.28。但是,在一些情况下使用圆括号会更好,比如说sizeof (6.28)。

最后一个示例中使用strlen()和sizeof只是为了满足用户潜意识中的好奇心。但事实上,两个函数都是很重要的编程工具。举个例子,strlen()在各种字符串程序中是很有用的,正如你将在第11章看到的一样。

让我们移步去看#define语句吧。

 



(未完待续)


你可能感兴趣的:(======翻译======,+C,Primer,Plus,5th,第四章翻译,翻译,C,Primer,Plus,5th)