Data Types In C .

In this session i have discussed the data types in C .


A data type is used to specify the type of data that a variable can store such as integer, floating, character, double etc.


Types
Data Types
Primary data types int , float , double , char
Derived data types array , pointer , structure , union
Enumeration data type enum
Void data type void
.


1-Primary data types

We will discuss the primary data types as below .

1.1-Integer data type

Integer data type allows the variable to hold the numeric value (whole numbers) . int keyword is used to refer the integer data type .

1.2-Floating data types

It is consist of two types

1-float

2-double

1-float

float data type allows the variable to hold the decimal numbers . float keyword is used to refer float data type .

2-double

double data type also same as float data type but it store the value up to 10 digit after decimal . double keyword is used to refer double data type .

1.3-Character data type

character data type is used to store the one character in variable . char keyword is used to refer the character data type .


2-Derived data types

2.1-Array

Array is the collection of values and thier data type is same . The values stored in array is called the elements .

    #include <stdio.h>
    int main()
    {
        int arr[4];   // declared the array
        arr[0]=10; //stored the value at 0 index
        arr[1]=20; //stored the value at 1 index
        arr[2]=30; //stored the value at 2 index
        arr[3]=40; //stored the value at 3 index
        printf("value1 : %d\n", arr[0]);
        printf("value2 : %d\n", arr[1]);
        printf("value3 : %d\n", arr[2]);
        printf("value4 : %d\n", arr[3]);
        return 0;
     }
    OUTPUT : value1 : 10
             value2 : 20
             value3 : 30
             value4 : 40
    

The above code declared the array of size 4 that means you can store the 4 elements . Array always start with 0 index .

We will discuss the array in details in array topic .

2.2-Pointer

A pointer is a data type that store the address of a variable with particular type . They are declared as follows .

    #include <stdio.h>
    int main()
    {
         int x;
         int* ptr;  // ptr pointer is declared
         x=10;
         ptr=&x;  // ptr store the address of x 
         printf("value : %d", *ptr );
         return 0;
    }
    
    value : 10
    

We will discuss the pointer in details in pointer topic .

2.3-Structure

Structure store the multiple data items with different data types in one memory block referenced by the single variable.

    #include <stdio.h>
    #include <string.h>
    struct Employee
    {
        int empid;
        char empname[30];
        int empsal;
    };
    int main()
    {
          struct Employee emp;
          emp.empid=1;//store the value in empid
          emp.empname="Amit";//store the value in empid
          emp.empsal=10000;//store the value in empid
          printf( "EmpID : %d\n", emp.empid);
          printf( "EmpName : %s\n", emp.empname);
          printf( "EmpSal : %d\n", emp.empsal);
          return 0; 
   }
    
 OUTPUT : EmpID : 1
         EmpName : Amit
         EmpSal  : 10000
    

We will discuss the structure in details in structure topic .

Union

A union type is a special construct that permits access to the same memory block by using a choice of differing type descriptions.

    #include <stdio.h>
    #include <string.h>
    union Data
    {
        int i;
        float f;
        char name[30];
        
    };
    int main()
    {
          union Data data;        

          data.i = 10;
          data.f = 220.5;
          strcpy( data.name, "C Language");

          printf( "data.i : %d\n", data.i);
          printf( "data.f : %f\n", data.f);
          printf( "data.name : %s\n", data.name);
          return 0;
      }

    
   OUTPUT : data.i : 1917853763
            data.f : 4122360580327794860452759994368.000000 
            data.name : C Language
    

The i , f value is got currupted because the final value stored in name variable .

We will discuss the union in details in union topic .


3-Enumeration data type

Enumeration data type is used to assign the names to number . enum keyword refer the enumeration data type . Bydefault it start from 0 .

    #include<stdio.h>
    enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun}; 
    int main() 
    { 
        enum week day; 
        day = Tue; 
        printf("%d",day); 
        return 0; 
    }  
    
    OUTPUT : 1
    

We will discuss the enum in details in enum topic .

Welcome to AndroidATPoint.

back image                     next image