Variable Declaration

This is a discussion on Variable Declaration within the C++ Programming forums, part of the General Programming Boards category; If i declare buf[32] in function(), it means it will be redeclared everytime i call the function? In this case ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    796

    Variable Declaration

    If i declare buf[32] in function(), it means it will be redeclared everytime i call the function?
    In this case would it be better to declare it globally?

    Code:
    void function()
    {
        char buf[32] ="Hello";
        cout<<buf<<endl;
    }
    
    int main()
    {
        for(int i)0;i<10;i++) function();
        return 0;
    }
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    Quote Originally Posted by Ducky View Post
    If i declare buf[32] in function(), it means it will be redeclared everytime i call the function?
    Yes.

    In this case would it be better to declare it globally?
    Not unless it's a bottleneck.
    Although you could make it static. Then it would only be created and initialized once and still only be visible from inside the function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    Thanks for the explanation.

    Do you mean static global or like this?
    Also static would mean that i couldnt change its value, right?

    Code:
    void function()
    {
        static char buf[32] ="Hello";
        cout<<buf<<endl;
    }
    Compiler MSVC++ 2013 with Code::Blocks.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    Like you have it is good, I think, depending on what you want done with it. Leaving it as static does not mean you would be unable to change the value, simply that the array would only be created/initialized once and any changes that were made in between instances of the function being called would remain in place from instance to instance.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    Thank you hk_mp5kpdw!
    Compiler MSVC++ 2013 with Code::Blocks.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Generally you shouldn't worry about this at all. In this case the call to cout will most likely take 99% of the time spent in this function.

    Creating the array pretty much means instructions to store the string "hello" in a block of memory. In this case it needn't be more expensive than assigning two integers ("hell" and "o\0").
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error in global variable declaration
    By JFonseka in forum C Programming
    Replies: 4
    Last Post: 04-11-2008, 10:50 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. variable declaration in H file
    By aleccher in forum C Programming
    Replies: 5
    Last Post: 04-02-2003, 05:18 PM
  5. variable declaration style
    By jdinger in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2002, 01:32 PM

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