Thread: Variable Declaration

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

    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;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    932
    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;
    }
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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
    932
    Thank you hk_mp5kpdw!
    Using Windows 10 with Code Blocks and MingW.

  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, 05:45 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. variable declaration in H file
    By aleccher in forum C Programming
    Replies: 5
    Last Post: 04-02-2003, 06:18 PM
  5. variable declaration style
    By jdinger in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2002, 01:32 PM