6.8 Variables in C


6.8 Variables in C++

If a constant is created and assigned a value, the value cannot be changed. This is because it's constant data. You can try to change it by writing something like this:

      const int Age = 27;      Age = 53; 

But the compiler will not permit it. For the value to change, it must be a variable. Declaring a variable in C++ is much like declaring a constant. When declaring variables, the keyword const can be omitted. The compiler assumes a variable is being declared unless the const keyword is used. Variables are much more common in programming.

      //This is a comment. Notice how the declaration of these variables is      //the same as constants, but with no const keywords      int Age = 27;      int Friend_Age = Age;      bool Is_Human = true;      float Pi = 3.14;      char Letter = 'a';      //These are all variables. They can be changed after declaration too, as      //below      Is_Human = false;      Letter = 'b';      Age = 27 + 3;      Friend_Age = Age + 3; 




Introduction to Game Programming with C++
Introduction to Game Programming with C++ (Wordware Game Developers Library)
ISBN: 1598220322
EAN: 2147483647
Year: 2007
Pages: 225
Authors: Alan Thorn

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net