Data Types In C Progrtamming

Prashant | Sun, 14 Jun, 2020 | 132

Data Types in C

Each variable in C has an associated data type. Each data type requires different amounts of memory and has some specific operations which can be performed over it. Let us briefly describe them one by one:

Following are the examples of some very common data types used in C:

  • char: The most basic data type in C. It stores a single character and requires a single byte of memory in almost all compilers.
  • int: As the name suggests, an int variable is used to store an integer.
  • float: It is used to store decimal numbers (numbers with floating point value) with single precision.
  • double: It is used to store decimal numbers (numbers with floating point value) with double precision.

Different data types also have different ranges upto which they can store numbers. These ranges may vary from compiler to compiler. Below is list of ranges along with the memory requirement and format specifiers on 32 bit gcc compiler.

Data TypesMemory (Bytes)RangeFormat Specifier
short int2-32,768 to 32,767%hd
unisgned short int20 to 65,535%hu
unsigned int40 to 4,294,967,295%u
int4-2,147,483,648 to 2,147,483,647%d
long int8-2,147,483,648 to 2,147,483,647%ld
unsigned long int80 to 4,294,967,295%lu
long long int8-(2^63) to (2^63)-1%lld
unsigned long int80 to 18,446,744,073,709,551,615%llu
signed char1-128 to 127%c
unsigned char10 to 255%c
float4 %f
double8 %lf
long double16 %lf

1) char

char represents character, it can be used to declare a character type variable, constant in C language. It takes only one byte (8 bits) in the computer memory and it stores a single character. The value range of char data type is -128 to +127.

'char' variable declaration

char gender; 	/* to store gender*/

'char' constant declaration

const char IS_MARRIED= 'N'; /*to store, marital status as "No"*/

2) int

int represents integer, it can be used to declare an integer type variable, constant in C language. It takes either 2 bytes (16 bits) or 4 bytes (32 bits) according to compiler architecture in the computer memory. It stores only integer numbers (numbers without precision). The value range of 2 bytes integer is -32,768 to +32,767 and 4 bytes integer is -21474836478 to +21474836477.

'int' variable declaration

int number; 	/*to store any integer number*/

'int' constant declaration

const int MAX_LENGTH=100; 	/*to store maximum length as 100*/

3) float

float represents single precision float type value, it can be used to declare a float type variable, constant in C language. It takes 4 bytes (32 bits) in the computer memory. The value range of float data type is 1.2E-38 to 3.4E+38 with 6 decimal places.

'float' variable declaration

float weight;	/*to store weight in kilogram and gram*/

'float' constant declaration

const float PI=3.14f;	/*to store value of PI as 3.14*/

These are the basic/primitive data types in C language, there are other data types too, they are the enhanced version of these data types, by using some qualifiers we can change their storage capacity and value range, some of the most useful qualifiers are:

  1. unsigned
  2. short
  3. long

A) unsigned

Consider the given declaration

char value;

Here, value is the signed char type variable and it can store value between -128 to +127, but if we have only positive values we can define the value as unsigned char.

unsigned defines that variable will store only positive values and the value range of unsigned type variable is: 0 to ((MAX_RANGE*2)+1).

unsigned char - 0 to 255
unsigned int - 0 to 65,535 (for 2 bytes integer)
unsigned int - 0 to 4294967295 (for 4 bytes integer)

B) short

short can also be written as short int, it takes 2 bytes (16 bits) in the computer memory and its value range is -32,768 to +32767 (which is similar to 2 bytes int data type).

short can also be used as unsigned short, in that case the value range will be 0 to 65,535.

C) long

long can be used with int and double data types, here is the sizes that variable will take in the computer memory.

According to 32 bits compiler architecture

long int - 8 bytes
long double - 16 bytes

0 comments
Leave a comment