Tuesday 12 June 2012

DATA TYPES NOTES PART 1


Data types in c language

Data types in c 


1. Introduction


2. List of data types


3. Primitive data types in c


4. Modifiers of data types in c


5. List of modifiers in c


6. Default modifiers of data types in c


7. Default data of modifiers in c


8. Rules of using modifiers in c


9. Possibles modifiers of given data types in c


10. Size modifier in c


11. Size of data types in c


12. Sign modifier in c


13. Range of data types in c


14. Easy way to remember limit of data types in c


15. Const modifiers in c


16. Pointers modifier in c


17. Function modifier in c


18. Interrupt modifier in c


19. Volatile modifier in c


20. Fundamental data types in c


21. Memory representation of char in c


22.  Memory representation of signed char in c


23.  Memory representation of int in c


24. Memory representation of signed int in c


26. Memory representation of double in c


Introducing c data type


Every programming language deals with some data. For example to print any message it requires charterer or string type of data. To solve any mathematic expression it requires integral as well as real number (floating type) of data. C is very rich in data type. We can broadly divide all data type in c in three categories:

1. Primitive or fundamental data type
2. Derived data type
3. User defined data type

List of data type in c


A complete picture of all c data types has been represented by following figure.





Note: Apart from these basic data type there are few other data types which have been defined inside header files which will be discussed later. 

Primitive data types in c

Primitive or most fundamental data type in c can be categorized in three groups on the basis of its application:

1. Integral type number: char , int
2. Real type number: float , double
3. Void or nothing type: void

C data type modifiers

There are some keywords in c which modify the meaning the meaning of above mentioned basic data type in c. On the basis of properties of modifier we can categories the modifier in following eight groups.

1. Size modifier
2. Signed modifier
3. Constant modifier
4. Volatile modifier
5. Storage class
6. Pointer modifier
7. Function modifier
8. Interrupt

All modifiers in c have been represented by following table:

List of modifiers in c



In the above table modifier ending with * indicates they are not keyword of c language. These modifiers are called as nothing modifier since there is not any special keyword in which represents those modifiers. If you will not write any thing it then compiler will understand you are writing nothing modifier of those groups.

Meaning of following word in the above table:

nothing: It is not short as well as not long.
not_const:  It is not constant. You can modify.
Not_volatile: It is not volatile.
not_interrupt: It is not sending interrupt signal.

Important points:

1. Nothing modifier must be default modifier of that group.
2. In LINUX GCC compiler there is not any concept of pointer modifier.

Default modifier in c


    If you will not write any modifiers of a particular group then c compiler will take default modifier of that group. Default modifier of each group has written in the following table:  



1. Default modifier of storage class is auto when we declared the variable inside any function and default modifier of storage class is static when we declared variable outside of all functions. In other word we can say if variable has declared locally then default storage class is auto and if it has declared globally then default storage class of variable is extern.

2. Default storage class of function is extern.

3. Default modifier of pointer modifier depends upon memory model. For detail knowledge click following link:

WHAT IS MEMORY MODEL IN C?

Default data type in c

Default data type is opposite of default modifier as you read in the above topic. In case of only SIZE, SIGN, STORAGE CLASS, CONSTANT, and VOLATILE modifier compile automatically assumes int as a default data type. For example:

(1)
long a=25;
It is equivalent to: long int a=25;
(2)
signed a=25;
It is equivalent to: signed int a=25;
(3)
register a=25;
It is equivalent to: unsigned register int a=25;

But it is illegal to write:
far *f;
pascal p;
interrupt i;

Question: What will be output of following c code?

#inclued<stdio.h>
int main(){
    printf(“%d”,sizeof(const,extern,volatile));
    return 0;
}

Modifiers in c


Explanation of modifiers in c programming language by examples and questions

Rules for using modifier in c

Rule 1: We cannot use two modifiers of same groups in any particular data type of c.
For example, following declaration of c are illegal:

short long int i;
static auto char c;
signed unsigned int array[5];
pascal cdecl display();

Following are valid declaration of c:

