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
- #include<stdio.h>
- #include<conio.h>
- main()
- {
- printf("********* size of various data types *********\n");
- printf("\n character takes %d bytes in memory \n", sizeof(char));
- printf("\n integer takes %d bytes in memory \n",sizeof(int));
- printf("\n float takes %d bytes in memory \n",sizeof(float));
- printf("\n double takes %d bytes in memory \n",sizeof(double));
- getch();
- }
Output Real life example
Suppose if we are giving any interview then let say interviewer ask the following questions:
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.