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

——大家好,我是挖坑大王。。。

我不得不这样讲,因为我又挖了个大坑。

某个群里说要组织翻译外文书籍,我二话不说报了,这种事情挺有意义的,既锻炼英语,又能学知识,何乐而不为呢。。。因为这坑也不是很大。。。

我负责的是第四章,关于字符串和输入输出的。

今天搞了一部分,先发到这吧。



原文:


Chapter 4. Character Strings and Formatted Input/Output

You will learn about the following in this chapter:

  • Function:

strlen()

  • Keywords:

const

  • Character strings
  • How character strings are created and stored
  • How you can use scanf() and printf() to read and display character strings
  • How to use the strlen() function to measure string lengths
  • The C preprocessor's #define directive and ANSI C's const modifier for creating symbolic constants

This chapter concentrates on input and output. You'll add personality to your programs by making them interactive and using character strings. You will also take a more detailed look at those two handy C input/output functions, printf() and scanf(). With these two functions, you have the program tools you need to communicate with users and to format output to meet your needs and tastes. Finally, you'll take a quick look at an important C facility, the C preprocessor, and learn how to define and use symbolic constants.

 

Introductory Program

By now, you probably expect a sample program at the beginning of each chapter, soListing 4.1 is a program that engages in a dialog with the user. To add a little variety, the code uses the new C99 comment style.

Listing 4.1. Thetalkback.c Program
 
// talkback.c -- nosy, informative program
 
#include 
 
#include       // for strlen() prototype
 
#define DENSITY 62.4     // human density in lbs per cu ft
 
int main()
 
{
 
    float weight, volume;
 
    int size, letters;
 
    char name[40];         // name is an array of 40 chars
 
 
 
    printf("Hi! What's your first name?\n");
 
    scanf("%s", name);
 
    printf("%s, what's your weight in pounds?\n", name);
 
    scanf("%f", &weight);
 
    size = sizeof name;
 
    letters = strlen(name);
 
    volume = weight / DENSITY;
 
    printf("Well, %s, your volume is %2.2f cubic feet.\n",
 
            name, volume);
 
    printf("Also, your first name has %d letters,\n",
 
            letters);
 
    printf("and we have %d bytes to store it in.\n", size);
 
 
 
    return 0;
 
}
 

 

Running talkback.c produces results such as the following:

 
 
 

 

 
Hi! What's your first name?
 
Sharla
 
Sharla, what's your weight in pounds?
 
139
 
Well, Sharla, your volume is 2.23 cubic feet.
 
Also, your first name has 6 letters,
 
and we have 40 bytes to store it in.
 

 

Here are the main new features of this program:

·        It uses an array to hold a character string. Here, someone's name is read into the array, which, in this case, is a series of 40 consecutive bytes in memory, each able to hold a single character value.

·        It uses the%s conversion specification to handle the input and output of the string. Note thatname, unlike weight, does not use the & prefix when used withscanf(). (As you'll see later, both &weight and name are addresses.)

·        It uses the C preprocessor to define the symbolic constantDENSITY to represent the value 62.4.

·        It uses the C functionstrlen() to find the length of a string.

The C approach might seem a little complex compared with the input/output of, say, BASIC. However, this complexity buys a finer control of I/O and greater program efficiency, and it's surprisingly easy once you get used to it.

Let's investigate these new ideas.




章节4.字符串和格式化输入/输出
你将在这一章节中学到以下内容:
函数:
strlen()
关键字:
const
字符串
字符串是如何创建和储存的
如何用scanf()和printf()来读取和显示字符串
如何用strlen()函数测量字符串长度
用C的预处理#define指令和ANSI C的const修饰语来创建字符常量


这一章重点在输入和输出。你可以通过让程序充满交互性及使用字符串来个性化你的程序。你也将更加详细地了解这两个方便的输入/输出函数:printf()和scanf()。通过这两个函数,你就拥有了按你需要和喜好来和用户交互和编排输出的工具。最后,你将快速了解一个重要的C工具,C预处理器,并学习如何定义和使用字符常量。








导引程序


直到目前,你可能希望每一章开头都能有一个示例程序,所以Listing 4.1是一个能够与用户进行对话的程序。为了更加的灵活多彩,代码使用了新的C99标准注释风格。
Listing 4.1. The talkback.c Program


// talkback.c – 好管闲事的提供信息的程序


#include


#include      // strlen()函数的头文件


#define DENSITY 62.4     // 人类密度,单位:英镑/每立方英尺(lbs per cu ft)


int main()


{


    float weight, volume;


    int size, letters;


    char name[40];         // 40字符长度的name数组






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


    scanf("%s", name);


    printf("%s, what's your weight in pounds?\n", name);


    scanf("%f", &weight);


    size = sizeof name;


    letters = strlen(name);


    volume = weight / DENSITY;


    printf("Well, %s, your volume is %2.2f cubic feet.\n",


            name, volume);


    printf("Also, your first name has %d letters,\n",


            letters);


    printf("and we have %d bytes to store it in.\n", size);






    return 0;


}


执行talkback.c产生如下结果:




Hi! What's your first name?


Sharla


Sharla, what's your weight in pounds?


139


Well, Sharla, your volume is 2.23 cubic feet.


Also, your first name has 6 letters,


and we have 40 bytes to store it in.






这个程序的主要的新特征是:
程序使用了一个数组来保存字符串。在这里,某人的名字被读入这个数组。该数组在内存中为连续的40个字节,每个字节都能保存一个字符。
程序使用了转换说明符(conversion specification)%s来处理输入和输出。注意,name,不像weight一样,它不需要在scanf()里面使用&前缀。(正如你稍后所会见到的,&weight和name都是地址。)
程序使用了C预处理器来定义符号常量DENSITY为数值62.4。
程序使用了strlen()函数来获取字符串的长度。

C输入输出的方法与BASIC相比似乎有点复杂。但是,这种复杂性换来的是对输入输出出色的控制以及极高效的程序,在你习惯它后你就会发现他是惊人的简单。





后记:

排版排的很难看。。。

我才不会说我实在看不懂会去翻网上的中文版的。。。

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