Datatypes in C language

All the programs we code and applications we develop are to for manipulations data. The way we store/fetch/process the data depends upon the datatype we use. The term datatype can be understood with two constraints.
The term ‘data’ can be interpreted in two ways – variable data and constant data. The term ‘type’ can be interpreted in two ways – integral type and real type.
There are two classifications of datatypes – basic/primitive datatypes and use user-defined datatypes. The chart shown below can make the difference between the interpretation of the term ‘datatype’ and classification of datatypes.
Datatypes in C
Datatypes in C
Interpretation of datatypes in C:
All the integral datatypes deal with the data that does not consist of decimal point. Though ‘char’ datatype deals with characters, gcc compiler considers ‘char’ as integer datatype. All the real datatypes deal with the data consisting of decimal point.
Irrespective of the data stored, data corresponding to a datatype can be constant or variable; throughout the program.
Classification of datatypes in C:
Size of the datatype, memory alignment and type of data to be store are the constraints for classification of datatypes.
The constraints of datatypes are known to the compiler in the basic datatypes.
For example, char occupies 1 byte of memory and an integer occupies 4 bytes of memory and so on.
The constraints of datatypes are known to the compiler only after compilation and/or sometimes when the program is being executed in the user-defined datatypes.
Execute the following program and observe the output.
 #include<stdio.h>  
 int main()  
 {  
  char v=50;  
  printf("v=%d\t%c\n",v,v);  
  v=v*2;  
  printf("v=%d\t%c\n",v,v);  
  v=v*2;  
  printf("v=%d\t%c\n",v,v);  
  return 0;  
 }  
Observe that the variable ‘v’ consists of the value 50, which is once fetched as an integer using %d and as a character using %c. When they are fetched using different format specifiers, the data is displayed according to ASCII values for %c.
The conclusion from this observation is that any type of data can be stored in any datatype but, the way the data is fetched and processed gives different results.
To get the datatypes, their explanation, ranges and format specifiers, click here.
*Remember that all the sizes of datatypes used in this blog are with respect to gcc compiler on 32-bit machine.  Also, note that the datatype Boolean is also added in the C99 version of C.
Share:

0 comments:

Post a Comment