Type declaration instruction in c

This article cover all the aspect of type declaration instruction in c, with the help of examples.

Type declaration instruction in c is used to declare the type of variables being used in the program. Any variable used in the program must be declared before using it in any statement. The type declaration statement is written at the beginning of the main( ) function.

Syntax:

data_type variable_name; 

Data types

Data types are used to inform the system which type of data you are going to allocate to the variable. There are basically two type of data types primitive and non-primitive.

Such as int for integer, float for floating number, char for a character, etc.

If you want to know more about the data types in c language you can check out by click here.

Variable

An entity that may vary during program execution is called a variable. Variable names are names given to locations in memory. These locations can contain integer, real, or character constants.

There are some basic rules for a variable name, and generally it is almost same in every programming languages like C, C++, Java, etc.

For more about variable click here.

For example:

float rs, gross_sal; 
char name, code;

C type declaration instruction variations

There are several subtle variations of the type declaration instruction. These are discussed below:

(a) While declaring the type of variable we can also initialize it as shown below.

int i = 10, j = 25 ; 
float a = 1.5, b = 1.99 + 2.4 * 1.44 ;

(b) The order in which we define the variables is sometimes important sometimes not.

For example,

int i = 10, j = 25 ; 

is the same as

float a = 1.5, b = a + 3.1 ;

is alright, but

float a = 1.5, b = a + 3.1 ;

is not. This is because here we are trying to use a before defining it.

(C) The following statements would work

int a, b, c, d; 
a = b = c = 10;

However, the following statement would not work

int a = b = c = d = 10;

Once again we are trying to use b (to assign to a) before defining it.