Data Types in C Language

DataTypes are used for declaring variables and functions of different types.

When program store data in a variable such as an integer, character floating etc, It is necessary that each variable must be assigned a specific data type.

Also, It helps the compiler to reserve memory for variable and how to read and write them.

There are two categories of Data types:

Primitive/Basic/Primary Datatypes

User-defined Datatypes

Primitive Datatypes

There are following primitive or primary datatypes:

int (integers)

char (characters)

float (floating point numbers)

double

void (void datatype)

Integers

Integers are whole numbers that represent positive and negative numbers, such as 3, 2, 1, 0, 1, 2, and 3, but not decimal or fractional numbers.

Integer data types hold a maximum of four bytes of information and are declared with the int (short for integer) keyword, as shown in the following line of code.

1

intx;

In C, you can declare more than one variable on the same line using a single int declaration statement with each variable name separated by commas, as demonstrated next.

1

intx,y,z;

The preceding variable declaration declares three integer variables named x, y, and z. Remember that executable program statements such as a print statement or in this case a variable declaration require a statement terminator (;).

Floating-Point Numbers

Floating-point numbers are all numbers, including signed and unsigned decimal and fractional numbers. Signed numbers include positive and negative numbers whereas unsigned numbers can only include positive values. Examples of floating-point numbers are shown in the following list:

09.4543

3428.27

112.34329

342.66

-55433.33281

Use the keyword float to declare floating-point numbers, as shown next.

Characters

Character data types are representations of integer values known as character codes. For example, the character code 90 represents the letter Z.

Note that the letter Z is not the same as the character code 122, which represents the letter z (lowercase letter z).

Characters represent more than just the letters of the alphabet; they also represent numbers 0 through 9, special characters such as the asterisk (*), and keyboard keys such as the Del (delete) key and Esc (escape) key. In all, there are a total of 128 common character codes (0 through 127), which make up the most commonly used characters of a keyboard.

Character codes are most notably organized through the ASCII (American Standard Code for Information Interchange) character set.

ASCII or American Standard Code for Information Interchange is noted for its character set, which uses small integer values to represent a character or keyboard values.

In C, character variables are created using the char (short for the character) keyword as demonstrated next.

1

2

3

charfirstInitial;

charmiddleInitial;

charlastInitial;

Character data assigned to character variables must be enclosed in single quotes (‘), also known as tick marks or apostrophes, and the equal sign (=) is used for assigning data to the character variable.

Double

double-precision floating value is used to store real number with high precision of fractional part ( e.g 2.44445453, 5.4445453543, 4.45353453453, 45.453534534534 ).

Void

void means no value. the void is used to represent that it has no value. Generally used for mentioning function return type when the function has nothing to return.

The void uses void as a datatype.

Following are the list of basic data types. It’s size is given according to 32-bit architecture.

Data Types

Memory

Range

char or signed char

1 byte

-128 to 127

unsigned char

1 byte

0 to 255

int or signed int

2 byte

-32,768 to 32,767

unsigned int

2 byte

0 to 65535

short int or signed short int

1 byte

-128 to 127

unsigned short int

1 byte

0 to 255

long or signed long

4 bytes

-2,147,483,648 to 2,147,483,647

unsigned long

4 bytes

0 to 4,294,967,295

double

8 bytes

1.7E -308 to 1.7E +308

float

4 bytes

3.4E -38 to 3.4E +38

long double

10 bytes

3.4E -4932 to 1.1E +4932

User-defined data type

The user-defined data type is built upon basic type.

Array

The array represents multiple continuous elements of given data type. For example,

1

2

3

4

intnums[10];

// Here 'nums' is an array of 10 elements of integer kind.

charname[12];

// Here 'name' is an array of 12 elements of character kind.

Structure

The structure represents different kind of data type as a single record. For example,