——大家好,我是挖坑大王。。。
我不得不这样讲,因为我又挖了个大坑。
某个群里说要组织翻译外文书籍,我二话不说报了,这种事情挺有意义的,既锻炼英语,又能学知识,何乐而不为呢。。。因为这坑也不是很大。。。
我负责的是第四章,关于字符串和输入输出的。
今天搞了一部分,先发到这吧。
原文:
Chapter 4. Character Strings and Formatted Input/Output You will learn about the following in this chapter:
strlen()
const
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 ProgramBy 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 #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.字符串和格式化输入/输出
C输入输出的方法与BASIC相比似乎有点复杂。但是,这种复杂性换来的是对输入输出出色的控制以及极高效的程序,在你习惯它后你就会发现他是惊人的简单。
后记:
排版排的很难看。。。
我才不会说我实在看不懂会去翻网上的中文版的。。。