Home Contact About

Declaration of Variables in C






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.

Primary Type Declaration
A variable can be used to store a value of any data type. That is, the name has
nothing to do with it's type. THe syntax for declaring a variable is as follows:
data-type v1,v2,...vn ;
v1, v2, ...vn are the names of variables. Variables are seprated by commas.
A declaration statement must end with a semicolon.
For example, valid declaration are:
int count;
int number, total;
double ratio;

int and double are the keywords to represent integer type and real
type data values respectively.
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

main( ) is the beginning of the program. The opening braces { signals the execution
of the program. Declaration of the variable is usally done after the opening braces.
Sometimes variables can also be declared outside the main.
Declaration of variables
main() /*****************Program Name*****************/
{
/*************Declaration*******************/
float x,y;
int code;
short int count;
long int amount;
double deviation;
unsigned int n;
char c;
/****************Computation*********************/
. . . .
. . . .
. . . .
} /************Program Ends***********************/

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.

User-defined Type declaration
C suppourts a feature known as "type defination" that allows users to define an identifier
that would represent an existing data type. The user defined data type identifier can later
be used to declare variables. It takes the Genral Form:
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:

enum identifier{value1,value2,...valuen};

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;