Difference between revisions of "Variable"
(Created page with "Variables are like little boxes to store data in, typically numbers. What's in the box?! == How to Declare a Variable == You declare/create a variable by stating its type and...") |
(→How to Declare a Variable) |
||
Line 2: | Line 2: | ||
== How to Declare a Variable == | == How to Declare a Variable == | ||
− | You declare/create a variable by stating its type and then stating its name: <code>int myVar;</code>. You can also give an initial value for the variable using the assignment operator: <code>int myVar = 69;</code>. | + | You declare/create a variable by stating its type and then stating its name: <code>int myVar;</code>. You can also give an initial value for the variable during creation using the assignment operator: <code>int myVar = 69;</code>. |
== Variable Types == | == Variable Types == |
Revision as of 18:40, 6 August 2016
Variables are like little boxes to store data in, typically numbers. What's in the box?!
How to Declare a Variable
You declare/create a variable by stating its type and then stating its name: int myVar;
. You can also give an initial value for the variable during creation using the assignment operator: int myVar = 69;
.
Variable Types
int
int
variables store an integer (non-fractional number) ranging from -2147483648 to 2147483647. They occupy 4 bytes of memory.
The const Specifier
You can specify that a variable's value cannot be changed by declaring it like this: const int blazeItFgt = 420;
. Doing so can protect your code from unintentional bugs and is generally considered a fucking good practice called 'const correctness'. Note that when you declare a variable as const
, you need to initialize it during creation because you cannot assign to it afterwards.