Variable Declaration & Initialization :: C++

This is a discussion on Variable Declaration & Initialization :: C++ within the C++ Programming forums, part of the General Programming Boards category; Hi. Conventionally speaking, do you declare and initialize variables at the beginning of a function or loop or right before ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    Variable Declaration & Initialization :: C++

    Hi.

    Conventionally speaking, do you declare and initialize variables at the beginning of a function or loop or right before usage.

    For example:

    int main()
    {
    char temp;

    cin >> temp;
    cout << "1234... & " << temp;

    cin >> temp;
    cout << temp;

    int x;
    cin >> x;
    cout << temp << " & " << x;
    return 0;
    }

    The arbitrary code above depicts declaration of variables right before usage (int x).

    Which way is most convention is arguably better?

    Thanks,
    Kuphryn

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    It's all a matter of preference. C++ gives you the ability to declare variables immediately before using them, but the common practice is to declare all variables at the start of the block so as to make the program easier to follow. This practice comes from C where you have no choice but to declare variables at the beginning of the block.

    However, there are exceptions such as for loops, it's far easier to declare and initialize a counter variable that has no purpose outside of the loop in the initialization section of the for loop.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Thanks!

    As of today, I have begun to declare variable when:

    1) there is something to initialize it with

    int x = ...functure return, something added/subtract, etc, to other variables

    2) right before its usage

    int x = 3;
    cout << x;

    The only time I declare a variable at the top:

    int x = 0;

    is when it will be referenced to.

    function(int &aboveX)
    cout << aboveX;

    So aboveX is a reference to x.

    I began learning and practicing C++ about four months ago. I find something really interesting. I find that when you first learn C++ in college and reading beginner C++ books, the professor and author typically recommend declaring variables right at the top. They make it like it is imperative that we do that or the program may not work. Now, I feel that the programmer is in control of the program and that he/she can declare variables wherever is appropriate. As I mentioned, I feel it is much easier to recognize variable when the program declare them accord to the examples above.

    Kuphryn

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Excellent, it's a good idea to build your own personal coding style early on. Just try to stay open minded because you might find later on that your chosen style uses some bad techniques. I'm not saying that's what is happening to you, but it happened to me a few times before I found something comfortable and correct.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Stoffel posted something interesting. He responded to this same question and said:
    "All memory for all variables in a function are allocated at the beginning of a function in a single step, period. You cannot allocate new stack variables in the middle of a function."

    http://www.gamedev.net/community/for...topic_id=71967

    Is that a true statement?

    Kuphryn

  6. #6
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Yes, but if you don't want to take his/her word for it take a look at the assembly generated using different methods.
    zen

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Okay thanks.

    It is an intrigue fact. If that is true, then declaring variable right before using them just makes it easier to read the code. Performancewise, doing so only increase performance for ADT.

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. compile error about member variable initialization
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 12-04-2007, 11:55 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, 02:32 PM

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