PDF

Data Type Modifiers

Leave a Comment

Modifiers 


The data types explained previously have the following modifiers : 

  1.  short 
  2.  long
  3.  signed
  4.  unsigned


The modifiers define the amount if storage allocated to variable. The amount of storage allocated is not cast in stone. ANSI has the following rules:

short int <= int <= long int

float <= double <= long double


What this means is that a 'short int' should assign less than or the same amount of storage as an 'int' and the 'int' should be less or the same bytes than a 'long int'.

What this means in the real word is:

Bytes:


2 short int
2 unsigned short int
4 unsigned int
4 int
4 long int
1 signed char
1 unsigned char
4 float
8 double
12 long double


Ranges


short int             -32,768 to +32,767

unsigned short int    0 to +65,535

unsigned int         0 to +65,535

int                  -32,768 to +32,767

long int             -2,147,483,648 to + 2,147,483,647

signed char          -128 to +127

unsigned char        0 to +255

float                3.4*10^-38 to 3.4*10^38

double               1.7*10^-308 to +1.7*10^308


Note: One simple way to remember the limits is :
Every 1 byte = 8 bits
so range is form (2^(n-1))  to  (2^(n-1))-1
where 'n' is the no. of. bits

e.g.
for int takes 2 bytes
2 bytes = 16 bits
so n = 16
so range is from (2^15) to (2^15)-1


Qualifiers 


A type qualifier is used to refine the declaration of a variable, a function, and parameters, by specifying whether:

The value of a variable can be changed. The value of variable must always be read from memory rather than from a register.

Standard C language recognizes the following two qualifiers:

1. const :


The const qualifiers is used to tell C that the variable value can not changed after initialisation.

const float pi = 3.24259;

Now pi cannot be changed at a later time within the program. Another way to define constants is with the # define preprocessor which has the advantage that it does not use any storage.

2. volatile :


The volatile qualifier declares a data type that can have its value changed in ways outside the control or detection of the compiler ( such as a variable updates by the system clock or by another program).
This prevents the compiler from optimizing code referring to the object by storing the objects value in a registered re-reading it from there, rather than from memory, where it may have changed. You will use this qualifier once you will become expert in 'C'. So for now just proceed.

0 comments:

Post a Comment