This is just a basic program. We will enter the name of the book(1 char name), price(float) and the number of pages(int) ant output should be the same as input, but it's not.
#include<stdio.h>
int main()
{
struct book
{
char name;
float price;
int pages;
};
struct book b1,b2,b3;
printf("Enter names,prices & no. of pages of 3 books\n");
scanf("%c %f %d",&b1.name,&b1.price,&b1.pages);
scanf("%c %f %d",&b2.name,&b2.price,&b2.pages);
scanf("%c %f %d",&b3.name,&b3.price,&b3.pages);
printf("And this is what you entered\n");
printf("%c %f %d\n",b1.name,b1.price,b1.pages);
printf("%c %f %d\n",b2.name,b2.price,b2.pages);
printf("%c %f %d\n",b3.name,b3.price,b3.pages);
return 0;
}
This is what you've entered. Floating points are more of approximations than actual values. That's why when you've entered 123.134
, computer rounded it to a nearest possible value, 123.124003
. It works for engineering, graphic and statistics, where such minor disrepancies are rounded before presenting back to humans. But when you want exact values, floating point variables are completely unsuitable.
In finances, we store 10 dollars as "1000 cents", in ints. Trying to use float or double for monetary values is a big no-no.
What you want is a decimal type, but there is no such type in C.