You are on page 1of 6

Data types are divided into following three categories,

(a)Basic or Primary data types


(b)Derived data types
(c)User-defined data types

Primary Data Types in C

All the C compilers support five Primary(Built-in) Data Types, namely int, char, float, double and void.
They are argumented by using data type qualifiers such as short, long, signed and unsigned.
Integer Data Types in C
Integers are whole numbers without a fractional part. In C, the keyword int is used to represent integer
quantity. They generally require 2 bytes of memory for their storage. It is a signed data type, i.e. it can
be used to store both positive as well as negative values. The range of values that variables of this type
can hold lies between -32768 to +32767. This range can be calculated by using the formula -2 x-1 to +2x-
1
-1 where x is the total number of bits used to store the signed integer type. Here, 1 has been subtracted
as one bit is reserved for the signed bit and the remaining for the magnitude. Here we have subtracted 1
from the upper limit of the range because 0 is also included.

For example : A variable of int type requires 2 bytes (= 2 x 8 = 16 bits). Out of this, 1 bit is reserved for
sign (-or+) and remaining 15 bits can contain 0 or 1. So the range of values supported will be -2 15 (-
32768) to +215-1 (+32767).
Therefore, out of the allocated memory space for integers, the leftmost bit is reserved for the number
sign. If this bit is 0, the number is positive, and if it is 1, the number is negative. However, it decreases
the size of memory and hence the maximum value that can be stored in the allocated memory to half
because we are losing the most significant bit.
The declaration of a variable length of int type is as follows,
int length ;
The variable length can store integers lying in the range -32768 to +32767. The conversion character
used with this data type is %d . We shall use this conversion character while printing variables of type
int.
Integers also differentiated into the following three categories according to their size:
(i) For variables having small integer values, the type code is short int or short.
(ii) For variables having an intermediate range of values, the type code is int.
(iii) For variables having large integer values, the type code is long int or long.
(iv) For variables having very large values, the type is long, long int, or only long long.
Size Numerical range
Conversion
Qualifier (Bytes in
Minimum Maximum Character
memory)
short 1 -128 (= -27) +127(=27-1) %hd
unsigned
1 0 255 %hu
short int
unsigned int 2 0 65535(=+216-1) %u
31 31
long 4 -2,147,483,648 (-2 ) +2,147,483,647 (-2 -1) %ld
unsigned long 4 0 4,294,967,295 (+232 -1) %lu
Note: The amount of memory occupied by variables of these types varies from computer to computer.
The int type only provides a range of integers to be operated on. But while making programs, we may
require integers beyond +32767 and integers below -32768. So to solve this problem, C provides certain
qualifiers like short, long, unsigned that extends the functionality of the int type. These qualifiers are
prefixed before int can be used by simply using the qualifier before the variable name in the variable
declaration. The table enlists the various qualifiers that can be used with int type.

Short Data Type in C

In certain programs, you may not need integers as large as the standard int type provides. In such cases,
C provides a short qualifier that can be prefixed before the int in the variable declaration. Variables of
such type require 1 byte for their storage and can support values ranging from -128 (-2 7) to +127 (+27-1).
Generally, in a 32-bit computer system, the size of a Short (also written as short int) is 2 bytes (16 bits)
are allocated for storage of short int; out of the 16 bits, the leftmost bit reserved for the sign. Therefore,
only 15 bits are available for the storage of the value.
Therefore, the values of short or short int or signed short int variables may vary from -32,768 to 32,767.
As mentioned above, for the positive numbers, the leftmost bit is 0; for negative numbers, the leftmost
bit is 1, and the remaining bits store complement of the number plus 1.
The declaration of a variable temperature of short int type is as follows,
1 short int temperature ;
You can also put the qualifier short before the variable name in the variable declaration.
1 short temperature ;
The variable temperature can store integers lying in the range -128 to +127. The conversion character
used with it is %hd.

Long Integers
In some programs, you may need very large integers. But we can specify only a small range of integers
with standard int type. Therefore, C provides keyword long that extends the range of int type variables.
The variables of long int requires 4-byte for their storage and therefore can store an integer value that
can range from -2,147,483,648 (-231) to +2,147,483,647 (+231-1).
A variable distance of type long int can be declared as
long int distance ;
You can also put the qualifier long before the variable name in the variable declaration,
long distance ;
The variable distance can store integer lying in the range -2,147,483,648 to +2,147,483,647.
The conversion character used with such a variable is %ld.

