程序清单4.3_praise2.c程序_《C Primer Plus》P63

// praise2.cpp : 定义控制台应用程序的入口点。
//
/* praise2.c */

/*
    时间:2018年06月10日 23:20:39
    代码:程序清单4.3_praise2.c程序_《C Primer Plus》P63
    目的:strlen()准确给出字符串中字符(含空格与标点);
        Sizeof 运算符则比前者大1,(\0:起终止作用的空字符)
*/
#include "stdafx.h"
#include "string.h"    /* 提供 strlen() 函数的原型 */
#define PRAISE "Waht a super marvelous name!"

int _tmain(int argc, _TCHAR* argv[])
{
    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);                /* occupies(占领) */    
    printf("The phrase of praise has %d letters", 
            strlen(PRAISE));                        /* phrase(短语) */
    printf(" and occupies %d memory cells.\n", sizeof PRAISE);
    getchar();
    getchar();
    getchar();
    getchar();
    getchar();
    getchar();
    getchar();
    getchar();
    getchar();
    getchar();
    getchar();
    getchar();


    return 0;
}

/*
    在VS2010中运行结果:
----------------------------------------------------------------
What's your name?
Morgan Buttercup
Hello, Morgan. Waht 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.
----------------------------------------------------------------
    google 翻译如下:

你叫什么名字?
摩根 毛茛
摩根,你好。 多么美妙的名字!
您的6个字母的名称占用40个存储单元。
赞美短语有28个字母,占据29个记忆细胞。
----------------------------------------------------------------
    总结:
        strlen("Waht a super marvelous name!")=28
        sizeof("Waht a super marvelous name!")=29
        这是因为 sizeof 把用来标志字符串结束的不可见的空字符 \0 也计算在内.
*/


猜你喜欢

转载自blog.51cto.com/13555061/2127876
今日推荐