Data Type in C

Views: 929
Created On: 25 Aug, 2012 Modified On: 08 Feb, 2013

Data Type

In C there are 4 primary in-built datatypes:
int, char, float, double
a lot more data types can be derived from these primitive data types by using modifiers. C provide a total of 4 modifiers:
signed, unsigned, long, short
Above specified 4 modifiers can be used only with int. With char data type only signed and unsigned modifiers can be used. With double data type only long can be used and with float data type none of the modifiers can be used.

Using these data-types variables and constants are declared. Variables must be declared at the starting of program else compiler will return an error.


char and signed char are similar. Char can be assigned a value ranging from -128 to 127.

char ch = 65;

In above assignment, ch contains 65 which is ASCII code for alphabet A, so when ch will be printed the output would be alphabet A. printf('%c',ch):


When assignment goes beyond range, it starts oscillating between the valid range i.e. if an signed char or char is assigned value 128 then it will contain value -127, similarly if an char is assigned value -129 it will contain value 127.
signed char ch1 = -149; printf("%d %c",ch1,ch1); O/P 107 k
Description: signed char can store ASCII code from -128 to 127 and input to variable ch1 is -149 which is outside the specified range so integer O/P that is obtained using %d for variable ch1 will be (127-(149-129))i.e. 107 and character/symbol O/P for input value is 'k'.

unsigned char can be assigned value ranging from 0 to 255. The oscillation effect faced in char and signed char is present here also but here the values oscillate b/w range 0 to 255.

unsigned char ch2 = 249; printf("%d %c",ch1,ch1); O/P 249
Description: unsigned char can store ASCII code from 0 to 255 and input to variable ch2 is 249 which is within specified range so integer O/P that is obtained using %d for variable ch2 is still 249 and character/symbol O/P for input value is ' .'.

float, double

float data type can have value between range -3.4e38 to 3.4e38. By default float variable contain real number with 6 digit precision.
float fl=1234567.123456789; printf("%f",fl); O/P 1234567.125000
Description: Value assigned to fl is 12354567.123456789 but value printed as O/P is 1234567.125000 this is because the value assigned to variable fl goes beyond its limit therefore an O/P with rounded value is printed.

If still longer precision is required double data type can be used and if still longer precision is needed then long double can be used. Range for double is -1.7e308 to 1.7e308 and for long double range is -1.7e4932 to 1.7e4932.

double fl1=1234567.1234567891011; printf("%.14lf",fl1); O/P 1234567.12345678918000;
Description: Value assigned to variable fl1 is 1234567.1234567891011 but the value printed as O/P is 1234567.12345678918000 this is because in print statement we specified length of precision i.e. 14 for variable fl1 but it is beyond scope of double data-type so a O/P with rounded value is printed. float uses 4bytes, double uses 8bytes and long double uses 10 bytes.


int data type have a range from -32768 to 32767 and requires 2 bytes of memory. Int can be used with all modifiers. When signed and unsigned modifiers are used size remains same for int but for long int a variable requires 4 bytes.
int in=33000; printf("%d",in); O/P -32536
Description: when value assigned to a variable of integer type is outside the range of possible values the oscillation effect seen in char data type is also seen here i.e. when a value exceed one side of range it start from another side of range.