# C Misunderstanding Of Variable Assignments.

• 09-07-2009
Krash005
C Misunderstanding Of Variable Assignments.
I have never programmed before, and am starting with some C using tutorials such as howstuffworks.com

In this particular tutorial: HowStuffWorks "How C Programming Works"

A list of Fahrenheit to Celsius conversions are made every 10 degrees F, adding in a line at 98.6 F, then continuing.

I do not understand the given code (the final piece of code on that page) on the loop that adds the 98.6 F line with no bugs.

Code:

``` #include <stdio.h> int main() {     float a, b;     a = 0;     b = -1;     while (a <= 100)     {         if ((a > 98.6) && (b < 98.6))         {             printf("%6.2f degrees F = %6.2f degrees C\n",                 98.6, (98.6 - 32.0) * 5.0 / 9.0);         }         printf("%6.2f degrees F = %6.2f degrees C\n",             a, (a - 32.0) * 5.0 / 9.0);         b = a;         a = a + 10;     }     return 0; }```
If b starts at -1, then is attached to a at the end of the loop, and a goes up by 10 each loop, then there is never a point at which a > 98.6 and b< 98.6.

On the end of the 10th loop, a is 90 and goes up by 10 to 100. But starting at -1, b should be going up to 99, which is higher than 98.6. So why does that meet the requirement of a > 98.6 and b< 98.6

I feel that I am misunderstanding the point of b being in the code, because it doesnt seem to matter what b is assigned to in the beginning, as long as it is assigned to a at the end. Where am I going wrong in my thinking?

Thank you so much for your time. I will get better.
• 09-07-2009
tabstop
b gets the previous value of a; so after the first loop b is 0 and a is 10; at the end of the next loop b is set to the current value of a (10) and then a is increased to 20. And so on.
• 09-07-2009
Krash005
Oh I see!

b is assigned to a, making b 0, but not till the end of the first loop.
But, why do they assign b to -1 in the beginning? It seems arbitrary, and maybe that's the point.

The program still works with assigning b to any number.
• 09-07-2009
tabstop
In this case, yes. You can get in trouble for using variables before assigning anything to them; if we didn't assign a value to b before starting, then the first time through the loop b < 98.6 might be true and it might not be true, and it would be an error (logical, not syntax) to try to check it before it had a value.