Programming

C – Data Types in C Programming

A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable.

Each and every variable that we are using in our program must be declared before its use.

The reason behind it is that, each and every variable must be placed in some memory location.

For example, if we are declaring variables as int a, b; then memory location is reserved at compile time for two variables a and b as integers. There are two types of data;

  1. Fundamental or basic data types
  2. Derived data types

some examples of derived data types are : array, structure, union etc.

Fundamental Data Types

The fundamental data types are the built in data types in C. They are char, int, float and double.

These are also called basic data types. C also provides more data types which are extended from these basic data types by adding different qualifiers.

The basic data types can be augmented by the use of the data type qualifiers short, long, signed and unsigned to create extended data type.

We can also create derived data types, such as Array, Structure etc. All the derived data types in C are based upon one of the fundamental (basic) data types.

The basic data types, their memory requirements and the range are given in the following table.  

Data TypeMemory RequirementRange
Char1 byte0 to 255
Int2 byte in 16 bit system 4 byte in 32 bit system-32768 to 32767 -2147483648 to 2147483647
Float4 bytes-3.4e38 to -3.4e-38 up to 0 and 3.4e-38 to 3.4e38
Double8 bytes-1.7e308 to -1.7e-308 up to 0 and 1.7e-308 to 1.7e308

Character Data Type

This type of data can hold single character and are placed between single quotes ( ‘   ‘ ).

It occupies 1 byte space in memory and can be used as char, signed char and unsigned char.

In C, char and int are compatible to each other, so a variable of char type can be used to hold integer value of 1 byte, that is char are also integers but they can hold only 1 byte.

The format specifier for the character data type is %c.

char one='a';
char two='b';
printf("%c %c",first, second);

actually characters are stored as numbers in memory. There is a standard rule which is used to represent characters as numbers.

That is, each character is given some specific numeric code. C uses ASCII ( American Standard Code for Information Interchange) coding scheme to specify for each character.

Some of the characters and their ASCII codes are given below;

A = 65 to Z=90;
a=97 to z=122;
0=48 to 9=57;
space = 32, & =38 etc.

the memory requirement and the range of character data type is given in following table.

Data TypeMemory RequirementRange
Char1 byte0 to 255 or -128 to 127
Unsigned char1 byte0 to 255
Signed char1 byte-128 to 127

Integral Data Type

This type of data can hold numeric values without fractional parts. The size of int data type depends on Operating System.

If the OS of 16 bit like MS-DOS or any other, then it occupies 2 bytes space memory. If the OS is of 32 bit like Windows 9598XP, UNIX etc, then int occupies 4 bytes in memory and it may be used as int or (singed int), unsigned (unsigned int) etc.  

The format specifiers for int is %d, in signed is %u, short is %hd, unsigned short is %hu, long is %ld and unsigned long is %lu.

Data TypeMemory UsedRange
Int2 byte for 16 bit system 4 byte for 32 bit system-32768 to 32767 -2147483648 to 2147483647
Unsigned2 byte for 16 bit system 4 byte for 32 bit system0 to 65535 0 to 4294967297
Short2 byte-32768 to 32767
Unsigned short2 byte0 to 65535
Long4 byte-2147483648 to 2147483647
Unsigned long4 byte0 to 4294967297

Floating Point Data Type

This type of data can hold an integral part and fractional part as its value. The first of such data type, float, occupies 4 bytes in memory and the range may be between +-3.4c3+-38.

Its format specifier is %f, %e and %g. With %f and %e format specifier 6 digits are displayed after a fractional point by default.

However, with %g format specifier as a whole 6 digits including integer part are displayed by default.

Data TypeMemory UsedRange
Float4 bytes-3.4e38 to -3.4e-38 up to 0 and 3.4e-38 to 3.4e38
Double8 bytes-1.7e308 to -1.7e-308 up to 0 and 1.7e-308 to 1.7e308
Long double10 bytes-1.7e4932 to -1.7e-3932 up to 0 and -1.7e-4932 to 1.7e4932

Tuts

About Author

Tutsmaster.org provides tutorials related to tech and programmings. We are also setting up a community for the users and students.