×
>
<

Aptitude

Data types in C

What are the Data types ?

A data type in C can be defined as an attribute of data that defines the property of variables as to know what kind of data will the variable store and also enables the interpreter and compiler to understand how the programmer intends to use the data.

Primary Data Type
  • Also known as primitive data types or built-in data types, are fundamental data types which provide a built-in support in the coding process in C.
  • Each of these data types stores data of different types and different range.We know that we have four Primary data types, namely:
    1. char
    2. int
    3. float
    4. double
Declaration of Primary Data types
  • DATA_TYPE VARIABLE_NAME : Must begin with a Letter or Underscore ( _ ).

  • Variables are case sensitive.

  • Variables may contain digit and letters.

  • Special character like ‘@’, “%’, ‘#’, ‘&’ cannot be used while declaring variable.

  • Eg. int a, float b, a_2 
Initialization of Primary Data types
  • Variable Initialization means assigning value to a variable.

  • DATA_TYPE  VARIABLE_NAME = VALUE.

  • The value of variable always come on the left side ( 10 = a) will be wrong.

  • Variable should be declared in the starting of C program before use.

  • Variable Initialization can be done in 2 different ways : ‘Compile Time’ & ‘Run Time’

  • Scope of a variable is decided by whether it is a Global Variable or Local Variable

Compile Time

#include "stdio.h"
int main()
{
   int a = 10;  
   float b;
   b = 10.56;
}
  
Run Time
  
#include "stdio.h"
int main()
{
   int a; 
   float b;  
   printf("Enter a : \n");
   scanf("%d", &a);
   printf("Enter b : \n");
   scanf("%f", &b);
}
  
Char

Char Data Type

  • Char is used to store the single character.
  • Char can store digit, letter or special character.
  • We use ‘ %c ‘ for initialization of a character data type.
Example of char
  
#include"stdio.h"
int main()
{
    char c= 'a';
    char d;
    d= 'b';
    printf("%c\t",c);
    printf("%c\t",c);
}
  
Taking character input in run time
  
#include"stdio.h"
int main()
{
    char x;
    printf("Enter x : ");
    scanf("%c",&x);
    printf("%c",x);
}
  
Float

Float Data Type

  • Float is used to store the real number.

  • It  consider numbers after decimal

  • For eg : float a = 12.75

  • We use ‘ %f ‘ for initialization
Float Example
  
#include"stdio.h"
int main()
{
    float x = 10.3897;
    float y;
    y = 20.4556;
    printf("%2f\t" x);
    printf("%3f\t" y);
}
  
Use of some common Data Types
  
#include"stdio.h"
int main()
{
    float x,y;
    printf("Enter x : ");
    scanf("%f",&x);
    y = 20.4556;
    printf("%2f\t", x);
    printf("%3f\t", y);
}
  
Range of the datatypes
  

#include "stdio.h"
#include "string.h"

int main()
{
    int a = -123456; //integers
    unsigned int b = 123456; // only positive integers

    short c = -123; // for small integers only
    unsigned short d = 123; //only pos.

    long e = -1234567890; //large integers
    unsigned long f = 1234567890; // only pos.

    long long g = -1234567890123; //Very large integers
    unsigned long long h = 1234567890123;//positive very large integers

    char i = 'Z'; //char is used to store characters
    char j[] = "CrackEx";

    //default precision is 6 unless specified for all below
    float k = 1.0001; // decimal values
    double l = 123456.0000000001; //large decimal values
    long double m = 1234567890.00000000000001; //very large decimal values 

    printf("%d\n",a);
    printf("%i\n",b);

    printf("%hi\n",c);
    printf("%hu\n",d);

    printf("%li\n",e);
    printf("%lu\n",f);

    printf("%lli\n",g);
    printf("%llu\n",h);

    printf("%c\n",i);
    printf("%s\n",j);

    printf("%f\n",k);
    printf("%lf\n",l);
    printf("%Lf\n",m);

    return 0;
}
  
Storage size of some common Data Types
  
int main()
{
    printf("Storage size for char is: %d \n\n", (int)sizeof(char));

    printf("Storage size for int is: %d \n", (int) sizeof(int));
    printf("Storage size for unsigned int is: %d \n\n", (int) sizeof(unsigned int));
    
    printf("Storage size for short is: %d \n", (int) sizeof(short));
    printf("Storage size for unsigned short is: %d \n\n", (int) sizeof(unsigned short));
    
    printf("Storage size for long  is: %d \n", (int )sizeof(long));
    printf("Storage size for unsigned long is: %d \n\n", (int )sizeof(unsigned long));    
    
    printf("Storage size for long long is: %d \n", (int )sizeof(long long));
    printf("Storage size for unsigned long long is: %d \n\n", (int )sizeof(unsigned long long));
    
    printf("Storage size for float is: %d \n", (int )sizeof(float));
    printf("Storage size for double is: %d \n", (int )sizeof(double));
    printf("Storage size for long double is: %d \n", (int )sizeof(long double));

    return 0; 
}
  
output
  
Storage size for char is: 1 

Storage size for int is: 4 
Storage size for unsigned int is: 4 

Storage size for short is: 2 
Storage size for unsigned short is: 2 

Storage size for long  is: 8 
Storage size for unsigned long is: 8 

Storage size for long long is: 8 
Storage size for unsigned long long is: 8 

Storage size for float is: 4 
Storage size for double is: 8 
Storage size for long double is: 16