Tuesday, September 28, 2010

Data Types in 'C'

DATA TYPES

'C' language is rich in its datatypes. The variety of data types available allow the programmer to select the type appropriate to the needs of the application as well as the machine. 'C' supports four classes of data types
  • Primary (fundamental) data types
  • User-defined data types
  • Derived data types
  • Empty data set
PRIMARY DATA TYPES

All 'C' compilers support four  fundamental data types, 
  • integer(int)
    • signed int
    • short int
    • long int
    • unsigned int
    • unsigned short int
    • unsigned long int
  • character(char)
    • signed char
    • unsigned char
  • floating point(float)
    • float
    • double
    • long double
  • double precision floating point(double)

The size and range of each data type is given in the table below

















DATA TYPE

RANGE OF VALUES

char

-128 to 127

Int

-32768 to +32767

float

3.4 e-38 to 3.4 e+38

double

1.7 e-308 to 1.7 e+308


Integer Type :

Integers are whole numbers with a machine dependent range of values. A good programming language as to support the programmer by giving a control on a range of numbers and storage space. C has 3 classes of integer storage namely short int, int and long int. All of these data types have signed and unsigned forms. A short int requires half the space than normal integer values. Unsigned numbers are always positive and consume all the bits for the magnitude of the number. The long and unsigned integers are used to declare a longer range of values.

Floating Point Types :

Floating point number represents a real number with 6 digits precision. Floating point numbers are denoted by the keyword float. When the accuracy of the floating point number is insufficient, we can use the double to define the number. The double is same as float but with longer precision. To extend the precision further we can use long double which consumes 80 bits of memory space.

Character Type :

A single character can be defined as a defined as a character type of data. Characters are usually stored in 8 bits of internal storage. The qualifier signed or unsigned can be explicitly applied to char. While unsigned characters have values between 0 and 255, signed characters have values from –128 to 127.

Size and Range of Data Types on 16 bit machine.
















type

SIZE (Bits)

Range

Char or Signed Char

8

-128 to 127

Unsigned Char

8

0 to 255

Int or Signed int

16

-32768 to 32767

Unsigned int

16

0 to 65535

Short int or Signed short int

8

-128 to 127

Unsigned short int

8

0 to 255

Long int or signed long int

32

-2147483648 to 2147483647

Unsigned long int

32

0 to 4294967295

Float

32

3.4 e-38 to 3.4 e+38

Double

64

1.7e-308 to 1.7e+308

Long Double

80

3.4 e-4932 to 3.4 e+4932


Declaration of Variables

Every variable used in the program should be declared to the compiler. The declaration does two things.

1. Tells the compiler the variables name.
2. Specifies what type of data the variable will hold.

The general format of any declaration

datatype v1, v2, v3, ……….. vn;

Where v1, v2, v3 are variable names. Variables are separated by commas. A declaration statement must end with a semicolon.

Example:
Double average, mean;
Int sum;
Int number, salary;



USER DEFINED DATA TYPES


 'C' supports a feature known as "type definition" 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. Its general form is
 typedef type identifier;

where
type refers to an existing data type
identifier refers to the new name given to existing data types

NOTE: typedeff cannot create a new type

Examples

typedef int units;
typedef float marks;

Here units symbolizes int and marks symbolizes float, they can be later used to declare variables as follows:

units batch1, batch2;
marks name1[80], name2[80];

Another userdefined datatype is enum (enumerated data type). It is defined as follows:

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

The identifier is a user defined enumerated datatype which can be used to declare variables that have one of the values enclosed within the braces. After the definition we can declare variables to be of this ‘new’ type as below.

enum identifier V1, V2, V3, ……… Vn

The enumerated variables V1, V2, ….. Vn can have only one of the values value1, value2 ….. value n

Example:

enum day {Monday, Tuesday, …. Sunday};
enum day week_st, week end;
week_st = Monday;
week_end = Friday;
if(wk_st == Tuesday)
week_en = Saturday; 


Declaration of Storage Class

Variables in C have not only the data type but also storage class that provides information about their location and visibility. The storage class divides the portion of the program within which the variables are recognized. 

/*Example of storage Classes*/
int m;
main()
{
   int i;
   float balance;
   .....
   function1();
 }

 function1()
 {
    int i;
    float sum;
    ......
    ......
  }


The variable m which has been declared before main is called global variable. It can be used in all functions in the program and no new variable can created using name m. The variables i, balance and sum are called local variables because they are declared inside a function. Local variables are visible and meaningful only inside the functions.

auto : It is a local variable known only to the function in which it is declared. Auto is the default storage class.

static : Local variable which exists and retains its value even after the control is transferred to the calling function.

extern : Global variable known to all functions in the file

register : Social variables which are stored in the register. 

Assigning Values to Variables

Variables are created for use in program statement such as

value=amount+inrate*amount;

Here the numeric value stored in variable inrate  is multiplied by the value strored in amount and product is added to amount. The result is stored in the variable value.


Defining Symbolic Constants

A symbolic constant value can be defined as a preprocessor statement and used in the program as any other constant value. The general form of a symbolic constant is

# define symbolic_name value of constant

Valid examples of constant definitions are :

# define marks 100
# define total 50
# define pi 3.14159


These values may appear anywhere in the program, but must come before it is referenced in the program.It is a standard practice to place them at the beginning of the program. 

Declaring Variable as Constant

The values of some variable may be required to remain constant through-out the program. We can do this by using the qualifier const at the time of initialization.
 
Example:
Const int class_size = 40;

The const data type qualifier tells the compiler that the value of the int variable class_size may not be modified in the program.
 
 Volatile Variable 
 
A volatile variable is the one whose values may be changed at any time by some external sources.

Example:

volatile int num;

The value of data may be altered by some external factor, even if it does not appear on the left hand side of the assignment statement. When we declare a variable as volatile the compiler will examine the value of the variable each time it is encountered to see if an external factor has changed the value.

0 comments:

Post a Comment