What will you learn today?
23/06/2003
Carbonize
>>>>> Free condom from MrCondom.com (US Only) <<<<<
I have been receiving a large amount of infected emails.
FREE online virus scan http://housecall.antivirus.com and FREE anti virus software www.grisoft.com

Synfire's Guide To C Programming

3. Variable Basics

       Variables are memory locations that have been given a name that stores certain information, depending on what type was specified.

       Make sure that you define a variable and give it a type. The different types contain a lot of technical information that later on you should remember, but for now I think I can slide by in giving a brief definition of the different types:

short
Holds a small numerical value (2 bytes).
int
Holds a numerical value, on some computers it is larger than a short, but not always, I suggest for any small number just use an int don't worry about short. (2 or 4 bytes).
long
Holds a slightly larger numeric value but depending on the endianness of the computer could be the same as int. (4 bytes).
double
Holds numerical values with decimal points such as 3.14
float
Holds large numerical values with decimal points such as 3.141592653
char
Holds a single character value (1 byte)
bool
holds either a true value or a false value.
       Now this should suffice but there is one thing, on all these types except for char, the numerical ones in other-words, you can further specify your needs through the 'signed' and 'unsigned' prefixes. Signed types only allow the variable to hold negative data, while unsigned types can only hold positive. Unsigned is used alot for Ages, because you can't be -13 years old!

       So you may be asking yourself how do I create a variable.

Well first thing you must define the variable, then you can assign a value to it and use it any way you want. Unfortunately there are a few rules to naming variables, first you can't have spaces in your variable name (use an '_' character if you wanna space), second you can't use any special characters like !@#$%^&*()Ü¥, and last but not least you MUST start your variable with a letter, You can use numbers, but the first character must be a letter.

I suggest using the camels back technique that I use in this tutorial. Now that I have explained the rules, lets play ball...
creating_var.c
----------------------------------
#include <stdio.h>

int VarInt;
/* This has created an int variable called VarInt */

unsigned int PosVar;
/* This created a variable int that can only hold positive numbers */

signed int NegVar;
/* This created a variable int that can only hold negative numbers */

float Pi = 3.14;
/* This has created a float and assigned the value 3.14 to it */

int main()
{
    printf("Enter a positive number: ");
    scanf("%d", &PosVar);
    printf("Enter a negative number: ");
    scanf("%d", &NegVar);
    printf("Enter a number negative or positve: ");
    scanf("%d", &VarInt);
    printf("PosVar:  %d\n", PosVar);
    printf("NegVar:  %d\n", NegVar);
    printf("VarInt:  %d\n", VarInt);
    printf("Pi:      %f\n", Pi);
    return 0;
}
        This should be a good example of how to use variables.
Basically, define a variable, these are called global (any part of the program can use them) variables. You could also use local (only the fuction that the variable is in can use them) variables by moving the 'int VarInt;' or whatever into the funtion that you need them in. This will be discussed in chapter 6 Functions.

        Now that we have gone over normal variables, it is time to go into constants. Constants are variables that the value defined is never changed. You should use constants anywhere that a variable has a defined value that should not be changed. An example of this is a program that finds the area of a circle.

area_of_circle.c
------------------------
#include <stdio.h>

const double PI = 3.14;
/* this makes a double named PI with the value of 3.14 that cannot be changed. */

const int TWO = 2;
/* this makes an int named TWO with the value of 2 that cannot be changed. */

long Radius;
/* this makes a long named Radius that it's value CAN be changed. */

float Area;
/* this makes a float named Area that it's value CAN be changed. */

int main()
{
    printf("Enter the Radius: ");
    scanf("%d", &Radius);
    /* this next part may look a little greek for now
    but this is just to demonstrate that the PI and
    TWO are not changed. */
    Area = (Radius * TWO)* PI;
    printf("The area is:  %f", Area);
    return 0;
}
       Another form of constant is the #define constant.
#define is used right after the '#include's section. Here is an example...
--------------define.c--------------
#include <stdio.h>

#define PI 3.14
#define SENTENCE "This is a sentence."

int main()
{
    printf("Pi is %f\n", PI);
    printf("%s", SENTENCE);
    return(0);
}
----------------------------------


< < < Lesson 2 - Standard I/O    |    Lesson 4 - Conditional Statements > > >