DataTypes Instructions

DataTypes Instructions

Primitive DataTypes

What are the reasons for data classification or types?

As we know, the software manages data, and as we learned in a previous blog, there are two types of data.

Let's discuss the major reasons:

  • Different types of data hold different memories in the memory location of the computer.

  • Different binary representations of different types of data.

  • Different kinds of operations are performed on different types of data.

Note: The above three points may not be clear to you. Do not worry; keep reading.

Where do we use data types?

We use datatypes to specify the type of data (value) stored in a variable.

Data types in C

Primitive Data Types: Primitive data types are those that are predefined, which means they do not need to be defined. Primitive datatypes include the following:

  • Int

  • char

  • float

  • double

  • bool

  • void

Note: Primitive data types are also known as "fundamental data types," because sometimes we need to define new datatype so we use primitive data types to define them.

Non-primitive Data Types: Non-primitive data types are those that are not predefined, it is defined by us. Non-primitive datatypes include the following:

  • Structure

  • Union

  • Enum

  • Typedef

Note: Non-primitive data types are also known as "user-defined data types." In this blog, we discuss only primitive datatype types like int, char, float, and double. One more thing: we also discuss derived datatypes when we learn arrays.

Overview of bytes and bits

Computer memory like RAM and hard disks generally measure in bytes and bits.

1 byte = 8 bits A bit memory unit is smaller than a byte.

Variable declaration using primitive data types

Int Data Type: When we create a variable using the int data type, the variable occupies 4 bytes in the memory which stores integer constants.

Syntax:

int num = 5;

int num = 5;

Above line is known as "Variable declaration using int datatype instruction."This tells the compiler to make a memory block of four bytes in memory whose name is num (this name is also given to the block by the compiler) and assign 5 to the num variable. When we assign a value to a variable during the declaration of the variable, this is known as the "initialization" of the variable. The symbol (;) is like a full stop, which means a line ends here.

Char Data Type: When we create a variable using the char data type, the variable occupies 1 byte in memory which stores character constants.

syntax:

char ch = 'A';

Float Data Type: When we create a variable using the float data type, the variable occupies 4 bytes in memory which stores real constants.

syntax:

float f1 = 3.123;

Double Data Type: When we create a variable using the double data type, the variable occupies 8 bytes in memory which stores real constants.

syntax:

double d1 = 3.14159265359;

We know how integers and real constants convert into binary but how do character constants convert into binary?

In C, character constants are mapped to a character code. The character code is also known as the ASCII code, and this method is known as character encoding. So, when we declare a char variable, we store a character constant according to us, but according to the compiler, it is a character code that is an integer value.

The ASCII value or code of the lowercase and uppercase alphabets is:

Character Hex  Character  Decimal  Hex  Character
code  
-------  ---  ---------  -------  ---  ---------
65       41   A          97       61   a
66       42   B          98       62   b
67       43   C          99       63   c
68       44   D          100      64   d
69       45   E          101      65   e
70       46   F          102      66   f
71       47   G          103      67   g
72       48   H          104      68   h
73       49   I          105      69   i
74       4A   J          106      6A   j
75       4B   K          107      6B   k
76       4C   L          108      6C   l
77       4D   M          109      6D   m
78       4E   N          110      6E   n
79       4F   O          111      6F   o
80       50   P          112      70   p
81       51   Q          113      71   q
82       52   R          114      72   r
83       53   S          115      73   s
84       54   T          116      74   t
85       55   U          117      75   u
86       56   V          118      76   v
87       57   W          119      77   w
88       58   X          120      78   x
89       59   Y          121      79   y
90       5A   Z          122      7A   z

The ASCII value or code of the digits is:

Character Hex  Character
code  
-------  ---  ---------
48       30   0
49       31   1
50       32   2
51       33   3
52       34   4
53       35   5
54       36   6
55       37   7
56       38   8
57       39   9

Why use the int data type if the char data type stores an integer value?

It is possible to store integer values in char data types variables but we can store integer values up to a certain limit in char data types variables. This is due to the char datatypes variables occupying 1 byte in memory but on the other hand, the int data type variable occupies 4 bytes in memory.

Let's compare float and double dataTypes

We know both data types are used to create the types of variables that store real constants. So, questions arise when we use float or double data types. Just remember one thing in your mind if we have a big real constant like the value of pi then always use double because it stores more accurately the value of pi as compared to float. This is due to double datatype variables occupying 8 bytes in the memory but on the other float datatype variables occupying 4 bytes in the memory.

Data Types value range

Data Type Range of Values

char - 128 to 127 or 0 to 255

int -2147483648 to 2147483647

float approximately -3.4e38 to 3.4e38

double approximately -1.7e308 to 1.7e308

Conclusion:

You covered the primitive data types of C in this blog. You can now research more about it online. If you'd like, you can connect with me on Twitter.

Gratitude for reading.