February 24, 2019

Data Types in C

For declaring any variable, one has to mention the type of data that variable or constant is. Different types of values in numerical and character formats can be input by users. In that case it is necessary for programmers to have a sound knowledge about all data types available in C Programming.

Data Types in C

Overview:


What is a Data Type?

Data Types simply refers to a set of values together with a set of permitted operations. Variables in C programs are declared with appropriate data types before the variable is assigned to any value.

Types of Data Types

Different data types are discussed below:
C data Types
Data Types in C chart

char(%c): A char data type is used to hold individual character or letter or symbol. It usually occupies 1 byte in RAM. The value range in decimal in char data types is between -128 to 127.
In C programming, char is used for declaring character type variable.
For example:

char a='t';

Here a is a character data type whose value is t.



int(%d): An int data type is used to store a signed or unsigned whole integer number within a specified range and cannot have fractional values or a decimal point. It usually occupies 2 bytes in RAM. The value range in decimal in int data types is between -32768 to 32767.
In C programming, keyword int is used for declaring integer variable.
For example:

int sides;

Here, sides is a variable of type integer.
We can declare multiple variables at once in C programming.
For example:

int length, breadth;

float(%f): A float data type is used to store floating point numbers or real numbers that is numbers that can have decimal values. It usually occupies 4 bytes in RAM. The value range in decimal in float data type is between 3.4e-38 to 3.4e+38. We can declare a floating point variable by using float as well as double data type. 
For example:

float volume; double tax; 

Here both volume and tax are floating type variables.

Difference between float and double

A double data type is also used to store floating point numbers or real numbers, but can give a better precision. It usually occupies 8 bytes in RAM. The value range is between 1.7e-308 to 1.7e+308.

C language provides some additional data types to declare a variable in an appropriate manner. They are as follows:

unsigned char(%c): It usually occupies 1 byte in RAM. The value range in decimal form in unsigned char is from 0 to 255.

unsigned int(%u): It usually occupies 2 bytes in RAM. The value range in decimal in unsigned int data types is between 0 to 65535.



short int(%hd): It usually occupies 1 byte in RAM. The value range in decimal in short int data type is between -128 to +127.

unsigned short int(%hu): It usually occupies 1 byte in RAM. The value in decimal in unsigned short int data type is between 0 to 255.

long int(%ld): It usually occupies 4 bytes in RAM. The value range in decimal in long int data type is between -2147483648 to +2147483647.

unsigned long int(%lu): It usually occupies 4 bytes in RAM. The value range in decimal in unsigned long int data type is between 0 to 4294967295.

long double(%lf):It usually occupies 10 bytes in RAM. The value range in decimal in long double data type is between 3.4e-4932 to 1.1e+4932.

Tabular Stats of Data Types

To summarise the above discussed data type, we take a lot at the following tables:

Integer Types

Data TypeStorage SizeValue range
char1 byte-128 to 127 or
0 to 255
unsigned char1 byte0 to 255
signed char1 byte-128 to 127
int2 or 4 bytes-32,768 to 32,767 or
-2,147,483,648 to 2,147,483,647
unsigned int2 or 4 bytes0 to 65,535 or
0 to 4,294,967,295
short2 bytes-32,768 to 32,767
unsigned short2 bytes0 to 65,535
long4 bytes-2,147,483,648 to
2,147,483,647
unsigned long4 bytes0 to 4,294,967,295

Floating Point Types

TypeStorage sizeValue rangePrecision
(Decimal Places)
float4 bytes1.2E-38 to
3.4E+38
6
double8 bytes2.3E-308 to
1.7E+308
15
long double10 bytes3.4E-4932 to
1.1E+4932
19

Lastly, we have one data type that I decided to alienate from the rest.

The Void Data Type

This kind of Data Type indicates that no value is available. Based upon the necessity of the situation they are used as under:

  1. Function returns as void: There are functions in C which do not return any value or you can say they return void. For example, void exit(int status);
  2. Function arguments as void: There are functions in C which doesn't accept any parameter. A function with no parameter can accept a void. For example, int rand(void);
  3. Pointers to void: A pointer of type void * represents the address of an object, but not its type. For example, a memory allocation function void *malloc(size_t size); returns a pointer to void which can be casted to any data type.


Books I Prefer:



  

I hope this article was helpful to understand different Data Types in C! Comment below, if you've got any question. Head back soon for another interesting article on C Programming.