Thread: Static variable usage

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    8

    Static variable usage

    Hi,

    I was exploring static variable by writing code snippets. I tried below code and it ended up throwing error saying "error: storage class specified for parameter 'b'"

    Why static cannot be used in func() ?
    Code:
    int main()
    {
    int a; a=5; func(a); printf("%d",a); return 0;
    } void func(static int b) {
    b=6; printf("%d",b);
    }
    Thanks,
    Shilpa

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your compiler is basically telling you that the static keyword cannot be used in the declaration of a parameter.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    You can't do that. If you want the value in b static, make it so within the body of the function.

    Code:
    void func(int b)
    {
        static int c = b;
        printf("%d", c);
    }
    Last edited by kermit; 12-27-2013 at 10:48 AM.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    8
    Yes it cannot be used. But is there any reason why it can't be used ?

    Thanks,
    Shilpa

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The short answer is that the standard says so. The static keyword is only applicable to definitions of variables and functions. It is not applicable to function parameters (or arguments).

    As to why .... all function parameters in C are passed by value, meaning that a copy of the value supplied by the caller is what the function receives. The static keyword tells the compiler that something has a single fixed representation (for example, a specific location in memory). The two concepts are mutually exclusive - it does not make sense for a function parameter which will always be a copy of what the caller supplies to have a fixed representation.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by shilpac View Post
    Yes it cannot be used. But is there any reason why it can't be used ?
    Maybe if you explain what you are trying to accomplish conceptually someone can suggest an alternative. In other words, explain what do you want func(b) to do for some integer b?

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by shilpac View Post
    Yes it cannot be used. But is there any reason why it can't be used ?
    The issue is where the variable are stored. Static variables normally go into the same storage area as globals. Function parameters are normally located on the stack or in registers.

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    8
    grumpy and rcgldr,

    Thank you for the info, that made it clear.

    c99tutorial,

    No requirement. Just learning

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static Variable and Static method
    By codewriter in forum C++ Programming
    Replies: 6
    Last Post: 03-25-2012, 07:49 AM
  2. Doubt on usage of static...
    By sanddune008 in forum C Programming
    Replies: 7
    Last Post: 05-11-2009, 08:09 AM
  3. Static variable usage
    By shwetha_siddu in forum C Programming
    Replies: 1
    Last Post: 04-02-2009, 12:33 AM
  4. Replies: 8
    Last Post: 01-19-2009, 07:42 PM
  5. Replies: 6
    Last Post: 12-13-2007, 08:20 PM