zoukankan      html  css  js  c++  java
  • C Primer Plus 5th 翻译 第四章:字符串和格式化输入/输出(二)

    没想到翻译这么费劲。。。

    今天花了近两个小时才翻译了一些。

    里面有些图,发出来还得上传,不是什么重要的图片,我就不发出来了。。。

    第一次翻译,可能有些渣请见谅。



    先上原文:

    Character Strings: An Introduction

    A character string is a series of one or more characters. Here is an example of a string:

    
    
    "Zing went the strings of my heart!"
    
    

    The double quotation marks are not part of the string. They inform the compiler that they enclose a string, just as single quotation marks identify a character.

    Type char Arrays and the Null Character

    C has no special variable type for strings. Instead, strings are stored in an array of type char. Characters in a string are stored in adjacent memory cells, one character per cell, and an array consists of adjacent memory locations, so placing a string in an array is quite natural (see Figure 4.1).

    Figure 4.1. A string in an array.

    graphics/04fig01.gif


    Note that Figure 4.1 shows the character \0 in the last array position. This is the null character, and C uses it to mark the end of a string. The null character is not the digit zero; it is the nonprinting character whose ASCII code value (or equivalent) is 0. Strings in C are always stored with this terminating null character. The presence of the null character means that the array must have at least one more cell than the number of characters to be stored.

    Now just what is an array? You can think of an array as several memory cells in a row. If you prefer more formal language, an array is an ordered sequence of data elements of one type. This example creates an array of 40 memory cells, or elements, each of which can store one char-type value by using this declaration:

    
    
    char name[40];
    
    

    The brackets after name identify it as an array. The 40 within the brackets indicates the number of elements in the array. The char identifies the type of each element (see Figure 4.2).

    Figure 4.2. Declaring a variable versus declaring an array.

    graphics/04fig02.gif


    Using a character string is beginning to sound complicated! You have to create an array, place the characters of a string into an array, one by one, and remember to add \0 at the end. Fortunately, the computer can take care of most of the details itself.

    Using Strings

    Try the program in Listing 4.2 to see how easy it really is to use strings.

    Listing 4.2. The praise1.c Program
    /* praise1.c -- uses an assortment of strings */
    
    #include <stdio.h>
    
    #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);
    
    
    
        return 0;
    
    }
    
    

    The %s tells printf() to print a string. The %s appears twice because the program prints two strings: the one stored in the name array and the one represented by PRAISE. Running praise1.c should produce an output similar to this:

    
    
    What's your name?
    
    Hilary Bubbles
    
    Hello, Hilary. What a super marvelous name!
    
    

    You do not have to put the null character into the name array yourself. That task is done for you by scanf() when it reads the input. Nor do you include a null character in the character string constant PRAISE. We'll explain the #define statement soon; for now, simply note that the double quotation marks that enclose the text following PRAISE identify the text as a string. The compiler takes care of putting in the null character.

    Note (and this is important) that scanf() just reads Hilary Bubble's first name. After scanf() starts to read input, it stops reading at the first whitespace (blank, tab, or newline) it encounters. Therefore, it stops scanning for name when it reaches the blank between Hilary and Bubbles. In general, scanf() is used with %s to read only a single word, not a whole phrase, as a string. C has other input-reading functions, such as gets(), for handling general strings. Later chapters will explore string functions more fully.






    译文:


    字符串简介
    一个字符串就是一系列的单个或多个字符。下面是一个字符串的例子:


    "Zing went the strings of my heart!"


    两个双引号并不是这个字符串的一部分。他们告诉编译器一个信息:它们之间是一个字符串,正如单引号定义一个字符一样。


    char数组类型以及空字符
    C没有专门为字符串设定的变量类型。相反的,字符串储存在类型为char的数组里。字符串中的字符都储存在相邻的内存单元,一个字符占一个内存单元,数组是有相邻的内存位置组成的,所以把字符串放在一个数组里面是很自然的(见图4.1)。


    图4.1 字符串是一个数组
     


    注意,图4.1里面数组最后一个位置显示字符\0。这是个空字符(null charater),C使用空字符表示一个字符串的结束。空字符并不是一个数字;这是个非打印字符,它的ASCII值是0。字符串在C里面储存时通常以空字符结束。空字符的存在使得这个数组至少要比要储存的字符数多1。
    那么到底什么是数组?你可以把它想象成排成一排的几个内存单元。如果你喜欢更正式点的描述,数组就是一个按顺序排的同一类型的基础数据。这个示例程序利用下面的声明创建了一个拥有40个内存单元(或者说基础数据)的数组,里面每个内存单元可以储存一个char类型的值:
    Char name[40];
    在name后面的方括号表示这是一个数组。里面的数字40代表这个数组的长度。char说明了这个数组储存的数据类型(见图4.2)。


    图4.2 声明变量与数组
     
    使用字符串类型开始变得复杂起来了!你必须创建一个数组来一个一个地放字符串里的字符,别忘记还得在末尾添加\0字符。幸运的是,计算机能够细心地处理这些细节。


    使用字符串
    试着运行Listing 4.2的程序并看看它能如何轻易地使用字符串。
    Listing 4.2.  praise1.c 程序


    /* praise1.c – 使用不同类型的字符串 */


    #include <stdio.h>


    #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);






        return 0;


    }


    %s说明printf()输出的是一个字符串。%s出现两次因为程序输出了两个字符串:一个储存在name数组里面,另一个被PRAISE说代表。运行praisel.c将会输出如下:
    What's your name?


    Hilary Bubbles


    Hello, Hilary. What a super marvelous name!


    你不需要手动把空字符放入name字符数组里面。scanf()在读去输入时会帮你完成这个任务,。你也不用再字符串常量PRAISE末尾放置空字符。我们将很快就解释#define语句;目前只需要简单记住PRAISE后面的双引号表示它们之间的文本是一个字符串。编译器会帮你把空字符插进数组里的。
    注意(这是很重要的):scanf()只读取了Hilary Bubble的名字。在scanf()读取输入之后,它会在遇到第一个空白符号(如空格、制表符或者换行)之后结束读取。因此,它遇到Hilary和Bubbles中间的空格后,便停止了读取字符给name数组。通常,scanf()使用%s来读取单个字符,而不是一个词组或者是一串字符串。C有其他读取输入的函数比如gets()来处理一般的字符。后面的几张将会更加深入的探究字符串函数。


    (未完待续)

  • 相关阅读:
    2016年 CodePen 最热门的前端代码 Top 100
    HttpClientHelper
    .net core mvc 简易登录
    senparc 第三方授权回调,保存授权信息到文件
    盛派微信Sdk .Net Core 初始化注册服务信息
    分享一个gif截图工具
    C# 单例模式
    获取微信AccessToken,保存在内存中,过期后重新获取
    .NET Core WebAPI Swagger使用
    .net Core 读取配置文件参数
  • 原文地址:https://www.cnblogs.com/java20130723/p/3212181.html
Copyright © 2011-2022 走看看