Learning C => Floating Point Numbers and Variables#
float#
With the float type, we can now declare variables to store decimal numbers, whereas before we only had int for integer types.
To print a float value using printf, we need to use "%f". In this example.c, "%.2f" is used to accurately display the value with two decimal places.
scanf()#
Additionally, we use the scanf() function to read input from the keyboard.
#include <stdio.h>
int main(void) {
float weight; // weight
float value; // gold
printf("Are you worth your weight in platinum?\n");
printf("Let's check it out.\n");
printf("Please enter your weight in pounds: ");
// reads input
scanf("%f", &weight);
value = 1700.0 * weight * 14.5833;
printf("Your weight in platinum is worth $%.2f.\n", value);
printf("You are easily worth that! If platinum prices drop,\n");
printf("eat more to maintain your value.\n");
getchar();
getchar();
return 0;
}
(There shouldn't be any problems, right? ^^)
This article is synchronized and updated to xLog by Mix Space.
The original link is https://www.xingbest.fun/posts/programming/C-Study(1)