Unsigned Integers
In some programs, you may need only non-negative integers such as the number of pages in a book, the
number of players in a football team, and age, etc. C provides a keyword unsigned that can use with int,
short, and long to specify positive integers. The range of such type variables lies between 0 to 2 x – 1,
where x is the total number of bits need to store the unsigned integer type. Here, we have subtracted 1
from the upper limit of the range because 0 included. When unsigned used in association with int type,
the range will lie between 0 and 65535 (216-1).
A number in this sequence is the address of the byte. This number has to be positive. Such numbers are
called unsigned numbers.
The signed numbers can be positive or negative, but unsigned numbers are always positive. Unsigned
numbers are also categorized according to their size, as shown in Table.
Range of values of unsigned numbers

Type Memory Maximum value Range of values


allocated

unsigned char 1 byte 2 8– 1 0 to 255

unsigned short 2 bytes 216– 1 0 to 65535


int

unsigned int 4 bytes 232– 1 o to 4294967295

unsigned long 4 bytes 232– 1 0 to 4294967295


int

unsigned long 8 bytes 264– 1 0 to 18446744073709551615


long int

We can use the entire allocated memory space for storing an unsigned number because no bit requires
representing the number’s sign. Therefore, the maximum value of an unsigned number stored in a given
memory space is double that of signed numbers.
Variable pages of unsigned int type can be declared as follows,
1 unsigned int pages;
The variable pages can store integers lying in the range 0 to 65535. The conversion character used with
unsigned int is %u.
As discussed above, unsigned can also use in association with short and long. For example
1 unsigned short int age;
2 unsigned long int count_of_people;
3 unsigned short a;
4 unsigned long b; /* b is of unsigned long int type */
5 unsigned x; /* x is of unsigned int type */
The age variable of unsigned short int type can store integers lying in the range 0 to 255. The conversion
character used with the variable of an unsigned short int is %hu.
The variable count_of__people is of unsigned long int type and can store integers lying in the range 0 to
4,294,967,295 (232 – 1). The conversion character used with variables of this type is %lu.
The following program would clear your logic.
1 main() {
2 short a,b;
3 long x;
4 unsigned i;
5 unsigned long int j;
6 ..................
7 ..................
8 scanf ("%hd%ld%u", &a,&x,&i);
9 printf {"%hd%ld%u" ,a,x, i);
10 ..................
11 ..................
12 scanf ("%hd%lu",&b, &j);
13 printf ("%hd%lu",b, j);
14 ..................
15 }
You can find the value ranges of integer types for your C compiler in the header file limits.h, which
defines macros such as INT_MIN and INT_MAX and many more. The INT_MIN determines the
minimum value that an int type can store, and INT_MAX determines the maximum value that an int type
can store. In addition to these macros, it also contains macros such as UINT_MAX, LONG_MAX,
LONG_MIN, CHAR_MAX, CHAR_MIN. To see all of these and more, see the limits.h header file.
Now let us consider the following program.
To display the maximum and minimum range of some data types?
#include<stdio.h>
1
#include<limits.h>
2
main() {
3
printf ("Minimum value of int = %d", INT_MIN);
4
printf ("\nMaximum value of int = %d", INT_MAX);
5
printf ("\nMaximum value of unsigned int = %u",
6
UINT_MAX);
7
printf("\nMinimum value of long int = %ld",LONG_MIN);
8
printf("\nMaximum value of long int = %ld",LONG_MAX);
9
}
Output :
1 Minimum value of int = -32768
2 Maximum value of int = 32767
3 Maximum value of unsigned int = 65535
4 Minimum value of long int = -2147483648
5 Maximum value of long int = 2147483647

