Logo designed by CO2 - http://www.planetquake.com/CO2
Logo designed by CO2 - http://www.planetquake.com/CO2
Logo designed by CO2 - http://www.planetquake.com/CO2
Logo designed by CO2 - http://www.planetquake.com/CO2
   
Back to HomePage

Back to Tutorial menu

Credits

  • Puke
  • SumFuka
  • Saqquara

  •  

      Variables Part 2

    Tired yet? Too bad! There's still one more tutorial full of variables, so lets begin.

    OK, last time you learnt what a variable is, and what a data-type is, and the four main types. Sure there are a few more, but you don't need to worry about them just yet:-) The four you have learnt about are floats, integers, long integers and characters. Now, to define any variable for use, use the following:

    int TestInt;
    long TestLong;
    char TestChar;
    float TestFloat, TestF2, TestF3;

    That's it! That's all you need to do to create a variable, just define its data-type, and there you go! Sure the names can be replaced with whatever you want, as long as they follow the rules in the last tutorial. You might want to note the last line ('float TestFloat, TestF2, TestF3;'). All it does is create 3 floats with the appropriate name, it could also be written as:
    float TestFloat;
    float TestF2;
    float TestF3;

    Simple stuff, now lets make a program using variables.

    /* Tutorial 4 -Variables part 2 - Vartest.c */

    #include <stdio.h>

    int main()
    {
    int MyFirstVar, Var2;
    int Total;

    MyFirstVar = 10;
    Var2 = 25;

    /*---------------------------------------------------------------------*/
    /*We add MyFirstVar to Var2 and stick the awnser in variable 'total' */
    /*-------------------------------------------------------------------- */

    Total = MyFirstVar + Var2;
    printf("%d + %d is equal to %d\n", MyFirstVar, Var2, Total);

    /*---------------------------------------------------------------------*/
    /*Now we make 'MyFirstVar' a different number, and do the same as above*/
    /*-------------------------------------------------------------------- */

    MyFirstVar = 300;
    Total = MyFirstVar + Var2;

    printf("%d + %d is equal to %d", MyFirstVar, Var2, Total);
    }

    Now, most of this should not have been to hard to understand. First we defined three variables, MyFirstVar, Var2, and Total. Then we gave each a value, and made Total equil MyFirstVar plus Var2. Now, back to the program. After we gave 'Total' a value, we printed all the info out using a call to the printf function. Now, this may look really weird, but don't worry. OK, inside printf, you use '%d' to represent the variable in the string to be printed to the screen. Now, the %d actually means that the variable to be printed is an integer. Printf can also format numbers in hexadecimal (base 16) or octal (base 8), so the 'd' really stands for decimal (base 10). You can use '%d' for normal integers (int) and also long integers (long). The other obvious ones are '%f' for float, and '%c' for char. So, if you wanted to print out a variable with a data-type of 'float', then you would use '%f', and for char, '%c'. The concept shouldn't be to difficult to understand.
    Then, when the string to be printed had finished, you place the finishing speech mark, but now, instead of putting in the closing bracket, you put a comma, and then the names of the variables, as they appear in the string. Get it??!?
    OK, after that you give 'MyFirstVar' a new value of 300. Then, you need to update total, since it still has the value of the old calculation, when MyFirstVar held the value of 10.

    Tutorial by Puke


    Back To Tutorial 3 Forward To Tutorial 5
                   
    
    
    
    

     

    Logo designed by CO2 - http://www.planetquake.com/CO2

    This site, and all content and graphics displayed on it,
    are ©opyrighted to the Quake DeveLS team. All rights received.
    Got a suggestion? Comment? Question? Hate mail? Send it to us!
    Oh yeah, this site is best viewed in 16 Bit or higher, with the resolution on 800*600.
    Thanks to Planet Quake for there great help and support with hosting.
    Best viewed with Netscape 4 or IE 3