Welcome back. We are now going to talk about variables.

Variables, just like in Algebra are the letters representing a number. For the computer variables are little storage place inside the memory. In C there are four types of variables: integer, float point, double floating point and character.

The first one is the integer or counting numbers or numbers without decimals. The second is floating point which are the fractional numbers or numbers with decimals. Double is just like float except it can handle bigger numbers and has more precission, it could hold more decimals places. The last one is characters, these are the letters and numbers.

To be able to use the variables they must be first initialized. We must tell the program that they exist, and create a location for them in the memory.

To initialize an integer we use int.
int x, y;
This initiates the integer variable x and y.

To initialize a floating point we use float.
float a, b;
This initiates the floating point variable a and b.

To initialize a double floating point we use double.
double c, d;
This initiates the double floating point variable c and d. You get the idea right?

To initialize a character we use char.
char s, t;
This initializes a character variable s and t.

To initialize a string we use character array.
char u[8], v[8];
This initializes a character variable array u and v of 7 characters. It's only seven characters because the last one contains the terminating character. Just remember that expected number of characters is always one less than the number specified.

Now we need to know specifiers. Specifiers are used when we want to input or output value of variables using printf and scanf. It specifies what type of variable it it is, and is always a percent sign (%) followed by a letter. For integers the specifier is %d, for float and double %f for characters % and for strings %s.

Now, here's an example.

#include <stdio.h>
#include <conio.h>

char name[8];
int a,b,sum;

main()
{
clrscr();

printf("What is your name: ");
scanf("%s", &name);

printf("Please enter two numbers:");
scanf("%d %d", &a, &b);

sum=a+b;

printf("\nYour name is %s.\n", name);
printf("%d + %d = %d", a, b, sum);

getch();
return(0);
}

Here's the output.


What is your name: Anthony
Please enter two numbers: 1 2

Your name is Anthony.
1 + 2 = 3

printf("What is your name:");
scanf("%s", &name);
The first line ask the user their name and the scanf gets the input from the user. We use the specifier %s because it is a string variable. There should be an ampersand before the the varible where inputted data will be stored. C considers a space as a terminating character only your first name can be saved. To be able to have spaces you can replace the scanf with name=gets();.

printf("Please enter two numbers:");
scanf("%d %d", &a, &b);
Here, the user is asked to input two numbers and the numbers will be stored in variable a and b. We used %d for integers and there should always be an ampersand in front of the variable.

sum=a+b;
Adds the two numbers together and store the answer in the variable sum. The basic mathematical operators are plus(+), minus(-), multiply(*), divide(/) and modulus(%). What's modulus you ask, it returns the remainder. You should know what the rest of them do. When performing division using integers the decimals are cut off.

printf("Your name is %s.\n", name);
printf("%d + %d = %d", a, b, sum);
When displaying variables, you have the comment inside the string and you replace where you want the value of a variable to be with a specifier. The string is followed by a comma and the variable. The order of the variable from left to right follows the order of the specifier respectively.

That should do it for ya. Now time for your homework. Create a program that prompts for your name and three numbers, compute for the sum and average and outputs your name, the sum and average.

Click here to see what I did.

The output out put should look similar to this.

What is your name: Anthony
Please enter two numbers: 1 2 3

Your name is Anthony.
1 + 2 + 3 = 6
6 / 3 = 2

Did you actually do the homework? If not, go try it now. It's simple, you just change and add a few to the example program I showed you. Next we will discus the conditional statements when I get around to it.

<<back<< >>Next>>