码迷,mamicode.com
首页 > 其他好文 > 详细

程序清单4.1_talkback.c程序_《C Primer Plus》P60

时间:2018-06-09 00:55:09      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:C Primer Plus

// talkback.cpp : 定义控制台应用程序的入口点。 // /* talkback.c -- 一个能为您提供一些信息的对话程序 */ /*     时间:2018年06月08日 23:30:05     代码:程序清单4.1_talkback.c程序_《C Primer Plus》P60     目的:调用函数加头文件,及预先处理器定义常量,scanf()-前缀& */ #include "stdafx.h" #include "string.h"        // 提供 strlen() 函数的原型 #define DENSITY 62.4    // 人的密度(单位是:英镑/每立方英尺) int _tmain(int argc, _TCHAR* argv[]) {     float weight, volume;     int size, letters;     char name[40];        // name 是一个有 40 个字符的数组     printf("Hi! What's your first name?\n");     scanf("%s", name);    // 这里不需要给 name 加前缀 & (此处未明所以然)     printf("%s, what's your weight in pounds?\n",name);     scanf("%f", &weight);    // 这里务必要在 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);     getchar();     getchar();          return 0; } /*     在VS2010中运行结果: -------------------------------------------- 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. --------------------------------------------     google 翻译如下: 嗨! 你的名字是什么? Sharla Sharla,你的磅数是多少? 139 那么,Sharla,你的体积是2.23立方英尺。 另外,你的名字有6个字母, 我们有40个字节来存储它。 ---------------------------------------------     总结:         1>.头文件 #include "string.h" 为调用--     --strlen() 函数而设;         2>.预处理器 #define DENSITY --     --定义了代表值 62.4 的符号常量 DENSITY;         3>.scanf() 参数[数组变量]不用带前缀&(未明所以然) ----------------------------------------------- */


程序清单4.1_talkback.c程序_《C Primer Plus》P60

标签:C Primer Plus

原文地址:http://blog.51cto.com/13555061/2126628

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!