After declaring suitable variable names, we must declare them to the compiler.
Declaration does two things:
The declaration of variables must be done before they are used in the program.
data-type v1,v2,...vn ; |
---|
int count;
int number, total;
double ratio;
Data Type | Keyword equivalent |
---|---|
Character | char |
Unsigned character | unsigned char |
Signed character | signed char |
Signed integer | signed int |
Signed short integer | signed short int |
Signed long integer | signed long int |
Unsigned integer | unsigned int |
unsigned short integer | unsigned short int |
Unsigned long integer | unsigned long int |
Floating point | float |
Double-precision floating point | double |
Extended double-precision floating point | long double |
Declaration of variables |
---|
main() /*****************Program Name*****************/
|
When an qualifier short, long, or unsigned is used without basic
type specifier, C compilers treat data types as an int. If we want to use the
specific type we have to define it to the compiler.
typedef type identifier;
|
---|
Where type refers to an existing data types and identifiers refer to the new name
given to the data type. THe existing data type may belong to any class of type,
including the user defined ones. Remember that the new type is 'new' only in name
but not the data type. typedef cannot create a new type.
Some examples of type defination are:
typedef int units;
typedef float marks;
Here, units symbolizes int and mark symbolizes float.
THey can be later used to declare variable as follows:
units batch1 , batch2;
marks name1[50], name2[50];
batch1 and batch2 are inclared as int variable and name1[50] and name2[50]
declared as 50 elements floating point array variables. The main advantage of typedef
is that we can create meaningful data type names for increasing the readiblity of the program.
Another user-defined data type is enumerated data type provided by ANSI standard.
It is defined as follows:
THe "identifier" is a user-defined enumerated data type which can be used to declare
variables that can have one of the values enclosed within the braces (known as
enumeration constants). After this defination, we can declare variables to be of this
new type as below:
enum identifier v1, v1, ...vn;
The enumerated variables v1, v2, vn can only have on the values value1, value2, ...valuen.
THe assignments of the following type are valid:
v1 = value3;
v5 = value1;
An Example:
enum day {Monday,Tuesday,...Sunday};
enum day week_st, week_end;
week_st = Monday;
week_end = Friday;
if(week_st == Tuesday)
week_end = Saturday;
THe compiler automatically assigns integer digits beginning with 0 to all enumeration
constants. That is, the enumeration constant value1 is assigned 0, value2 is assigned 1, and
so on. However, the automatic assignments can be overridden by assigning values explicitly
to the enumeration constants. For example:
enum day{Monday = 1, Tuesday, ...Sunday};
Here the constant Monday is assigned the value of 1. The remaining constants are as signed
values that increases successively by 1.
THe defination and declaration of enumerated variables can be combined in one statement.
For Example:
enum day {Monday, ...Sunday} week_st, week_end;