Data Types in C

data type in c

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.

Types of  Data Type

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 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:
    • char
    • int
    • float
    • double

Declaration of Primary Data types

  • DATA_TYPE  VARIABLE_NAMEMust 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
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 x;
    printf("Enter x : ");
    scanf("%c",&x);
    printf("%c",x);
}

Integer Data Type

  • Int is used to store the whole number.

  • It does not consider digits  after decimal

  • For eg : 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);
}

Run Time

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

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

Compile Time

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

Run Time

#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);
}

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

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

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

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

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription