Data types are very important concept in programming languages.You can choose the right kind of variable types for your program. The data types depends on the compiler which create 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 minutes. Machine codes are binary strings made of 0s and 1s. They are instructions or data for processor – Opcode or 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
[cc lang=”c” tab_size=”4″ line_numbers=”off”](2^16 /2) – 1 = (65536/2) – 1 = -32768 to + 32767[/cc]
For 32-bit compiler the range is
[cc lang=”c” tab_size=”4″ line_numbers=”off”](2^32 /2) – 1 = (4294967296/2) – 1 = -2147483648 to +2147483647[/cc]
Unsigned and Signed Integers
If you want all positive values then declare the int variable as unsigned. An unsigned data type disregard the sign bit, so that all the values are positive starting from 0.unsigned int for 16-bit compiler.
[cc lang=”c” tab_size=”4″ line_numbers=”off”]2^16 – 1 = 65536 – 1 = Range from 0 to 65535.[/cc]
unsigned int for 32-bit compiler
[cc lang=”c” tab_size=”4″ line_numbers=”off”]2^32 – 1 = 4294967296 – 1 = Range from 0 to 4294967295.[/cc]
Signed data type has both positive and negative values.
[cc lang=”c” tab_size=”4″ line_numbers=”off”]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
[cc lang=”c” tab_size=”4″ line_numbers=”off”]
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|
|long double||10 bytes|
An Example Program
[cc lang=”c” tab_size=”4″]
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);
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.