Data Types in C

TCS NQT Registration Steps Large

Introduction to Data types in C

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.

  • Data types are broadly divided into two categories:
    • Primary data types.
    • Derived data types.

What are DataTypes ?

As we have discussed above, datatypes helps the compiler to understand the different types of data on which the programmer will be working. Datatypes are broadly divided into two types “

  • Primary Data types: 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. Primary data types are broadly classified as:
    • char
    • int
    • float
    • double
    • void
  • Derived Data types : These data types can be defined as the aggregation of the Primary data types, i.e , these are derived from the pre-existing data type. Derived data types are as follows:
    • array
    • pointers
    • structures
    • unions

Primary Data types

  • Primary data types are the one which are pre-defined in a programming language, which provide built-in support.
  • Each of these data types stores data of different types and different range.We know that we have four Primary data types, namely:
    • char
    • int
    • double
    • void
  • Each of  the primary data type is further divided into broader classifications as seen in the image beside.
  • Char is divided into char, signed char and unsigned char.
  • Int is classified as signed and unsigned int, signed and unsigned short int and signed and unsigned long int.
  • Float is classified as float, double and long double.
Primary Data Types in C Programming

Declaration of Primary Data types

  • C variable is a named location, where a program can operate the data.

  • DATA_TYPE    VARIABLE_NAME : Must begin with a Letter or Underscore ( _ ).

  • Variables are case sensitive.

  • Variables may contain digit and letters.

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

 

Declaration of primary data types

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 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.

Compile Time

int main()
{
    char c= 'a';
    char d;
    d= 'b';
    printf("%c\t",c);
    printf("%c\t",c);
}

Run Time

int main()
{
    char c;
    printf("Enter x : ");
    scanf("%c",&x);
    printf("%c",x);
}

Integer Data Type

  • Int is used to store the whole number.

  • It does not consider after decimal

  • For ex : int a = 12.75 is equal to the 12.

  • We use ‘ %d ‘ for initialization

Compile Time

int main()
{
   int x = 10;
   int y;
   y =20;
   printf("%d\t", x);
   printf("%d\t", y);
}
Output
10  20

Run Time

int main()
{
   int x;
   printf("Enter x : ");
   scanf("%d", &x);
   printf("%d\t", y);
}

Output
Enter x : 10 10

Float Data Type

  • float is used to store the real number.

  • It  consider after decimal

  • For ex : float a = 12.75

  • We use ‘ %f ‘ for initialization

Compile Time

int main()
{
    float x = 10.3897;
    float y;
    y = 20.4556;
    printf("%2f\t" x);
    printf("%3f\t" y);
}
Output
10.38 20.455

Compile Time

int main()
{
    float x,y;
    printf("Enter x : ");
scanf("%f") y = 20.4556; printf("%2f\t" x); printf("%3f\t" y); }
Output
10.38 20.455

Use of some common Data Types

#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[] = "PrepInsta";

//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

#include <stdio.h>
#include <limits.h>

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  

Range of some common Data Types

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>

int main()
{

    printf("CHAR_BIT : %d\n", CHAR_BIT);
    printf("CHAR_MAX : %d\n", CHAR_MAX);
    printf("CHAR_MIN : %d\n", CHAR_MIN);    
    printf("INT_MAX : %d\n", INT_MAX);
    printf("INT_MIN : %d\n", INT_MIN);
    printf("LONG_MAX : %ld\n", (long) LONG_MAX);
    printf("LONG_MIN : %ld\n", (long) LONG_MIN);
    printf("SCHAR_MAX : %d\n", SCHAR_MAX);
    printf("SCHAR_MIN : %d\n", SCHAR_MIN);
    printf("SHRT_MAX : %d\n", SHRT_MAX);
    printf("SHRT_MIN : %d\n", SHRT_MIN);
    printf("UCHAR_MAX : %d\n", UCHAR_MAX);
    printf("UINT_MAX : %u\n", (unsigned int) UINT_MAX);
    printf("ULONG_MAX : %lu\n", (unsigned long) ULONG_MAX);
    printf("USHRT_MAX : %d\n", (unsigned short) USHRT_MAX);

    return 0;
}
Output
CHAR_BIT : 8
CHAR_MAX : 128 
CHAR_MIN : -127
INT_MAX : 2147483647
INT_MIN : -2147483648
LONG_MAX : 9223372036854775807
LONG_MIN : -9223372036854775808
SCHAR_MAX : 127
SCHAR_MIN : -128
SHRT_MAX : 32767
SHRT_MIN : -32768
UCHAR_MAX : 255
UINT_MAX : 4294967295
ULONG_MAX : 18446744073709551615
USHRT_MAX : 65535