const volatile float f;
signed static long volatile int i;

Question: Is following declaration is valid in c?

1.   intnear * far * huge *p;
2.  char const * const *c;
3.  short short int i;
4.  const const int i;

Rule 2: We can write modifier either before the data type or after the data type. For example, both of following declaration is correct:

unsigned char c;
char unsigned c;

Rule 3: Order of modifier including data type doesn’t affect the meaning of declaration. For example all of the following have same meaning:

int const short extern i;
int extern const short i;
int short extern const i;
const int short extern i;
extern short const int i;

Rule 4: There is one exception in rule 3. POINTER, FUNCTION and INTERRUPT modifier must be written after the data type. For example, in the following declaration:

unsigned const char far *c;
char unsigned const *c;
char far unsigned const *c;
const char far unsigned *c;
far char const unsigned *c;
const unsigned far char *c;

First four declarations are valid as well as equivalent. But last two declarations are invalid.   

Modifiers of variable in c

Possible modifiers with given data type in c

We cannot use all modifiers with each data types in c. Following table represents permitted or meaningful modifiers which can be used with a particular data type.

Some important points regarding above table:

1. In case of array and pointer, size and sign modifier depends upon which data type you are using in the declaration of array and pointer. Only those SIZE and SIGN modifier are valid which are also valid for that data type. For example:

signed float * p;
short double arr[5];


Above declaration is invalid for c because signed and short modifier is not suitable for float and double data type respectively as shown above table.    

2. In the above table we have used word permitted and meaningful modifier because some modifier has permitted from only compilation point of view i.e. but compiler is not showing any error but it is meaning less statement of c. For example:

short float f;
long float f;
short double d;
void const display();
int static far i;
char interrupt c;

3. We can use pascal and cdecl modifier with primitive data type, pointer and array only for naming convention.  

Question: Consider the following c program:

char c;
int display();
int main(){
    int i;
    return 0;
}


Write down all default modifier of variable c, i and function display, as declared in the above c code.

Answer:
All default modifier of variable: c



Equivalent declaration: signed static char c;

All default modifier of variable: i



Equivalent declaration: signed auto int i;


All default modifier of function: display



Equivalent declaration: extern void cdecl near display();
  
Importance of all eight group of modifier in c:

1. SIZE MODIFIERS:
    Only two modifiers short and long can affect the size of any data type in c. size of primitive data type in c depends upon the word length of the microprocessor. In general we can say size of int is word length of microprocessor.

Size of data types in c

Size of data types in the 16 bit compilers, like TURBO c++, Borland c++ etc.











Size of data types in the 32 bit compilers. Example: LINUX gcc compiler.






2. SIGN MODIFER:

    These modifiers are responsible for to make data type signed or unsigned. Both signed and unsigned modifiers affect the range of a data type. In c char all primitive data type except void are by default signed. If you will write:
signed char c;
signed short int si;
signed int i;
signed long int li;

Compiler will not show any error or warning message but it increases the unnecessary redundancy. So it is better to write:

char c;
short int si;
int i;
long int li;

But if you will write:
signed float f;
signed double d;
signed long double ld;

Then compiler will show an error message.

Following table illustrate the range or maximum or minimum value of data types in TURBO C++ and Borland c++ compilers.


Note: In the above table range of float, double and long double has written only for positive numbers. But this range is also true for negative numbers i.e. for range of float is -3.4*10^38 to -3.4*10^ (-38) and so on.
 
Interview Question: Why range of signed char is -128 to 127 not -127 to 128?




How to remember size of data type in c?

    If you have memorized the size of data type in all types c compiler then there is not any problem. If you cannot memorized then it is also not a problem because every c compiler provide one header file <limits.h>which contain some micro constants for size of integral data type which is easy to memorize. Those micro constants are:

       IN TURBO C++ IDE (Header file: <limits.h>)


  IN LINUX GCC Compiler (Header file: <limits.h>,<float.h>)


Make the habit to use macro constant to check boundary condition so that your programs will more platforms independent.