What are variables
Variables in
C are memory lo
cations that are given names and
can be assigned values. We use variables to store data in memory for later use. There are 2 basi
c kinds of variables in
C whi
ch are numeri
c and
chara
cter.
Numeric variables
Numeri
c variables
can either be integer values or they
can be Real values. Integer values are whole numbers without a fra
ction part or de
cimal point in them. Real numbers
can have a de
cimal point in them.
Character variables
Chara
cter variables are letters of the alphabet as well as all
chara
cters on the AS
CII
chart and even the numbers 0 - 9.
Chara
cters must always be put between single quotes. A number put between single quotes is not the same thing as a number without them.
What are constants
The differen
ce between variables and
constants is that variables
can
change their value at any time but
constants
can never
change their value.
Constants
can be useful for items su
ch as Pi or the
charge on an ele
ctron. Using
constants
can stop you from
changing the value of an item by mistake.
Declaring variables
To de
clare a variable we first put the type of variable and then give the variable a name. The following is a table of the names of the types of variables as well as their ranges:
Name | Type | Range |
int | Numeric - Integer | -32 768 to 32 767 |
short | Numeric - Integer | -32 768 to 32 767 |
long | Numeric - Integer | -2 147 483 648 to 2 147 483 647 |
float | Numeric - Real | 1.2 X 10-38 to 3.4 X 1038 |
double | Numeric - Real | 2.2 X 10-308 to 1.8 X 10308 |
char | Character | All ASCII characters |
You
can name a variable anything you like as long as it in
cludes only letters, numbers or unders
cores and does not start with a number. It is a good idea to keep your variable names less than 32
chara
cters long to save time on typing them out and for
compiler
compatibility reasons. Variables must always be de
clared at the top before any other
commands are used. Now let's de
clare an integer variable
called a and a
chara
cter variable
called b.
int main()
{
int a;
char b;
return 0;
}
You
can de
clare more than one variable at the same time in the following way:
int main()
{
int a,b,c;
return 0;
}
To de
clare a
constant all you have to do it put the word
const in front of a normal variable de
claration and make assign a value to it.
int main()
{
const float pi = 3.14;
return 0;
}
Signed and unsigned variables
The differen
ce between signed and unsigned variables is that signed variables
can be either negative or positive but unsigned variables
can only be positive. By using an unsigned variable you
can in
crease the maximum positive range. When you de
clare a variable in the normal way it is automati
cally a signed variable. To de
clare an unsigned variable you just put the word unsigned before your variable de
claration or signed for a signed variable although there is no reason to de
clare a variable as signed sin
ce they already are.
int main()
{
unsigned int a;
signed int b;
return 0;
}
Using variables in calculations
To assign a value to a variable you use the equals sign.
int main()
{
int a;
char b;
a = 3;
b = 'H';
return 0;
}
There are a few different operators that
can be used when performing
cal
culations whi
ch are listed in the following table:
Operator | Operation |
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus(Remainder of integer division) |
To perform a
cal
culation you need to have a variable to put the answer into. You
can also use both variables and normal numbers in
cal
culations.
int main()
{
int a,b;
a = 5;
b = a + 3;
a = a - 3;
return 0;
}
Reading and printing variables
You
can read a variable from the keyboard with the s
canf
command and print a variable with the printf
command.
#include
int main()
{
int a;
scanf("%d",&a);
a = a * 2;
printf("The answer is %d",a);
return 0;
}
The %d is for reading or printing integer values and there are others as shown in the following table:
%d or %i | int |
%c | char |
%f | float |
%lf | double |
%s | string |