Sunday, November 26, 2017

Data Types in C

In this section we will learn about data types. We all know that, before using any variable in C it should be declared. Data types used to define a variable and how much memory space need to allocate that variable.

In our variable section we compared variables with containers or boxes. Every box or container has its limit. Which container we need to store anything it depends on the size of what we want to store. Like that, we also need to take a decision to how much memory we need to allocate. We can’t always measure the exact space we need but we can easily calculate/guess approximate space we need.

Data Types in C language

Fundamental data type

  • Integer Type
  • Floating Type
  • Character Type

Derived Data Type

  • Array Type
  • Structure Type
  • Pointer Type

Fundamental Data Type

Integer Type

The following chart will show the details about integer type data
Type Size (bytes) Range Format Specifier
short 2 -32,768 to 32767 %hd
unsigned short 2 0 to 65,535 %hu
unsigned int 4 0 to 4,294,967,295 %u
int 4 -2,147,483,648 to 2,147,483,647 %d
long int 4 -2,147,483,648 to 2,147,483,647 %ld
unsigned long 4 0 to 294,967,295 %lu
long long 8 -(2^63) to (2^63)-1 %lld
unsigned long long 8 0 to 18,446,744,073,709,551,615 %llu

Note:
  • First column indicates the reserve words or keywords.
  • We can’t store decimal values into integer data. If we assign 7.36 into any integer variable then it will store 7 (only the integer part)


Floating Type

The following chart will show the details about floating type data
Type Size (bytes) Range Precision Format Specifier
float 4 1.2E-38 to 3.4E+38 6 decimal places %f
double 8 2.3E-308 to 1.7E+308 15 decimal places %lf
long double 12 3.4E-4932 to 1.1E+4932 19 decimal places %Lf

Note:
  • First column indicates the reserve words or keywords.
  • Float data type allows to store decimal values.
  • Floating point variable has precision of 6 digits and double has 15 digits precision.


Character Type

The following chart will show the details about character type data
Type Size (bytes) Range Format Specifier
signed char 1 -128 to 127 %c
unsigned char 1 0 to 255 %c
char 1 -128 to 127 or 0 to 255 %c

Note:
  • First column indicates the reserve words or keywords.
  • A character type data only allow to store character.

We will discuss about Derived Data Type later as it requires some advance knowledge.

Previous Page

Next Page



No comments:

Post a Comment