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
For 32-bit compiler the range is
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.
unsigned int for 32-bit compiler
Signed data type has both positive and negative values.
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
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
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.