What is datatype? List and explain different datatypes with examples.

1 year ago
C Programming

Datatype is an entity that defines set of values and set of operations that can be applied on those values.

Datatypes are broadly categorized as TWO types:

1)  Primitive Data Types: Fundamental data types or Basic data types are primitive data types.

Examples: int, char, float, double, void

 

2)  Non-Primitive Data Types: The derived or user-defined data types are called as non- primitive data types.

Examples: struct, union, pointers, etc.

  • int type:

The int type stores integers in the form of "whole numbers". An integer is typically the size of one machine word, which on most modern home PCs is 32 bits. Examples of whole numbers (integers) such as 1,2,3, 10, 100... When int is 32 bits, it can store any whole number (integer) between -2147483648 and 2147483647. A 32 bit word (number) has the possibility of representing any one number out of 4294967296 possibilities (2 to the power of 32).

int numberOfStudents, i, j=5;

In this declaration it declares 3 variables, numberOfStudents, i and j, j here is assigned the literal 5.

  • char type

The char type is capable of holding any member of the character set. It stores the same kind of data as an int (i.e. integers), but typically has a size of one byte. The size of a byte is specified by the macro CHAR_BIT which specifies the number of bits in a char (byte). In standard C it never can be less than 8 bits. A variable of type char is most often used to store character data, hence its name.

Examples of character literals are 'a', 'b', '1', etc., as well as some special characters such as '\0' (the null character) and '\n' (newline, recall "Hello, World").

Note that the char value must be enclosed within single quotations.

char letter1 = 'a';   /* letter1 is being initialized with the letter 'a' */ char letter2 = 97; /* in ASCII, 97 = 'a' */

In the end, letter1 and letter2 both stores the same thing the letter 'a', but the first method is clearer, easier to debug, and much more straightforward.

  • float type

float is short for floating point. It stores real numbers also, but is only one machine word in size. Therefore, it is used when less precision than a double provides is required. float literals must be suffixed with F or f, otherwise they will be interpreted as doubles. Examples are: 3.1415926f, 4.0f, 6.022e+23f. float variables can be declared using the float keyword.

  • double type

The double and float types are very similar. The float type allows you to store single-precision floating point numbers, while the double keyword allows you to store double-precision floating point numbers – real numbers, in other words, both integer and non-integer values. Its size is typically two machine words, or 8 bytes on most machines. Examples of double literals are 3.1415926535897932, 4.0, 6.022e+23 (scientific notation).

  • void type

It is an empty data type. It has no size and no value. It is used with functions which doesnot return any value, pointers,etc.

Range of values for char, int data type can be calculated with following formula

 

1) Range of Signed numbers (Both Positive and Negative numbers)

-2n-1                 to         + 2n-1 -1           where n is number of bits

Therefore,

  • char - 1 byte (8 bits)

range of values = -28-1 to +28-1 – 1 => -128 to +127

  • int - 2 bytes (16 bits in 16-bit OS)

range of values= -216-1 to +216-1 – 1=>-32768 to +32767

2) Range of UnSigned numbers ( Only Positive Numbers)

0                      to         + 2n-1 -1           where n is number of bits

Therefore,

  • char -           1 byte (8 bits)

 

range of values = 0      to +28 – 1 => 0 to 255

 

  • int -           2 bytes (16 bits in 16-bit OS)

 

range of values= 0       to +216 – 1=>0 to +65535

 

Size of each data type and ranges of values is given below.

char                             1 byte ( 8 bits )            range -128 to 127

int        16-bit OS :       2 bytes                        range -32768 to 32767

            32-bit OS :       4 bytes                        range -2,147,483,648 to 2,147,483,647

float                             4 bytes                        range 3.4E-38 to 3.4E+38 with 6 digits of precision 

double                         8 bytes                        range 1.7E-308 to 1.7E+308 with 15 digits of precision

void    generic pointer, used to indicate no function parameters, no retrun value etc.

 

Type Modifiers: Except for type void the meaning of the above basic types may be altered when combined with the following keywords.

  • signed
  • unsigned
  • long
  • short

Table: Use of modifiers to create modified data type

Primitive Data Type

Type Modifier

Modified Data Type

Char

Unsigned

signed

unsigned char

signed char

Int

Unsigned signed short long

unsigned int signed int short int short

unsigned short int unsigned short

 

 

 

 

signed short int signed short long int

long

unsigned long int unsigned long signed long int signed long

Float

N/A

N/A

Double

Long

long double

 

The signed and unsigned modifiers may be applied to types char and int and will simply change the range of possible values.

For example an unsigned char has a range of 0 to 255, all positive, as opposed to a signed char which has a range of -128 to 127.

An unsigned integer on a 16-bit system has a range of 0 to 65535 as opposed to a signed int which has a range of -32768 to 32767.

Note however that the default for type int or char is signed so that the type signed char is always equivalent to type char and the type signed int is always equivalent to int.

The long modifier may be applied to type int and double only. A long int will require 4 bytes of storage no matter what operating system is in use and has a range of -2,147,483,648 to 2,147,483,647.

A long double will require 10 bytes of storage and will be able to maintain up to 19 digits of precision.

The short modifier may be applied only to type int and will give a 2 byte integer independent of the operating system in use.

Examples:

short int, long int, long double, unsigned char, signed char, unsigned int, etc

 

Sanisha Maharjan
Jan 20, 2022
More related questions

Questions Bank

View all Questions

Top