Variables

Before we look at variables, we have to look at something called "comments"

Comments

A comment is something that is put in your code that won't be executed, it can be used to prevent unwanted code execution or to annotate code.
There are two types of comments: single-lined and multi-lined.
Single-line comments start with "//" and end at the end of the line.

Example:
                                
// This is a single-line comment
int x = 5; // This is a single-line comment and also a declaration and initialization of a variable.
                                
                            

Multi-lined comments start with "/*" and end with "*/".
Example:
                                
/* this is a multi-line comment
comment continues
until
here*/co/*comments can be placed here.*/ut<<"ends";
                                
                            

Variables

Variables are containers for storing data values. The values they store are not fixed but the type of the value is. They are declared by specifying the type and the name, an optional value is provided. Variable naming rules include:

  1. English alphabets and underscores may be included anywhere.
  2. Numbers may be included, but not the first character of the name.
  3. Other characters are prohibited.
  4. Variable names may not conflict with C++ keywords and existing names, they include:
    for,while,do,if,else,int,short,long,double,float,bool,true,false,vector,string,array,...
    and stuff that are previously declared in the program
  5. Variable names are case-sensitive, so "x" and "X" are different variables.
  6. It is advised that variable names do not start with underscores, as those are commonly used by internal functions and variables, e.g. __x,__gcd,__gnu_pbds,__cplusplus,_GLIBCXX_IOSTREAM

Common Types

For integers, we typically used int and long long that can store integer values in the range \([-2^{31},2^{31}-1(\text{equal to }2147483647)]\) and \([-2^{63}(\approx 9\cdot10^{18}),2^{63}-1]\), respectively.

A machine stores integers by the following manner:

  1. Obtain binary representation of number. If number is positive, add 0 in front of it, else add 1, this added number is the sign bit.
  2. If the number is positive, store it directly.
  3. If the number is negative, flip all bits of the binary representation (except the sign bit) and add 1 to it. Store the result.

An int uses 32 bits while a long long uses 64. You can also declare unsigned version of them by adding unsigned before the type. the unsigned versions, with no sign bit, are capable of extending the upper and lower bounds to twice as usual.

Other integer types include long, which can store the same things as an int, and short, which uses only 16 bits.

Quiz: what is the range capable for a short to store?

A short can store \([-2^{15},2^{15}-1]\), which is \([-32768,32767]\).

Characters are stored in the char type, of range \([-128,127]\). There is an unsigned version. They are stored by their ASCII values, a table can be found here. A single character can be represented by the character itself in single quotes.

Real numbers (decimals) are stored in float, double, and long double types. They do not have unsigned versions. Computers store decimals in three parts: the sign, the exponent, and the fractional part. They correspond to \(\text{fractional}\cdot2^\text{exponent}\). A float uses 1 bit for sign, 8 bits for exponent, and 23 bits for fractional. A double uses 1 bit for sign, 11 bits for exponent, and 52 bits for fractional. As for a long double ... It depends on what type of machine you store. If on a 32-bit machine, it is the same as a double. If on a x87, it occupies 80 bits. If on a 64-bit, it uses 128 bits. The 80- and 128-bit version both use 15 bits for exponent. Note that the exponent can be negative, so 1 of the exponent section is actually the sign bit!

Type Storage Range Precision
float \(\pm1.18\times10^{-38} \text{ to } \pm3.4\times10^{38} \text{ and } 0.0\) 6-9, usually 7 decimal places
double \(\pm2.23\times10^{-308} \text{ to } \pm1.8\times10^{308} \text{ and } 0.0\) approximately 15 decimal places
long double (80-bit) \(\pm3.36\times10^{-4932} \text{ to } \pm1.18\times10^{4932} \text{ and } 0.0\) approximately 18-21 decimal places
long double \(\pm3.36\times10^{-4932} \text{ to } \pm1.18\times10^{4932} \text{ and } 0.0\) approximately 33-36 decimal places

Notes: above data are for 64-bit devices, on 32-bit devices, upper and lower bounds for ints and long longs will half.

Variable declaration

A variable is like a box for data. In order to store data in the box, you need to have the box first. The process of getting this box is known as declaration. Declaration is in the format Typename VarName.

Example:
                                
                                    int age; //declare variable of type int and name age
                                    double weight;
                                    char grade;
                                    long long studentID;
                                    bool isStudent;
                                    float pi;
                                    double e;
                                    long double phi;
                                    short shortNumber;
                                    char character;
                                    unsigned int uNumber;
                                    unsigned char uCharacter;
                                    unsigned short uShortNumber;
                                    unsigned long long uLongNumber;
                                
                            

You need to put stuff in the box now. This is known as assignment. Assignment is done in the format VarName = Value. Note that the spaces (whitespace) is not necessary. Declaration and assignment can be put together, like TypeName VarName = Value

Example:
                                
                                    int age = 25;
                                    age = age + 1; //Student has gone older
                                    /*age++ is an alternate for age=age+1
                                    so is age-- for age=age-1
                                    ++age and --age also works*/
                                    double weight = 75.5;
                                    char grade = 'A';
                                    long long studentID = 1234567890123456789;
                                    bool isStudent = true;
                                    float pi = 3.14159;
                                    double e = 2.71828;
                                    long double phi = 1.61803;
                                    short shortNumber = 123;
                                    char character = 'c';
                                    unsigned int uNumber = 1234567890;
                                    unsigned char uCharacter = 255;
                                    unsigned short uShortNumber = 65535;
                                    unsigned long long uLongNumber = 18446744073709551615;
                                    /*
                                    age=age+1 returns a value of age+1, which means when using
                                    int b=(a=a+1);
                                    it will assign the value of a+1 to b. 
                                    b=a++ will assign a to b, and then execute a=a+1,
                                    b=++a will execute a=a+1, and then assign a to b.
                                    */
                                
                            

Constants

Constants are variables that cannot be changed and are declared with const before the variable type. It can be used ike any other variable with one exception: it cannot be changed and must be assigned together with initialization. Constant are usually declared with names in all caps.

Example Usage:
                                
                                    #include<iostream>
                                    using namespace std;
                                    int main(){
                                        const int N=10;//declare constant N with value 10
                                        int sz=2*N+10;//size is a function, so its name may not be used
                                        //const int ERR;//Compile error will be caused by uncommenting this line
                                        //N+=10;//so will this
                                        return 0;
                                    }
                                
                            

Output Variables

Outputting a variable is just like doing so to a number. For example:

                        
                            int MyAge=12;
                            cout << myAge << endl;
                        
                    

Now head to the next section to learn about input!