Although the term “Data Type” describes itself yet I will tell you in one sentence that “A data type defines all about the variable behavior”.
you can tell every detail(nature, limit, size) about a variable if you know about its data type.
1 |
signed short int input1; |
So, input1 is an :
— Integer type variable
— Which holds +ve and -ve values
— Between range -32768 to +32767.
And all you can predict about input1 because of its data type signed short int.
Let’s say you are writing a program for two number addition.To write this program you must be clear about the user’s input behavior like –
— Type of Input Data i.e integer, decimal, positive, negative
— Range of input data
let’s assume as a user I will input
— Integer value only
— Positive and negative both
— Input data between -32,000 to +32,000
So as a programmer I will declare following variable at 32- bit platform –
— Input1 and Input2 —– > For storing user input in memory
— result ———————> to store the result of the operation
1 2 3 4 5 |
signed short int input1; /*memory size=2 byte ,data limit=-32768 to +32767*/ signed short int input2; /*memory size=2 byte ,data limit=-32768 to +32767*/ signed int Result; /*memory size=4 byte ,data limit=-2147483648 to +2147483647*/ |
Note:
Data type of Result is signed int to accept the correct result of extreme inputs of signed short int inputs.
If input1 =20000 , input2 = 20000
Result =40,000
40,000 is beyond the short int range.So Result has signed short int data type.
Data Types are mainly divided into three categories(Click On link for detail) –
Fundamental /Primary/Primitive data types
Derived/ Secondary/Non-Primitive data types
See below classification diagram for detail.
Fundamental types | Memory Size | Range(for 32-bit platform) | Format Specifier |
---|---|---|---|
char | 1 byte | -128 to +127 | %c (%d for Numerical output) |
signed char | 1 byte | -128 to +127 | %c (%d for Numerical output) |
unsigned char | 1 byte | 0 to 255 | %c (%u for Numerical output) |
short int | 2 byte | -32768 to 32767 | %d |
signed short int | 2 byte | -32768 to 32767 | %d |
unsigned short int | 2 byte | 0 to 65535 | %u |
int | 4 byte | -2147483648 to 2147483647 | %d |
signed int | 4 byte | -2147483648 to 2147483647 | %d |
unsigned int | 4 byte | 0 to 4294967295 | %u |
long int | 8 byte | -9223372036854775808 to 9223372036854775807 | %ld |
signed long int | 8 byte | -9223372036854775808 to 9223372036854775807 | %ld |
unsigned long int | 8 byte | 0 to 18446744073709551615 | %lu |
float | 4 byte | 1.175494e-38 to 3.402823e+38 | %f or %e(for exponential output) |
double | 8 byte | 2.225074e-308 to 1.797693e+308 | %lf or %le(for exponential output) |
long double | 16 byte | 3.362103e-4932 to 1.189731e+4932 | %Lf or %Le(for exponential output) |
void | 0 byte |
Write a program to print the size and acceptable data range by all data types?