Fundamental data type

Share This Post

In this tutorial, we will talk about the fundamental data types covering integer, character, float, double and long double.

1. Integer

Integer data types are used to store whole numbers.  It takes 2 or 4 bytes depending on machine. 1 byte = 8 bits and more size equals more content. To confirm the size of integer on your machine, you can use sizeof(int). Note that sizeof() is a unary operator not a function. For a machine whose size of integer is 2 bytes (16 bits) , range will be 0 – 65535 (2n-1).

To declare an integer varaible, we write

int my_var = 256

When declaring a variable as int, we can specify a modifier- Short, long, signed and unsigned. Modifiers make it possible to take more or less size. if int is 4 bytes, long int may be 8 bytes. By default, int data types are signed. This means they can take values for positive and negative integers. To make int store only positive values, use unsigned keyword. %d is used to print signed integer. %u is used to print unsigned integer. %ld is to print long signed integer while %lu is to print long unsigned integer. The rule of thumb in understanding modifiers is: sizeof(short) <= sizeof(int) <= sizeof(long).

 

2. Character

Character data type stores character data (e,g A-Z). Size of character data type is 1 bye long. To declarable character varaiable, we write

char va_name = ‘N’

Note the single quote. It is important to put single quote or you will get unexpected result. Character variable is able to hold one character at a time. It dioesn’t hold string.  we could also use integer values.

char var_name = 65

The value acts as a character if we use the character specifier format. Remember how we represent characters in 0 and 1 in our first lesson using binary numbers? This is because computers understand binary numbers. So there need to be a way to represent character in numbers. To encode characters, there are several scheme available. ASCII is one of the most commons.

In the code below, the associated character for the ASCII value 65 is A. So providing 65 is similar to providing character A.

 

3. Float, Double and Long Double

Like int data type is used to represent integer, char for characters, float, double and long double are used to represent fractional or real numbers. 3.14, 0.678. -3276.67. The data types are of different sizes depending on systems. On most systems, float takes 4 bytes, double takes 8 bytes and long double takes 12 bytes. See example code below.

 

As shown in the image above, float is able to represent fractional values precisely up to 7 digits. it prints everything as it is up to that point. But after that everything changes. Double and long double represents the values precisely up to 16 digits. Precision depends on the size of the data type. For less precision use float, for more precision use double or long double.

 

More To Explore