C Language Class 4 : C variables and Constants
Read original Article:
C Language Class 4 : C variables and Constants | Learn C Programming | C Language | C programs
Variables in C are
memory locations that are given
names and can be assigned values. We use variables to store data in
memory for later use. There are 2 basic kinds of variables in C which
are numeric and character.
Numeric variables
Numeric variables can either be integer values or they can be Real
values. Integer values are whole numbers without a fraction part or
decimal point in them. Real numbers can have a decimal point in them.
Character variables
Character variables are letters of the alphabet as well as all
characters on the ASCII chart and even the numbers 0 – 9. Characters
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 difference 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 such as Pi or the charge on an
electron. Using constants can stop you from changing the value of an
item by mistake.
Declaring variables
To declare 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 includes only
letters, numbers or underscores and does not start with a number. It is
a good idea to keep your variable names less than 32 characters long to
save time on typing them out and for compiler compatibility reasons.
Variables must always be declared at the top before any other commands
are used. Now let’s declare an integer variable called a and a character
variable called b.
You can declare more than one variable at the same time in the following way:
To declare a constant all you have to do it put the word const in
front of a normal variable declaration and make assign a value to it.
Signed and unsigned variables
The difference 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 increase the
maximum positive range. When you declare a variable in the normal way it
is automatically a signed variable. To declare an unsigned variable you
just put the word unsigned before your variable declaration or signed
for a signed variable although there is no reason to declare a variable
as signed since they already are.
Using variables in calculations
To assign a value to a variable you use the equals sign.
There are a few different operators that can be used when performing calculations which are listed in the following table:
Operator |
Operation |
+ |
Addition |
– |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Modulus(Remainder of integer division) |
To perform a calculation you need to have a variable to put the answer into. You can also use both variables
Reading and printing variables
You can read a variable from the keyboard with the scanf command and print a variable with the printf command.
printf ( "The answer is %d" ,a); |
Format specifiers
when ever a variable has to be inputted then some registers the
values from the input unit and transfers it to their respective memory
locations. Hence it is required to specify the format of the incoming
data so that there is ease to send the corresponding no. of registers
because if the format is not known to us then it becomes very difficult
for the processor of decide the number of registers to send and in this
case the maximum possible registers are send and in general case it
causes the memory loss. Hence this format specifiers are used to save
the memory. Some of the common format specifiers used in C are
Format specifier |
Purpose |
%d |
integer data types |
%f |
float |
%u |
unsigned integers |
%s |
string |
%c |
char |
%ld |
long integer |
Comments
Post a Comment