• admin@embedclogic.com

Data type In C

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.

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.

How to decide Data Type for a Variable

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

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.

Classification of Datatype

Data Types are mainly divided into three categories(Click On link for detail) –

Fundamental /Primary/Primitive data types

Derived/ Secondary/Non-Primitive data types

User-defined data types

See below classification diagram for detail.

Details about the fundamental data types

Fundamental typesMemory Size Range(for 32-bit platform)Format Specifier
char1 byte-128 to +127%c (%d for Numerical output)
signed char1 byte-128 to +127%c (%d for Numerical output)
unsigned char1 byte 0 to 255%c (%u for Numerical output)
short int2 byte-32768 to 32767%d
signed short int2 byte-32768 to 32767%d
unsigned short int2 byte0 to 65535%u
int4 byte-2147483648 to 2147483647%d
signed int4 byte-2147483648 to 2147483647%d
unsigned int4 byte0 to 4294967295%u
long int8 byte-9223372036854775808 to 9223372036854775807%ld
signed long int8 byte-9223372036854775808 to 9223372036854775807%ld
unsigned long int8 byte0 to 18446744073709551615%lu
float4 byte1.175494e-38 to 3.402823e+38%f or %e(for exponential output)
double8 byte2.225074e-308 to 1.797693e+308%lf or %le(for exponential output)
long double16 byte3.362103e-4932 to 1.189731e+4932%Lf or %Le(for exponential output)
void0 byte

Program

Write a program to print the size and acceptable data range by all data types?