C Misunderstanding Of Variable Assignments.

This is a discussion on C Misunderstanding Of Variable Assignments. within the C Programming forums, part of the General Programming Boards category; I have never programmed before, and am starting with some C using tutorials such as howstuffworks.com In this particular tutorial: ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    13

    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.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    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.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 12:57 PM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 11:07 AM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21