关于结构程序的困惑

时间:2017-04-10 13:16:32

标签: c struct structure

#include <stdio.h>

main(void)
{
    struct computer
    {
        float cost;
        int year;
        int cpu_speed;
        char cpu_type[16];
    } model;

    printf(“The type of the CPU inside your computer?\n”);
    gets(model.cpu_type);
    printf(“The speed(MHz) of the CPU?\n”);
    scanf(“%d”, &model.cpu_speed);
    printf(“The year your computer was made?\n”);
    scanf(“%d”, &model.year);
    printf(“How much you paid for the computer?\n”);
    scanf(“%f”, &model.cost);

    printf(“Here are what you entered:\n”);
    printf(“Year: %d\n”, model.year);
    printf(“Cost: $%6.2f\n”, model.cost);
    printf(“CPU type: %s\n”, model.cpu_type);
    printf(“CPU speed: %d MHz\n”, model.cpu_speed);

    return 0;
}

以上代码来自Teach Yourself C 24小时内,但在运行时显示出错误。

在书中,还显示了一个输出。

在输出中,模型的成本为$1234.561234.56如何适应%6.2f ...我的意思是%6.2f我们只会获得234.56,对吧?

3 个答案:

答案 0 :(得分:2)

双引号导致杂散错误的字符无效。你可能让他们从其他网站或pdf复制它们。否则,您需要检查键盘设置。

正确的双引号为"

答案 1 :(得分:1)

%6.2f中的6是最小字段宽度。

答案 2 :(得分:0)

您对字符串使用了奇怪的字符:您使用了,但必须使用"

相关问题