Globla and local variables

This is a discussion on Globla and local variables within the C Programming forums, part of the General Programming Boards category; Hi, I don't understand this. Why the result is always 0. have a look at this code Code: #include<stdio.h> int ...

  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682

    Globla and local variables

    Hi,
    I don't understand this. Why the result is always 0. have a look at this code

    Code:
    #include<stdio.h>
    
    int count;
    
    void test(int count)
    {
        if(count==10)
            return;
        else
        {
            printf("%d\n",count);
            test(count++);
        }
    }
    
    int main()
    {
        test(0);
        
        getchar();
        return 0;
    }
    As u can see in the function test. It has been called recu incrementing count by 1 every time it call back. why it dost show the value 0 - 9 rather than to be infinite loop with just 0.

    But when it refers int global value count we get the right answer. Thats right, but the local variable count is not why??

    ssharish2005

  2. #2
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    It's printing 0 infinitely because you're not passing the incremented value to test in the recursive call.

    To make it work use
    Code:
    test(++count); /*pre-increment*/
    instead of
    Code:
    test(count++); /*post-increment*/
    EDIT: Note that a new copy of the variable is created at each function call.
    Last edited by noodles; 11-18-2006 at 09:48 PM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    To clarify, "count" the global variable does nothing in this code. As a matter of fact, your compiler should warn you about that, if you were actually to enable warnings. You have a local variable by the same name. The local scope overrides the global scope, using that varaible, not the global one.


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

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    And there's a compiler flag to tell you what Quzah just told you.

    Code:
    $ gcc -Wshadow foo.c
    foo.c:5: warning: declaration of 'count' shadows a global declaration
    foo.c:3: warning: shadowed declaration is here
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Heyy thats was my bad, i dint turn on my warning. Thanks very much guys.

    ssharish2005

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    1. Don't use global variables.
    2. If you must use global variables, then give them a distinctive namespace.

    Like your global would have been called
    int g_count;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Just have one more question. Well i know i shouldn't do this, just to know how to do, thats it

    how do i explictly specific to say that i am refering the global even through global and local varibales names are same. I know there is scope resolution operator. How do we use it in C

    ssharish2005

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    You (*)can't do it in C, since it has no scope resolution operator.

    (*)
    Well I suppose you might be able to fake something with some pre-processor horror, but it sure wouldn't be pretty.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by Salem
    You (*)can't do it in C, since it has no scope resolution operator.

    (*)
    Well I suppose you might be able to fake something with some pre-processor horror, but it sure wouldn't be pretty.
    Thanks very much salem.

    ssharish2005

Popular pages Recent additions subscribe to a feed

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