You've got the basics right, but you're understanding it wrong.
Let's examine the code a bit:
> char name[10];
> float rate;
> float hours;
> float tax;
Here, you are "declaring" variables. All this does is tell the compiler to make a variable called "name" that is of type "char", and is an array of 10 total characters. (And for "rate", "hours", "tax", you're telling the compiler to make three variables with those names, that are of type "float".
What this does is set aside a block of memory for those variables.
You can also declare variables, and assign them a value in one line:
> float total=rate*hours;
This is what you are doing here. However, what value does "total" get? Well, it gets rate*hours.
This means:
"Whatever the value of 'rate' is, multiply that by whatever the value of 'hours' is, _AT THIS POINT IN TIME_, and put this value in the variable called "total".
Later on, if I did:
total = 1;
I've just set the value of total to 1. Any value that was there before is gone.
Now, one more thing to look at:
float myVariable;
Here, I've declared a variable. What value does it have?
It could be anything at this point in time. Its value will be whatever the value of that piece of memory was before I called it. This could be anything. I haven't given any value to "myVariable", so I am unsure what is in it.
This is why you always want to assign values to your variables before you use them.
You should do something like this:
Code:
char name[10];
/*
Notice, you can declare multiple variables
of the same type, on the same line, like I
did, by seperating them by a comma.
*/
float rate, hours, tax, total;
printf("Enter your hourly rate\n$");
scanf(" %f", &rate);
printf("Enter your hours worked\n");
scanf(" %f", &hours);
printf("Enter your tax rate in decimal format\n");
scanf(" %f", &tax);
/*
Now, calculate the total before we display it.
*/
total = rate * time;
/*
Now we can display it.
*/
printf("Your total gross wage for this time period is %.2f dollars\n", total);
Quzah.