C编程浮点数?

Not*_*ing 1 c floating-point

所以,我正在阅读C编程.我预订了这个练习:

Write a program which asks the user to enter a dollars-and-cents amount, then displays the amount with 5% added?

方案:

#include <stdio.h>

int main(void) {

    float original_amount;

    printf("Enter an amount: ");
    scanf("%f", &original_amount);
    printf("With tax added: $%.2f\n", original_amount * 1.05f);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我知道是什么.3f意思(在...之后应该有3位数字),但是什么1.05f意思?

urz*_*eit 7

1.05f不表示具有值约为1.05(它是105%= 100%+ 5%)一浮点数.这%.2f是一个格式说明符,是一个非常不同的东西.

与该数字相乘实际上会使该值增加5%(值*1.05 =值*(100%+ 5%)=值+值*5%).

格式说明符出现在printf-like函数的第一个参数中,并告诉函数如何输出与其位置对应的参数.