C tute 2: variables
July 31st 2006 03:31
Today's lesson is going to introduce variables, which are used to store information within a program. At the moment we will explore three main forms: int, float and char.
Int stands for integer, which basically means any number without decimal points.
Float stands for a floating point number, which basically means a number with decimals.
Char stands for a single character.
Variables can be named anything, but cannot begin with numbers, or be a c keyword.
Defining variables
A variable must be defined before it is used, and this is done as follows:
int variablename;
Multiple variables can be defined at once:
int v1, v2, v3;
It is also possible to give a variable a value at the same time as defining it.
int v1 = 1;
The system for defining variables is the same with other variable types.
Integers can also be defined with other words, like short, long, unsigned short and unsigned long.
Short and long define the number of bytes used to store the integer and signed and unsigned decide whether positive or negative numbers can be stores.
So
unsigned short int v1;
Would define v1 as a short integer that cannot store signs.
Custom data types
You can also define your own data types. For example, rather than writing
unsigned short int v1;
You can define a data type that has these features with the typedef command, which takes the following form:
typedef datatype dataname;
So, in this case it would be:
typedef unsigned short int customtype;
and then to define v1 we would write
customtype v1;
We will discuss custom data types more later, but that is it for today.
Adam
Int stands for integer, which basically means any number without decimal points.
Float stands for a floating point number, which basically means a number with decimals.
Char stands for a single character.
Variables can be named anything, but cannot begin with numbers, or be a c keyword.
Defining variables
A variable must be defined before it is used, and this is done as follows:
int variablename;
Multiple variables can be defined at once:
int v1, v2, v3;
It is also possible to give a variable a value at the same time as defining it.
int v1 = 1;
The system for defining variables is the same with other variable types.
Integers can also be defined with other words, like short, long, unsigned short and unsigned long.
Short and long define the number of bytes used to store the integer and signed and unsigned decide whether positive or negative numbers can be stores.
So
unsigned short int v1;
Would define v1 as a short integer that cannot store signs.
Custom data types
You can also define your own data types. For example, rather than writing
unsigned short int v1;
You can define a data type that has these features with the typedef command, which takes the following form:
typedef datatype dataname;
So, in this case it would be:
typedef unsigned short int customtype;
and then to define v1 we would write
customtype v1;
We will discuss custom data types more later, but that is it for today.
Adam
| 120 |
| Vote |
subscribe to this blog


















