C - Data Types


Next >>

A computer program need a data and process that data into meaningful information. There are many different kinds of data exist. The data types are used to determine the size of the variables. The following data types are commonly used in C programming.

  • Basic data types---int, char, float, double.
  • Enumeration Data type--- enum
  • Derived data types---array, pointer, structure, union.
  • Void data type--- void

Here, We discuss only basic data types, later we will see the rest of the data types.

Integers Data Type

Integer is a whole number. It does not have the decimal point.

For example:

42 245 -89 -3 125

The " int " keyword is used to declare an interger variable. The following are the different kinds of integer data type.

Type Memory RequirementRange Format specifier
int 2 bytes -32,768 to 32,767%d or %ld
short int2 bytes-32,768 to 32,767%d or %I
long int4 bytes -2,147,483,648 to 2,147,483,647%ld
signed short int2 bytes-32,768 to 32,767%d
signed long int4 bytes-2,147,483,648 to 2,147,483,647%ld
unsigned short2 bytes0 to 65,535%d or %hu
unsigned long 4 bytes0 to 4,294,967,295%lu

Floating Point Data Type

The numbers with decimal points are called floating point numbers. This floating point numbers require twice memory as integer. The float data type allows us to specify up to 6 digits after the decimal point where as double data type allows us to specify up to 10 digits after the decimal point.

For example:

255.12    0.666      -523.1      25.23     0.0    2.586745

The " float " and " double " keywords are used to define the float and double variables repectively. Following are the types of floating point data type.

TypeMemory RequirementRangeFormat Specifier
float4 bytes3.4E-38 to 3.8E+38%f or %E or %e
double8 bytes1.7E308 to 1.7E+308%lf or %E or %e
long double10 bytes3.4E-4932 to 3.4E+4932%Lf

If you want to print the range value of any data type, we can use the parameters such as INT_MIN, INT-MAX, FLT-MIN and so on. These parameters are presented in limits.h and float.h header file.

#Include <stdio.h>
#include <limits.h>
#include <float.h>
     printf("Integer range:\t%d,\t%d \n ", INT_MIN,INT_MAX);

     printf("Float range:\t%E,\t%E \n ", FLT_MIN,FLT_MAX);

     printf("Double range:\t%lf,\t%lf \n ", DBL_MIN,DBL_MAX);

When you compile and execute the above program, you will get the following result.

Integer range: -32768, 32767

Float range: 1.175494E-38, 3.402823E+38

Double range: 2.225074E-308, 1.797693E+308

Characters Data Type

A character data type can store single character. Our computer knows 256 different characters which are found in ASCII table. In C the single character is written enclosed by single quotation. The " char " keyword is used to define the character variable. The following are the types of character data type.

TypeMemory RequirementRangeFormat specifier
char 1 byte-128 to 127 or 0 to 255 %c
signed char1 byte-128 to 127%c
unsigned char 1 byte0 to 255%c

Following are examples of character data.

'a'      'B'   '10'     '%'   '-'

A group of multiple charachers or the sequence of characters are called string which is enclosed by a double quotes. Following are C string

" C is a Programming language "
" easy to learn "

Void Data Type

  • Void data type is an empty data type. That means, it does not have value.
  • This data type is mainly used for function definition and pointer declaration.
  • Later section, we see how void will be used in c program.

The sizeof() function

The sizeof() function is used to determaine the required memory space that are allocated to each data type in C Programming Language.

#Include <stdio.h>
#include <limits.h>
#include <float.h>
     int num;

     unsigned int a;

     float b;

     char m;

     printf("Integer Type:\t %d \n ",sizeof(num));

     printf("Unsigned Integer Type:\t %d \n ",sizeof(a));

     printf("Float Type:\t %f\n ", sizeof(b));

     printf("Character Type:\t %c\n ", sizeof(m));



Integer Type: 2
Unsigned Integer Type: 4
Float Type: 4
Character Type: 1

<< Previous

Next >>