Results 1 to 7 of 7
  1. #1
    Bot Bot's Avatar
    Join Date
    Aug 2011
    Posts
    2,671
    Points
    7,720
         User Info     Contact     Gamer ID
    Join Date
    Aug 2011
    Posts
    2,671
    Points
    7,720

    Post [C++] TUT - Lesson 3 - Declaring Variables

    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, liek 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);
    Previous Lesson: http://www.codeleakers.com/showthrea...-amp-Variables
    Next Lesson: Next Lesson Will Be On Strings

    0 Not allowed! Not allowed!








  2. #2
    Elite Sub's Avatar
    Join Date
    Dec 2008
    Location
    AL
    Posts
    705
    Points
    513
         User Info     Contact     Gamer ID
    Join Date
    Dec 2008
    Location
    AL
    Posts
    705
    Points
    513
    Gamer IDs

    Gamertag: Im Barry Bonds PSN ID: xSubliminal

    Default

    #include <iostream>
    int a, b, c, = 0/0;
    return a + b + c;

    good luck

    0 Not allowed! Not allowed!

    >meme arrows

  3. #3
    Robot Universe Owner
    Creator of DarkFrost
    demon450's Avatar
    Join Date
    Apr 2010
    Location
    New York
    Posts
    2,697
    Points
    6,184
         User Info     Contact     Gamer ID
    Join Date
    Apr 2010
    Location
    New York
    Posts
    2,697
    Points
    6,184
    Gamer IDs

    Gamertag: Medius450 PSN ID: Medius1337

    Contact info:

    Youtube Channel: http://www.youtube.com/user/Roceso1337

    Default

    sub thats ugly but ye 0/0 breaks it, and you should aslo talk of assignment operators
    +=
    -=
    /=
    *=
    %= (modulous for noobs who dont know this)

    0 Not allowed! Not allowed!
    Check out Darkfrost
    Check out Robot Universe






  4. #4
    Bot Bot's Avatar
    Join Date
    Aug 2011
    Posts
    2,671
    Points
    7,720
         User Info     Contact     Gamer ID
    Join Date
    Aug 2011
    Posts
    2,671
    Points
    7,720

    Default

    Quote Originally Posted by demon450 View Post
    sub thats ugly but ye 0/0 breaks it, and you should aslo talk of assignment operators
    +=
    -=
    /=
    *=
    %= (modulous for noobs who dont know this)
    I'm thinking maybe having some more advanced/extra stuff at the end - I felt that there was enough for this tutorial, especially as it took me over an hour .

    0 Not allowed! Not allowed!








  5. #5
    Elite Sub's Avatar
    Join Date
    Dec 2008
    Location
    AL
    Posts
    705
    Points
    513
         User Info     Contact     Gamer ID
    Join Date
    Dec 2008
    Location
    AL
    Posts
    705
    Points
    513
    Gamer IDs

    Gamertag: Im Barry Bonds PSN ID: xSubliminal

    Default

    Go on about arrays and all that good stuff.
    I forget what else i've learned, but it's been centuries since i last got into this type of crap

    0 Not allowed! Not allowed!

    >meme arrows

  6. #6
    Member RogerWilco's Avatar
    Join Date
    Mar 2012
    Location
    $home
    Posts
    152
    Points
    656
         User Info     Contact     Gamer ID
    Join Date
    Mar 2012
    Location
    $home
    Posts
    152
    Points
    656

    Default

    lmao at sub

    You just made every noob crash their program and give up on c++.

    And oh boy! strings! I cant wait! tell me when we get to pointers, thats when the students start dropping like flies.

    0 Not allowed! Not allowed!

  7. #7
    ($user_isGay == TRUE)? giveAids() : break; aids's Avatar

    Badge

    Join Date
    Jul 2008
    Location
    /dev/null
    Posts
    20,361
    Points
    27,190
         User Info     Contact     Gamer ID
    Join Date
    Jul 2008
    Location
    /dev/null
    Posts
    20,361
    Points
    27,190
    Gamer IDs

    PSN ID: TheOriginalVirus

    Contact info:

    Youtube Channel: http://www.youtube.com/riottape Kik Username: aids gtalk account: codeleakers.com@gmail.com

    Default



    pointers are adorable!

    0 Not allowed! Not allowed!
    BTC: 1AidsG8MHF4cEN6KpY1mhJPBhXtms65GVE

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -10. The time now is 04:52 AM.
Powered by vBulletin®
Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.