Data types explained in easy way C Programming

Data types explained in easy way C Programming

Data type allows us to create variables which store different types of data like integer, character, Boolean, float, double. Each data type has its own size.

Each variable has to be declared with some data type because data type decides which type of data variable will store and the size of variable.

Following are 3 types of data type:

  1. Primary data type
    1. int
    2. float
    3. double
    4. char
    5. void
  2. Derived data type
    1. array
    2. reference
    3. pointer 
  3. User define Data type
    1. structure
    2. union
    3. enumeration
Data type can be signed or unsigned. Each date type has its own size, range and format specifier.

Size, Range and Format Specifier of data type

Data Type in C

Data TypeSize(Byte)RangeFormat Specifier
signed char1-128 to 127%c
unsigned char10 to 255%c
short int2-32,768 to 32,767%hd
unsigned short int20 to 65,535%hu
unsigned int40 to 4,294,967,295%u
int4-2,147,483,648 to 2,147,483,647%d
long int8-2,147,483,648 to 2,147,483,647%ld
unsigned long int80 to 4,294,967,295%lu
long long int8-(2^63) to (2^63)-1%lld
unsigned long long int80 to 18,446,744,073,709,551,615%llu
double82.3E-308 to 1.7E+308%lf
long double103.4E-4932 to 1.1E+4932%Lf
float41.2E-38 to 3.4E+38%f

Following C program will help you to understand.

/*
Author : ITVoyagers (https://itvoyagers.in/)

Date :25th December 2018

Description : Programs to understand the basic data types and I/O.
*/

#include<stdio.h>
void main()
{
    int i;
    char c;
    float f;
    double d;
    char s[10]; //string

/********* Printing size of Datatypes **********/

    printf("size of number: %d n",sizeof(int));
    printf("size of character: %d n",sizeof(char));
    printf("size of float: %d n",sizeof(float));
    printf("size of double: %d n",sizeof(double));
    printf("size of string for this program: %d nn",sizeof(s));

/******** Accepting values from user *********/

    printf("Enter a number: ");
    scanf("%d",&i);
    printf("Enter a character: ");
    scanf(" %c",&c);
    printf("Enter a float: ");
    scanf("%f",&f);
    printf("Enter a double: ");
    scanf("%lf",&d);
    printf("Enter a string: ");
    scanf("%s",&s);

/******** Printing Accepted values *********/

    printf("nEntered number: %dn",i);
    printf("Entered character: %cn",c);
    printf("Entered float: %fn",f);
    printf("Entered double: %lfn",d);
    printf("Entered string: %s",s);
}

Output:

size of number: 4
size of character: 1
size of float: 4
size of double: 8
size of string for this program: 10

Enter a number: 5
Enter a character: v
Enter a float: 6.8
Enter a double: 6.655
Enter a string: ITVoyagers

Entered number: 5
Entered character: v
Entered float: 6.800000
Entered double: 6.655000
Entered string: ITVoyagers

CHECKOUT OTHER RELATED TOPICS

PRACTICALS IN C PROGRAM

Leave a Comment