• Game Rebels: The site that rewards you for posting
  • Join The Awesome ARK Server!

    Island PVE (Official Rates x2) by Vurh.Club
    Join ARK Server! Join ARK Discord!
  • The Game Shop V2!

    A Complete revamp! Hundreds of FREE games, now available!
    Shop Now!
  • Game Rebels Community

    Welcome to this awesome gaming forum
    Register Now
  • Earn Points to Buy Games!

    The more you post, the more you earn
    Learn More

C++|Lesson 3|Declaring Variables

Discussion in 'Coding Tutorials' started by Fuzed, Jul 3, 2012.

  1. Fuzed

    Fuzed Active Member

    1,452
    0.28
    95
    Now we've learnt about the various data types and variables, we will now move on how to declare and use them.

    Declaring variables
    In C++ we can't just use variables - like in many languages we first need to declare them. This is where we need to know our data types as they are necessary for us to be able to declare the variable.
    So let's have a look at an example
    Code:
    int a;
    To declare a new variable we need first the data type (in this case int) followed by a valid
    variable identifier (in this case a). We can now use the identifier later in in our code.
    if you want to declare more than one identifier for that data type you can do so by using a comma to seperate the identifiers, like this:
    Code:
    int a, b, c;
    it exactly the same as
    Code:
    int a;
    int b;
    int c;
    but it can help decreasing our code size, expecially if we need to declare a large number of identifiers.

    If you check back to the types of data variables you notice that char, short, long and int can be signed or unsigned - and this is dependant on which range of values you will be using.
    To declare either we simply put signed/unsigned before our data type:
    Code:
    unsigned short int a;
    or
    signed int a;
    but most compilers will presume, unless stated, our data type will be signed and so we can just have
    Code:
    int a;
    just removing the signed part completely.

    However you must always declare signed/unsigned (even if you just want signed) when using the char data type, as it exists by itself and can cause problems when compiling as char, without signed/unsigned, is used to store characters. So if storing a numerical value you must declare signed/unsigned.

    You may have noticed that on the data type list we had "short int (short)", this is because "short int" can also be declared as just "short". Short is equivalent to short int and long is equivalent to long int. So both bits of code below have the same function:
    Code:
    short Year;
    short int Year;
    So now this brings us onto signed and unsigned, which may also be used standalone, meaning the same as signed int/unsigned int. So:
    Code:
    unsigned NextYear;
    unsigned int NextYear;
    have the same function.

    Ok so now we're going to test what we've learnt. Here is some source code:
    Code:
    #include <iostream>
    using namespace std;
    int main ()
    {
    int a, b; // declaring the variables a and b
    int result; // declaring result
    
    a = 5; //assigning the value of 5 to a
    b = 2; // assigns the value of 2 to b
    a = a + 1; // increase a by 1
    result = a - b; // result is the signed of value a - b, which in this case is 6-2 which is 4
    
    cout << result; // this prints out the value of result
    
    return 0; //ends the program
    }
    In this case we declare our variables within our main function which makes them local variables, we could declare them anywhere outside our main function. A local variable can only be used within the function, around the {} brackets of the function. They cannot be used in other functions and that's why it may be necessary to declare a global variable.

    a - Local Variable - declared in our main fucntion:
    Code:
    #include <iostream>
    using namespace std;
    int main ()
    {
    int a;
    }
    a - Global Variable - declared ouside the main fucntion:
    Code:
    #include <iostream>
    using namespace std;
    int a;
    int main ()
    {
    }
    Finally if you noticed in our code earlier we declared our variable and signed a value later, however we can declare and asssign a value to the variable at the same time. Either by using equals or just wrapping the value around brackets - as shown below:
    Code:
    int a = 0;
    int a (0);
    Next lesson will be on strings.