Character Type
A variable may have value in the form of a character. The type of such variables is char. The char type
represents only a single character, which may be a letter or a digit or a punctuation mark and so on. It
generally takes 1 byte of memory. Characters assigned to a variable of the char types are stored using its
ASCII code, an integer. For instance, character.
In C language, the characters define as integer constants according to ASCII code. This code comprises
Latin alphabets from A to Z in uppercase and a to z in lowercase, digits 0 to 9, symbols such as+, -, *,
etc.
According to this code, ‘A’ is represented in memory by its ASCII code equivalent, an integer 65. The
char type can be either signed or unsigned. As a signed type, the value stored in a variable of type char
can range from -128 to +127. In the list, the value of each character differs from that of the previous by
1. In the case of unsigned type, the value stored ranges from 0 to 255. By default, the character data type
is treated as an unsigned character. The conversion character used with this data type is %c.
Some examples are,
1 char letter = 'B';
2 char digitl = '5';
3 char symbol = '#';
4 char newline = '\n';
The following program would tell how characters are accessed and declared.
1 main() {
2 char ch = 'a';
3 printf("\nASCII code of %c is %d",ch,ch);
4 }
Here, in the declaration statement, we have declared a variable ch of type char and assigned value a to it.
In the printf() statement, we are printing its value and corresponding ASCII equivalent.
So the answer is ASCII code of a is 97.
Therefore %d is the conversion character used with characters to print the ASCII values.
The ASCII code supports only Latin alphabets and other symbols with values from 0 to 127 and can
store in 1 byte of memory. Other languages did not represent by ASCII code. For defining the alphabets
of major languages of the world, the International Organization for Standardization (ISO) has created a
Universal Character Set (UCS) code.
It designates as ISO 10464 standard. In this code, a character allocates 4 bytes for storing its value. It is
known as UCS-4. Before this standard adopts, the Unicode (also called UCS-2) allocated 2 bytes for a
character was prevalent. The Unicode standard has also been modified to 4 bytes by ISO standard. The
new ISO standard supports all the three, that is, ASCII, UCS-2, and UCS-4. The revised C standard
supports the ISO standard.
Floating Point Numbers
The integer types do not provide the facility to represent decimal numbers so whenever you define a
number with a decimal such as 3.142, use a floating-point data type. If you try to use an integer type to
store a number with a fractional part, the fractional part will discard. C provides two standard floating-
point types: float and double to store numbers with fractional parts. These give you a choice in the
number of digits precision available to represent your values in the range of values that can
accommodate.
Float: The float data type requires 4-bytes (32-bits) for their storage. Consider that the number 52.5 is to
be stored. In a binary number system, it writes as 110100.1. The number store as .1101001 x 26. On a
32-bit system, the number 1101001 is stored on the 23-bit segment of the 32 bits, while the power 6 or
110 in binary floated, that is, it is stored separately on the 8-bit segment of the remaining 9 bits. The
leftmost bit (32nd bit) is kept for the sign (+ or -).
For positive numbers, this bit takes the value 0, and for negative numbers, this bit has value 1. The
scientific notation for floating-point numbers is the exponential notation. The number 7685.43 may write
as 7.68543 x 103. In C language, the scientific notation of the number 7685.43 may express as 7.68543
e+3 or 7.68543 E+3.
The data types for floating-point numbers are float, double, and long double. The distinction between
float and double is according to the precision, that is, the number of digits after the decimal point. On
most 32-bit systems, a float has 7 significant digits and allocate 4 bytes of memory, whereas a double
has 15 significant digits, nearly double the precision of float. It may get 8, 10, 12, or 16 bytes for storage
on some systems. The memory spaces generally allocate for a float, double, and long double given in
Table.
Memory allocated for floating point numbers

Type Memory Range of values that


allocated may be stored

Float 4 ± (3.4 e-38 to 3.4 e+38)

Double 8 ± (1.7 e-308 to 1.7 e+308)

long double 10 ± (3.4 e-4932 to 1.1


e+4932

A variable average of float data type can be declared as follows,


1 float average;
The conversion character used with this data type is %f usually. To represent the result in exponential
form, the conversion character %e used.
Double: In certain situations, like meteorology, we need to work with large decimal numbers that
require a high degree of accuracy. The float type is inappropriate in such cases. Here, we use the double
data type. The double data type uses 8 bytes (64 bits) for their storage. Their digits of precision are 15,
and their range lies between 1. 7E-308 to +1.7E308.
A variable sunDistance of double type can be declared as follows,
1 double sunDistance;
The conversion characters used with double type are %lf,%le,%lg.
The only qualifier that can use with double type is long. A variable of long double type requires 10 bytes
of memory. For example :
1 long double a,b;
The conversion characters that can use with this data type is %Le,%Lfand %Lg.
void TYPE
The void type designated by the keyword void; does not have any values or operations. It is primarily
used to indicate that.
• A function does not return a value. For instance, void factorial (int n);
• The function does not have any parameters. For example, int date (void) ;
• A generic type, which means that it can represent any of the standard types.
Now let us consider the following program,
1 #include<stdio.h>
2 void func_hello(void); /*function prototype*/
3 main() {
4 func_hello(); /*function call*/
5 }
6 void func hello(} /*function definition*/
7 {
8 printf ("Hello Everbody");
9 }
Here, void written before the function name func_hello indicates that this function does not return
anything. The void written in parentheses indicates that this function accepts no arguments.
C also supports the pointer to void type ( specified as void ). A program can convert a pointer to any type
of data to a pointer to void and back to the original type without losing information.

You might also like