Data Types in C | C Data Types

As the name suggests, a data type refers to the type of data being used. The data type defines an attribute to the variable. It defines the set of legal values that the variable can store. A data type specifies the type of data that a variable can store such as integer, float, character etc.

Each variable in C has an associated data type. This determines the type and size of data associated with variables. Whenever we define a variable or use any data in the C language program, we have to specify the type of data, so that the compiler knows exactly what type of data it must expect from the given program.


Following are the primary (basic) data types used in C:

  • Integer (int)
  • Floating point type (float)
  • Character data type (char)


Integer (int): 

The data type ‘int’ represents whole numbers with a range of values supported by a particular machine. For instance, in a 16 bit word length machine, the integer values lie between -32768 to 32767.

C facilitates some control over the integer data type by providing sub data types namely short int, int, long int.

The sub type short int represents a fairly small integer value and requires half the amount of storage as a normal int uses.

Similarly a long int represents a fairly higher integer value and requires generally twice the number of bits as a normal int uses.

Another option in integer data type is declaring it as unsigned. This unsigned integer uses all bits for the magnitude of the number and is always positive. For instance, in a 16 bit machine the range of unsigned integer is 0 to65, 535. Thus long and unsigned are intended for increasing the range of values.

Keyword “int” is used for declaring an integer variable. To define an integer variable to store the roll number of a student:

int roll;


Floating point (float):

It is used to store floating point number. Floating point numbers are numbers that have a decimal point. This data type in ‘C’ is an attribute for real numbers. The required declaration is,

          float a;

which instructs the compiler that the variable ‘a’ belongs to the data type real. If we want to initialize the variable, then

          float a;

a=14.752 ;

This can also be achieved through a single statement.

float a=14.752;

The keyword float defines the floating point number.

When more accuracy is required, another sub-data type, double can be used. Double data type uses twice the storage as that of the float data type. Float is just a single-precision data type, double is the double-precision data type.


Character data type (char):

It is usually used to store a single character in 1 byte (8 bits) of internal storage. The char keyword defines a character data type. Thus the declaration for this is

          char x;

          x= ‘a’;

The variable x is of type character and is initialized to the character ‘a’. The same effect could be achieved as,

          char x= ‘a’;



Data Type

Description

Format

Specification

Storage
(machine dependent)

int

short int

long int

unsigned int

Integer data type

%d or %i

%hd

%ld

%u

2 Bytes

2 Bytes

4 Bytes

2 Bytes

float

double

 

long double

Floating point representation

For better precision of floating point /scientific notation

An extended precision floating point number

%f or %e

%f or %e

 

%lf or %le

4 Bytes

8 Bytes

 

10 Bytes

Char

Character representation

String representation

%c

%s

1 Byte

No. of characters + 1 Byte




Programming Example:

Program to calculate simple interest:




Output:






Post a Comment

0 Comments