• 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 2|Data types and variables

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

  1. Fuzed

    Fuzed Active Member

    Ok so now we have learned to code C++ and made our first Hello World app we can move on.

    Let's move on to variables and data types.

    Variables are letters or words or whatever else you define them as within your code (we shall get onto declaring variables in a minute). The word you use for the variable is called an indentifier. So for example you could have some code such as this
    a = 9;
    b = 5;
    a = a + 1;
    result = a - b;
    So you have 2 variables in this code a and b. You assign the value of 9 to a, you assign 5 to b, then a increases by 1. The result (another identifier) is then (a+1)-(b) = (9+1)-(5) = 10-5 = 5. So the result is assigned 5.

    Do remember though:
    - identifiers (a, b and result) cannot be completely anything - you cannot use reserved words
    asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete,
    do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto,
    if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register,
    reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template,
    this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void,
    volatile, wchar_t, while

    - C++ is case sensetive so if you use an identifier such as 'a' then do not then start using 'A' as the program deals with cases as if they were sepearte letters/words.

    Data Types
    To declare our variable we need to decide what data type it is, here is a table to help you:
    Name - Description- Size* - Range*
    char - Character or small integer. - 1byte - signed: -128 to 127 unsigned: 0 to 255
    short int (short) - Short Integer. - 2bytes - signed: -32768 to 32767 unsigned: 0 to 65535
    int - Integer. - 4bytes - signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295
    long int (long) - Long integer. - 4bytes - signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295
    bool - Boolean value. It can take one of two values: true or false. - 1byte - true or false
    float - Floating point number. - 4bytes - +/- 3.4e +/- 38 (~7 digits)
    double - Double precision floating point number. - 8bytes - +/- 1.7e +/- 308 (~15 digits)
    long double - Long double precision floating point number. - 8bytes - +/- 1.7e +/- 308 (~15 digits)
    wchar_t - Wide character. - 2 or 4 bytes - 1 wide character

    * The values of the columns Size and Range depend on the system the program is compiled for.

    The memory in our computer is organised in bytes. A byte is the minimum amount of memory that we can
    manage in C++, it can store a relatively small amount of data: a single character or a small integer
    (generally an integer between 0 and 255). However we can use more bytes and store more complex/longer/bigger numbers.