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

// 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() 参数[数组变量]不用带前缀&(未明所以然)
-----------------------------------------------
*/


猜你喜欢

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