is there any variable type that we can change it's value in global scope?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: is there any variable type that we can change it's value in global scope?

  1. #1
    Join Date
    Apr 2009
    Posts
    605

    is there any variable type that we can change it's value in global scope?

    is there any variable type(or with another keyword) that we can change it's value in global scope?

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: is there any variable type that we can change it's value in global scope?

    I'm not sure I'm understanding the question. Any non-const variable defined as being in global scope in a single program file can be changed anywhere in that program file after the definiton providing the variable hasn't been re-defined within a local block.

    Do you mean that you want a global variable defined in one program file to be able to be accessed in another program file that is part of the same compilation project/solution?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Apr 2009
    Posts
    605

    Re: is there any variable type that we can change it's value in global scope?

    Quote Originally Posted by 2kaud View Post
    I'm not sure I'm understanding the question. Any non-const variable defined as being in global scope in a single program file can be changed anywhere in that program file after the definiton providing the variable hasn't been re-defined within a local block.

    Do you mean that you want a global variable defined in one program file to be able to be accessed in another program file that is part of the same compilation project/solution?
    example:

    Code:
    int a;
    a=100;
    i have seen something like these with 'static' variables

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: is there any variable type that we can change it's value in global scope?

    Static variables are most commonly used in a function. The variable defined as static is local to the function but retains its value between each call to the function.

    Code:
    void fun1()
    {
    static int cnt = 0;
    
         cout << "cnt value is " << cnt++ << endl;
    }
    Every time you call fun1, cnt is incremented even though it is local to the function. See
    http://msdn.microsoft.com/en-us/libr...vs.110%29.aspx
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Apr 2009
    Posts
    605

    Re: is there any variable type that we can change it's value in global scope?

    Quote Originally Posted by 2kaud View Post
    Static variables are most commonly used in a function. The variable defined as static is local to the function but retains its value between each call to the function.

    Code:
    void fun1()
    {
    static int cnt = 0;
    
         cout << "cnt value is " << cnt++ << endl;
    }
    Every time you call fun1, cnt is incremented even though it is local to the function. See
    http://msdn.microsoft.com/en-us/libr...vs.110%29.aspx
    sorry but outside the function

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: is there any variable type that we can change it's value in global scope?

    Like this?

    Code:
    int a;
    
    void f1()
    {
       a = 2;
    }
    
    int main()
    {
       a = 1;
       f1();
       cout << "a " << a << endl;
       return 0;
    }
    static used at the global level simply means that the variable can't be used outside of that 'code unit'.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center