Data Types In C With Real Life Example

Data Types:

  • Data types are an important attribute of an identifier in any language because all operations are type checked by compiler for the compatibility.

  • Data type determines all the possible values that an identifier can have and valid operations that can be applied on it. Illegal operations will not compile by compiler.

C language has the following categories:

Primitive / Basic Data Types

  • Character (Char)
  • Integer (Int)
  • Single Precision Floating Point (Float)
  • Double Precision Floating Point (Double)
  • No value Available (Void)

Derived Data Types

  • Array Types
  • Pointer Type
  • Function Types

User Defined Data Types

  • Structure
  • Union
  • Enumeration

Data types & their memory requirements:

SR Data Types Memory in Bytes
1 char 1 Byte
2 int 2 Byte
3 float 4 Bytes
4 double 8 Bytes
5 signed Same as data types 1, 2
6 Unsigned Same as data types 1, 2
7 short int 2 Bytes
8 long int 4 Bytes
9 long float 8 Bytes
10 long double 10 Bytes

Format Specifiers in C Language

S.No Data Type X Format Specifies Remark
1 Char C %c Single character
2 Int I %i Signed Integer
3 Int D %d Signed Integer in decimal number system
4 unsigned int O %o Signed integer in octal number system
5 unsigned int u %u Signed integer in decimal number system
6 unsigned int X %x Signed integer in hexadecimal number system
7 unsigned int X %X Signed integer in hexadecimal number system
8 long int Id %%Id Signed long
9 short int Hd $hd Signed short
10 unsigned long Lu %lu Unsigned long
11 unsigned short Hu %hu Unsigned short
12 float F %f Signed precision float
13 Double Lf %If Signed double precision float
14 String type S %s String
15 Pointer type P %p Pointer

Program

  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. main()  
  4. {  
  5.    printf("********* size of various data types *********\n");  
  6.    printf("\n character takes %d bytes in memory \n"sizeof(char));  
  7.    printf("\n integer takes %d bytes in memory \n",sizeof(int));  
  8.    printf("\n float takes %d bytes in memory \n",sizeof(float));  
  9.    printf("\n double takes %d bytes in memory \n",sizeof(double));  
  10.    getch();  
  11. }  
Output

Otput

Real life example

Suppose if we are giving any interview then let say interviewer ask the following questions:

CTC

Question: What is your designation in your current company?

Answer: Software Developer

In the above scenario whenever we think about answer, we decide the data type of our answer like it will be character string.

Question: What is your current CTC?

Answer: x Lac

In the above scenario whenever we think about answer, we decide data type of our answer like it will be in numbers (can be integer or float).

Summary

This article will help fresher candidates to understand Data Types in C with real life example.

Hope you enjoyed this one.