Data types used in C Programming

Data types used in C Programming

A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Data type is essential when we define a variable. Because when variable is defined we must maintain the data type along with the variable so that it is possible to allocate the memory space. Such memory occupancy is done at compile time. For example, if we are declaring int a, b; then memory location is reserved at compile time for two variables a and b.

 Let us consider the analogous example of cooking. At the time of defining a container we must define its capacity. If it is large enough, then space is wasted and if is small then data can be overflowed.

In C language, there are four basic data types.

a)      Primary or Fundamental Data Type

b)      User Define Data Type

c)      Derived Data Type

d)      Empty Data Type

 

  1. Primary or Fundamental Data Types:
    The primary data types are built in and with the help of that data type we can create all other data types. The Primary or Fundamental Data Types are again divided into three types as given below:
Data Type Ranges of values Memory occupies (bytes) Scan Format Code
Integer
Sign short Integer -128 to +127 2 %d short int
Unsigned short Integer 0 to 255 2 %d unsigned short int
Sign Integer -32768 to +32768 2 %d int
Unsigned Integer 0 to 65535 2 %u unsigned int
Sign long Integer -2147483648 to +2147483648 4 %ld long int
Unsigned long Integer 0 to +4294967295 4 %lu unsign long int
Floating Point
floating point -3.4×10^38 to +3.4×10^38 4 %f float
Double -1.7×10^308 to +1.7×10^308 8 %lf double
Long Double -1.7×10^4932 to +1.7×10^4932 10 %lf long double
Character
signed character -128 to 127 1 %c char
unsigned character 0 to 255 1 %c unsigned char

 

Table 1: Data types

  1.  User Defined Data Types:
    The user defined data types are defined by using primary data types. Example: Structure, Union, Enum, Array, etc.
  2.  Derived Data Types:
    The derived data types are the types that are derived from fundamental data types. Example: Arrays, Pointers, etc.
  3. Empty Data Types:
    The empty data type means that it can be assigned by any other data type. Example: void.

Now let us use all the data type we mentioned in a C program. This program shows how to use the data types with its format specifies in input and output throughout the program.

 

 main() { char c; unsigned char d; int I; unsigned int j; short int k; unsigned short int l; long int m; unsigned long int n; float x; double y; long double z; /*char*/ scanf(“%c%c”,&c,&d); printf(“%c%c”,c,d); /*int*/ scanf(“%d%u”,&i,&j); printf(“%d%u”,i,j);   /*short int*/ scanf(“%d%u”,&k,&l); printf(“%d%u”,k,l);   /*long int*/ scanf(“%ld%lu”,&m,&n); printf(“%ld%lu”,m,n);   /*float, double, long double*/ scanf(“%f%lf%Lf”,&x,&y,&z); printf(“%f%lf%Lf”,x,y,z); }