same variable ???

This is a discussion on same variable ??? within the C Programming forums, part of the General Programming Boards category; HI, I am using the DJGPP compiler for C found at http://www.delorie.com/djgpp/ I have the following problem. I am learning ...

  1. #1
    Registered User actionbasti's Avatar
    Join Date
    Dec 2002
    Posts
    48

    same variable ???

    HI,

    I am using the DJGPP compiler for C found at http://www.delorie.com/djgpp/

    I have the following problem. I am learning C right now and there is this example of a simple program, it creates a main function and two additional fuctions, which should demonstrate that variables are not carrying the same value in different functions (here() & there())
    The program is supposed to print out:

    The value of v here is 30
    The value of v there is 1234 //some other random number BUT NOT 35

    when i run the program though it DOES print:

    The value of v here is 30
    The value of v there is 35

    code:



    #include <stdio.h>

    void here(void);
    void there(void);

    void main()
    {
    here();
    there();
    }

    void here()
    {
    int v;

    v=6*5;
    printf("The value of v here is %i\n",v);
    }

    void there()
    {
    int v;

    v+=5
    printf("The value of v there is %i\n",v);
    }


    Why does it print 35 if the book tells me that it won't definitly do so??? And why does the variable v stores the value 30 along into the other function if i should not happen, according to the book (C for dummies vol 1, by the way)???

    thanks...

  2. #2
    Patent Pending GSLR's Avatar
    Join Date
    Sep 2001
    Posts
    134
    this works

    Code:
    #include <stdio.h>
    
    void here(void);
    void there(void);
    
    int main()
    {
    here();
    printf("\n");
    there();
    system ("pause");
    return 0;
    }
    
    void here()
    {
    int v;
    v=6*5;
    printf("The value of v in function here is %d\n",v);
    }
    
    void there()
    {
    int v;
    v+=5;
    printf("The value of v in function there is %d\n",v);
    }

    Well hope that works for you
    there are a few changes that you should notice
    And To All Those Opposed, WELL !!!
    >Deleted< " Looks like a serial no."

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This message is for both of you: READ THE FAQ! Use code tags, or don't post code.

    The reason the book tells you that the value of 'v' will be undefined is that when you use a variable, unless static, that has not been initialized, the variable will hold some arbitrary value.

    Consider this:

    You are interested in buying a plot of land. You ask me to buy you some. I do. You now have land that you've never seen, and you want to build a house. Now you ask me "what's on the land?"

    The same exact thing happens when you use an uninitialized variable. You have no idea what is there when you ask the computer to allocate space for a variable. It says, "Ok, here it is." Then you go to say, ok, show me what's there ... and you have absolutely no idea what the value of is there.

    Now again, consider you saying, "Buy me some land, then level the whole place."

    Now you know that your land is both land, and level so you can build on it.

    Your variable, when you initialize it before using it, is exactly the same. You know you have a variable, and you also know what's there.

    Damn I'm smooth.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Patent Pending GSLR's Avatar
    Join Date
    Sep 2001
    Posts
    134
    did i not use code tags ??????

    i thought i did

    unless the faq is bs ing me

    Code:
     
    <<<<<<<is this not a tag ?????????????????????????????????????????????????? ????????????????????????????????????????????
    And To All Those Opposed, WELL !!!
    >Deleted< " Looks like a serial no."

  5. #5
    Registered User actionbasti's Avatar
    Join Date
    Dec 2002
    Posts
    48
    I changed it to your code, considering int main() and %d , but it still prints 35 and treats v as a GOBAL variable and not a LOCAL one !

    could that be because of the compiler that I use, or is there still something else wrong ?



    thanks ...

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by GSLR
    did i not use code tags ??????

    i thought i did
    Yeah, you're right. You actually did use them. You just didn't indent anything, so it looked like you didn't bother to use code tags.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by actionbasti
    I changed it to your code, considering int main() and %d , but it still prints 35 and treats v as a GOBAL variable and not a LOCAL one !

    could that be because of the compiler that I use, or is there still something else wrong ?

    thanks ...
    No. It's doing exactly what it should be doing. It's giving you a value that you have no idea why it is that value. You're probably getting the same stack space as the pervious function, so oddly enough, the variable is in the same spot for each function. Thus, when you look at that spot in the stack, it increments whatever is there.

    Speculation, but that very well may be what's happening.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Patent Pending GSLR's Avatar
    Join Date
    Sep 2001
    Posts
    134
    point noted
    And To All Those Opposed, WELL !!!
    >Deleted< " Looks like a serial no."

  9. #9
    Registered User actionbasti's Avatar
    Join Date
    Dec 2002
    Posts
    48
    sorry, i was the one who didnt use them tags actually... cause i didnt read the ! READ ME FIRST ! thingy...

    anyway... what you told me quzah, makes sense, but i still dont get why i have a global variable ( which is usually declared outside any function) as a local one ! ?!?!

  10. #10
    Registered User actionbasti's Avatar
    Join Date
    Dec 2002
    Posts
    48
    ok ok ok .....


    look i compiled this with onther c compiler... called DMC ...
    and there it gave me back a peace of land ... that i didnt know what s on ... :-) .. the numbers were 30 and this 423724...
    then i went back to compile it with djgpp and i was back with 30 and 35 ...

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by actionbasti
    ok ok ok .....


    look i compiled this with onther c compiler... called DMC ...
    and there it gave me back a peace of land ... that i didnt know what s on ... :-) .. the numbers were 30 and this 423724...
    then i went back to compile it with djgpp and i was back with 30 and 35 ...
    It's not global. It's just two variables in the same program, called the same thing because they're in different functions.

    In your case, the compiler you're probably using, optimizes differently than the other compiler. Not all compilers handle things the same.

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Patent Pending GSLR's Avatar
    Join Date
    Sep 2001
    Posts
    134
    oddly enough i get the exact same values over here after entering and compiling the prog

    30
    35


    strange !!!


    Dev C++
    win 2000 pro
    And To All Those Opposed, WELL !!!
    >Deleted< " Looks like a serial no."

  13. #13
    Registered User actionbasti's Avatar
    Join Date
    Dec 2002
    Posts
    48
    so if i understood you right, then the DJGPP compiler might use the same memory location for the v variable ... and this had previously 30 stored in there...
    but another compiler ( such as DMC ) might use not the same but a totally different memory location , and in that one the number 4723... happened to be still in there....

    is that what you mean?

    thx btw

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    MSVC++ for me gives 30 and -858993455.

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Patent Pending GSLR's Avatar
    Join Date
    Sep 2001
    Posts
    134
    Thats what he means

    still strange my first compile got the same results

    try just declaring the variable in the second function and not initialising it
    And To All Those Opposed, WELL !!!
    >Deleted< " Looks like a serial no."

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

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  2. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 08:32 AM
  3. Replies: 2
    Last Post: 04-12-2004, 02:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 02:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 04:11 AM

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