Data types are a very important concept in programming languages. You can choose the right kind of variable types for your program. The data types depend on the compiler which creates machine codes suitable for 16-bit or 32-bit processors.
Sometimes when the processor has backward compatibility, then a 32-bit processor can run a 16-bit machine code successfully.
Integer Data Types
An integer data type is declared using keyword – int
. We will have a separate discussion on keywords so let’s not worry about them right now.
If you think of memory, then think of sequence of bytes with an address called a memory address. A memory address is a hexadecimal value. One byte of information is 8-bit of information.
Integer Data Types | Required Storage |
---|---|
int | 2 or 4 bytes |
short int | 2 bytes |
long int | 4 bytes |
On a 16-bit compiler like turbo C++, the int type has requires two bytes storage and on a 32-bit compiler, storage requirement is 4 bytes.
Short int
is always 2 bytes in size and long int is always 4 bytes in size.
Range of the data types
What does it mean to have a range of values? We will explain that in a minute. Machine codes are binary strings made of 0s and 1s. They are instructions or data for processor – opcode
and operand
.
So, the range of a data type is nothing, but number of possible binary strings that is allowed using the byte storage. In other words, ” How many numbers can you store in binary format using 2 or 4 bytes of memory space ?”
For 16-bit compiler the range is
\begin{aligned}\left(\frac{2^{16} }{2}\right )- 1 = \left(\frac{65536}{2}\right ) - 1 = -32768 \hspace{2px} to\hspace{2px} +32767\end{aligned}
For 32-bit compiler the range is
\begin{aligned}\left (\frac{2^{32} }{2}\right ) - 1 =\left (\frac{4294967296}{2}\right)- 1 = -2147483648 \hspace{2px} to \hspace{2px} +2147483647\end{aligned}
Unsigned and Signed Integers
If you want all positive values, then declare the int variable as unsigned. An unsigned
data type disregards the sign bit, so that all the values are positive starting from 0. The unsigned int
for 16-bit compiler.
\begin{aligned}2^{16} - 1 = 65536 - 1 = Range \hspace{2px} from \hspace{2px}0\hspace{2px} to \hspace{2px}65535\end{aligned}
unsigned int for 32-bit compiler
\begin{aligned}2^{32} - 1 = 4294967296 - 1 = Range \hspace{2px} from \hspace{2px} 0 \hspace{2px} to \hspace{2px}4294967295.\end{aligned}
Signed data type has both positive and negative values.
For example,
signed long int;
signed short int;
Character Data Types
The character data type takes 1 byte of storage. The signed char and char are the same.
Unsigned and Signed Characters
signed char ; /* Range from -128 to + 127 */
unsigned char; /* Range from 0 to 255 */
Each character has a number and char cannot take values greater than -128 to +127.
Float and Double Data Types
The float and the double data types take 4 bytes and 8 bytes respectively. The long double take 10 bytes of storage.
Data Types | Required Storage |
---|---|
float | 4 bytes |
double | 8 bytes |
long double | 10 bytes |
An Example Program
#include <stdio.h>
int main()
{
char p = 'a';
int s = 10;
long int q1 = 1000;
short int q2 = 223;
float b1 = 23.55;
double b2 = 33.5545;
signed char ch = 'h';
signed int g1 = -33;
unsigned char ch1 = 'D';
unsigned int g2 = 234;
unsigned long int num = 333234;
unsigned short int num2 = 2442;
printf("char = %c\n", p);
printf("int = %d\n",s);
printf("long int = %ld\n",q1);
printf("short int =%d\n",q2);
printf("float = %f\n",b1);
printf("double = %f\n",b2);
printf("signed char= %c\n",ch);
printf("signed int = %d\n", g1);
printf("unsigned char = %u\n",ch1);
printf("unsigned int = %u\n",g2);
printf("unsigned long int = %u\n",num);
printf("unsigned short int = %u\n",num2);
system("PAUSE");
return 0;
}
Output
char = a
int = 10
long int = 1000
short int =223
float = 23.549999
double = 33.554500
signed char = h
signed int = -33
unsigned char = 68
unsigned int = 234
unsigned long int = 333234
unsigned short int = 2442
References
- Balagurusamy, E. 2000. Programming in ANSI C. Tata McGraw-Hill Education,.
- Brian W. Kernighan, Dennis M. Ritchie. 1988. C Programming Language, 2nd Edition. Prentice Hall.