Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Variable Declaration & Initialization :: C++

Variable Declaration & Initialization :: C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    valikac
    wrote on last edited by
    #1

    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

    P V 2 Replies Last reply
    0
    • V valikac

      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

      P Offline
      P Offline
      Peter Godec
      wrote on last edited by
      #2

      See C++ FAQ Lite. HTH, --peter

      1 Reply Last reply
      0
      • V valikac

        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

        V Offline
        V Offline
        valikac
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups