Redefining a Variable but using the same name? (Beginner question)

This is a discussion on Redefining a Variable but using the same name? (Beginner question) within the C Programming forums, part of the General Programming Boards category; A few pointers on your first code... Code: int Change = round(change * 100); /* Initialise */ int quarters = ...

  1. #16
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,384
    A few pointers on your first code...
    Code:
        int Change = round(change * 100);
    
        /* Initialise */
        int quarters = 0;
        int dimes = 0;
        int nickels = 0;
        int pennies = 0;
    
        // how many quarters are needed
        /* Use curly braces to your advantage */
        if (Change >= 25)
        {
            quarters = (Change / 25);
            Change = (Change%25);
        }
    
        // how many dimes are needed
        if (Change >=10)
        {
            dimes = (Change / 10);
            Change = (Change%10);
        }
    
        if (Change >= 5)
        {
            nickels = (Change / 5);
            Change = (Change%5);
        }
    
        if (Change >= 1)
        {
            /* Don't divide by 1 */
            pennies = Change;
        }
    Fact - Beethoven wrote his first symphony in C

  2. #17
    Registered User
    Join Date
    Feb 2013
    Posts
    23
    Thank you for your response, Click_here!

    I initialized the value of coins to be 0 and it works now! For my first code, I initialized the values too, and it works! Yahooooo!!! I'm ecstatic right now.

    I have two questions though:

    1. In my first code, why do I need to initialize quarters, dimes, pennies, and nickels to 0? Why is that necessary? Because my line of thinking as to why that is unnecessary is: within my IF, I'm assigning a value to quarters ( ex: quarters = (Change / 25); ). It's not as if I'm not defining quarters.

    2. Basically, what I've gathered from this is that EVEN if I redefine a variable using the same name within a function such as IF (in the first code), for example, that carries over, even if it's inside curly brackets. For example, in my first code, using user input 0.37 for example, in the first if, Change is 37, and then Change becomes 37%25 (which is 12), and so in the second if, it's basically
    if (Change >= 10) where Change is 12
    but I'm confused by c99tutorial's comment about how if it's defined within curly brackets and within a function such as IF, it does NOT carry over and it is a local variable confined to only within the curly brackets.

    Thank you so much!
    Last edited by Rubik; 02-18-2013 at 09:46 PM. Reason: Additional thank you

  3. #18
    Registered User
    Join Date
    Feb 2013
    Posts
    23
    Never mind! EVERYTHING IS RESOLVED. Thank you!
    Last edited by Rubik; 02-18-2013 at 10:36 PM.

  4. #19
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by Rubik View Post
    1. In my first code, why do I need to initialize quarters, dimes, pennies, and nickels to 0? Why is that necessary? Because my line of thinking as to why that is unnecessary is: within my IF, I'm assigning a value to quarters ( ex: quarters = (Change / 25); ). It's not as if I'm not defining quarters.
    What is the value of that variable if the IF statement is false? Not every one of your if statements will execute every time (or else the "if" would be unnecessary). For example, if you have 22 cents, it never enters the block that would set quarters to anything.

    2. Basically, what I've gathered from this is that EVEN if I redefine a variable using the same name within a function such as IF (in the first code), for example, that carries over, even if it's inside curly brackets. For example, in my first code, using user input 0.37 for example, in the first if, Change is 37, and then Change becomes 37%25 (which is 12), and so in the second if, it's basically
    if (Change >= 10) where Change is 12
    but I'm confused by c99tutorial's comment about how if it's defined within curly brackets and within a function such as IF, it does NOT carry over and it is a local variable confined to only within the curly brackets.

    Thank you so much!
    Be very, very, very careful about terminology. You're simply changing a variable's value; that's not really called "redefining". You're not "using the same name", you're using the same VARIABLE.

    In your code, there is one and only one variable named Change. Its value may be reassigned by various expressions, but you are never creating a new variable. You're just putting different values into the single variable you already created.

    What he means about scope is that a variable only exists within the set of curly braces in which it is declared.

    Here's an example. I'm going to make some assumptions about how the compiler will store data in memory that may or may not be accurate, but I'm trying to give a concrete example to reinforce concepts - this is only one possible way the compiler might store these values, but it's an illustration.

    Code:
    void myFunc()
    {
        int x; // (1)
        x = 10; // (2)
        for (int y = 0; y < 10; y++) // (3)
        {
            x = x+1; // (4)
        }
        x = x * 3; // (5)
    }
    At point 1, x is declared. Since it is declared within the outermost curly braces in this code snippet, it exists throughout this entire block of code (the entire myFunc() function).

    At that point, the running program allocates 4 bytes of memory to store an integer variable (x). The current value is unknown (specifically, it's whatever was last in this memory location). X is declared but undefined.

    At point 2, x is defined. Whatever the old value of those 4 bytes is is now gone, and the value '10' is stored instead.

    At point 3, y is declared and defined. 4 bytes of memory are now devoted to storing y. This variable is treated as being declared within the for loop's scope.

    At point 4, x is changed. This is not a new variable named x. It's the same 4 bytes of memory we set aside to hold an integer back in step (1). We're just changing the contents from 10, to 11, to 12, ... to 20.

    At point 5, y no longer exists - those 4 bytes are no longer dedicated to y, because it has gone out of scope (it only existed within the scope of the for loop). x still exists, because we're inside the braces in which x was declared (back in step 1). We're now going to update its value yet again, from 20 to 60.

    Total, we've set the value of x 12 times, but there is still just one and only one x variable. The entire code utilized exactly two variables, one variable named x and another y.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unused Variable - Beginner Question
    By intex in forum C Programming
    Replies: 4
    Last Post: 11-24-2012, 06:25 PM
  2. Beginner, help with loop and variable
    By keeganstarr in forum C Programming
    Replies: 16
    Last Post: 05-28-2012, 07:31 PM
  3. Replies: 10
    Last Post: 03-28-2010, 01:35 AM
  4. Question about redefining functions in derived class
    By Sharke in forum C++ Programming
    Replies: 2
    Last Post: 08-05-2009, 11:48 AM
  5. Replies: 4
    Last Post: 11-15-2005, 01:13 PM

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