1. 程式人生 > >程序清單4.3_praise2.c程序_《C Primer Plus》P63

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

C Primer Plus

// 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 也計算在內. */


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