Difference between revisions of "Variable"

From Chilipedia
Jump to: navigation, search
(Variable Types)
(The const Specifier)
Line 24: Line 24:
  
 
== 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.

Revision as of 22:40, 11 September 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).

Variable Types

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 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 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 occupy 4 bytes of memory.

bool

bool variables store a Boolean logic value (true or false). They occupy 4 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.