-3
#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;
}

The above code is from Teach Yourself C in 24 hours,but it shows stray errors while being run.

In the book,an output has also been displayed.

In the output,the cost of the model is $1234.56. How can 1234.56 fit into %6.2f... I mean for %6.2f we'll get only 234.56,right?

Alex Lop.
  • 6,810
  • 1
  • 26
  • 45
Susan
  • 19
  • 1
  • 1
  • 5

3 Answers3

2

You have invalid character for double quote and which are causing the stray error. You probably have them copied them from some other site or pdf. Otherwise you need to check your keyboard setting.

The correct double quote is ".

taskinoor
  • 45,586
  • 12
  • 116
  • 142
1

The 6 in %6.2f is the minimum field width.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • Isn't it like this-we count 6 places and then 2 backwards(for 2 in 6.2) and then a place backward again for the decimal point? _ _ _ _. _ _ So there'll be 3 places before the decimal and 2 after it???If not,how actually do we count the places?? – Susan Apr 10 '17 at 13:32
  • 1
    `printf` never truncates the whole part of a floating point number. @Susan, please ask this as a separate question and you'll get more a detailed answer. –  Apr 10 '17 at 14:43
0

You used strange chars for strings: you used but you must use "

LPs
  • 16,045
  • 8
  • 30
  • 61