Difference between revisions of "Variable"

From Chilipedia
Jump to: navigation, search
(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...")
 
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
Variables are like little boxes to store data in, typically numbers. What's in the box?!
+
Variables are like little boxes to store data in and read data from, typically numbers. What's in the box?!
  
 
== 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 Names ==
 +
Variables names (the same as any other type of user-defined symbol) can contain any number, letter, and the underscore character.
 +
 
 +
-They cannot start with a number.
 +
 
 +
-Case matters (<code>DirtayBoi69</code> is not the same symbol as <code>dirtayboi69</code>).
 +
 
 +
-It is recommended to not use underscore for the name prefix because some of them are reserved <code>_DirtayBoi69</code>. But you can use them as you will in the middle <code>Dirtay_Boi_69</code>.
 +
 
 +
= Variable Types =
 +
 
 +
== Variable Size ==
 +
 
 +
Everytime you use a variable, it has to be stored in memory. And the size depends of the type you are using.
 +
 
 +
Variables size can be found by using the sizeof function.
 +
 
 +
The size of most types are not fixed and may depend of the architecture (32 or 64 bits) but every type is guaranteed to have a minimum size to store the desired value.
 +
<syntaxhighlight lang="cpp" line="1" >
 +
int main()
 +
{
 +
    std::cout << sizeof(int);
 +
};
 +
</syntaxhighlight>
 +
As an example, a variable of type int will occupy 4 bytes of memory, but not always as said before.
 +
So the console will usually output 4 for this code.
 +
In the next paragraphs we are talking about the average size and those numbers are valid for most computers. But don't assume they will always be the same.
 +
 
 +
=== Integral types ===
 +
Integral types are variables that store integers.  There are signed and unsigned versions of each integral type and the built-in or simple types are char, short, int/long, and long long/uint64_t.  Generally, for the signed versions, you do not have to specify signed, but for unsigned versions; unsigned char, unsigned short, unsigned int, unsigned long long, you do have to specify.  Unsigned integrals are integers that don't have negative numbers, they go from 0 to double + 1 their signed counterparts in the positive direction.
 +
 
 +
As an example, signed char goes from -128 to 127 whereas unsigned char goes from 0 to 255.
 +
=== char ===
 +
<code>char</code> variables store an integer ranging from -128 to 127.  They occupy 1 byte of memory and are most commonly used for narrow text/string manipulation. 
 +
=== short===
 +
<code>short</code> variables store an integer ranging from -32768 to 32767.  They usually occupy 2 bytes of memory.  An alias for short is wchar_t and is used for wide character text and strings used to extend support for languages with more characters than the char range can handle.
 +
=== long long ===
 +
<code>long long</code> variables store an integer ranging from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.  They usually  occupy 8 bytes in memory.  The type long long can also be referred to as int64_t because it is 64 bits long.
 
=== int ===
 
=== int ===
<code>int</code> variables store an integer (non-fractional number) ranging from -2147483648 to 2147483647. They occupy 4 bytes of memory.
+
<code>int</code> variables store an integer (non-fractional number) ranging from -2147483648 to 2147483647. They usually occupy 4 bytes of memory.
 +
=== bool ===
 +
<code>bool</code> variables store a Boolean logic value (true or false). They usually occupy 1 bytes of memory. The <code>bool</code> type is used to control the operation of flow control statements, such as the <code>if</code> statement.
  
 
== The const Specifier ==
 
== The const Specifier ==
 +
Main article: [[const]]
 +
 
You can specify that a variable's value cannot be changed by declaring it like this: <code>const int blazeItFgt = 420;</code>. 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 <code>const</code>, you need to initialize it during creation because you cannot assign to it afterwards.
 
You can specify that a variable's value cannot be changed by declaring it like this: <code>const int blazeItFgt = 420;</code>. 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 <code>const</code>, you need to initialize it during creation because you cannot assign to it afterwards.

Latest revision as of 10:49, 11 October 2017

Variables are like little boxes to store data in and read data from, 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 Names

Variables names (the same as any other type of user-defined symbol) can contain any number, letter, and the underscore character.

-They cannot start with a number.

-Case matters (DirtayBoi69 is not the same symbol as dirtayboi69).

-It is recommended to not use underscore for the name prefix because some of them are reserved _DirtayBoi69. But you can use them as you will in the middle Dirtay_Boi_69.

Variable Types

Variable Size

Everytime you use a variable, it has to be stored in memory. And the size depends of the type you are using.

Variables size can be found by using the sizeof function.

The size of most types are not fixed and may depend of the architecture (32 or 64 bits) but every type is guaranteed to have a minimum size to store the desired value.

int main()
{
    std::cout << sizeof(int);
};

As an example, a variable of type int will occupy 4 bytes of memory, but not always as said before. So the console will usually output 4 for this code. In the next paragraphs we are talking about the average size and those numbers are valid for most computers. But don't assume they will always be the same.

Integral types

Integral types are variables that store integers. There are signed and unsigned versions of each integral type and the built-in or simple types are char, short, int/long, and long long/uint64_t. Generally, for the signed versions, you do not have to specify signed, but for unsigned versions; unsigned char, unsigned short, unsigned int, unsigned long long, you do have to specify. Unsigned integrals are integers that don't have negative numbers, they go from 0 to double + 1 their signed counterparts in the positive direction.

As an example, signed char goes from -128 to 127 whereas unsigned char goes from 0 to 255.

char

char variables store an integer ranging from -128 to 127. They occupy 1 byte of memory and are most commonly used for narrow text/string manipulation.

short

short variables store an integer ranging from -32768 to 32767. They usually occupy 2 bytes of memory. An alias for short is wchar_t and is used for wide character text and strings used to extend support for languages with more characters than the char range can handle.

long long

long long variables store an integer ranging from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. They usually occupy 8 bytes in memory. The type long long can also be referred to as int64_t because it is 64 bits long.

int

int variables store an integer (non-fractional number) ranging from -2147483648 to 2147483647. They usually occupy 4 bytes of memory.

bool

bool variables store a Boolean logic value (true or false). They usually occupy 1 bytes of memory. The bool type is used to control the operation of flow control statements, such as the if statement.

The const Specifier

Main article: const

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.