#include<stdio.h>
#define IC 60
#define TAX_PERCENTAGE 7.5
int main()
{
float tax_decimal = TAX_PERCENTAGE / 100;
float ST = IC * tax_decimal;
float total = IC ST;
printf("Price = %f\nTax = %f\nTotal = %f\n\n",IC, ST,total);
}
this is my code
why does #define TAX_PERCENTAGE stop at zero? because 7.5 / 100 = 0.075 but it just stops at 0. is it because #define is an int?
how do i fix this?
thanks
CodePudding user response:
IC
expands to 60
, which is an int
. Your printf
specifier expects a float
. If you fix that, it should work fine:
#define IC 60.f
#define TAX_PERCENTAGE 7.5f
CodePudding user response:
As others have pointed out, the problem is not in the macros or the arithmetic but in the call to printf
, but it might look a bit odd that you can get completely garbled output if the calculations are right.
When I run
printf("Price = %f\nTax = %f\nTotal = %f\n\n", IC, ST, total);
for example, I get
Price = 4.500000
Tax = 64.500000
Total = 0.000000
while with the correct
printf("Price = %d\nTax = %f\nTotal = %f\n\n", IC, ST, total);
I get
Price = 60
Tax = 4.500000
Total = 64.500000
The thing is, printf
doesn't know about the type of its arguments except through the formatting string. It has no way of knowing, as we don't track type information anywhere in C. So, printf
has to parse the formatting string and then interpret the arguments it got as having the corresponding types. If the formatting string is wrong, printf
will interpret its arguments wrong, and what happens is anyone's guess.
The IC
macro defines a literal integer, so if you want to print it, you must use %d
(or cast to another appropriate type). If you use %f
, then printf
will interpret its arguments as having a float parameter and get the data from wherever it gets its data on your platform.
Where a function gets its argument depends on the platform, but it is not unusual that integers and pointers go in some registers, floats in others, and such. If I am calling printf("...", i, f, f)
with an integer and two floats, the integer might go in one register as the "first integer argument" and the two floats somewhere else. But if I told printf
via the formatting string that I had given it three floating-point arguments, it would look where it expects floats to be, and if that isn't where the integer went, it just won't see the integer argument. Instead, it will see the two floats I gave it and some garbage where it expected a float to be.
Variadic functions are tricky that way. A call that seems totally fine will go wrong if the caller and receiver do not agree on what the arguments are.
For printf
, though, compilers generally know how to check if the formatting string and the arguments match. If you turn on warnings, it should catch it here as well.
CodePudding user response:
Use %d
for displaying IC
value In print because you assigned integer value to IC
.
Use this code it will help.
#include<stdio.h>
#define IC 60
#define TAX_PERCENTAGE 7.5
int main()
{
float tax_decimal = TAX_PERCENTAGE / 100;
float ST = IC * tax_decimal;
float total = IC ST;
printf("Price = %d\nTax = %f\nTotal = %f\n\n",IC, ST,total);
return(0);
}