Data types in C Programming

p>For communication with computer, we need to pass different data to the computer. Data passing to the computer can be of different types like integer, float or fractional number or string. 

Memory is required to store data. The size of the memory is different for each data type. Way of representing different types of data during programming is known as data types. It will determine the types of data which a variable will hold. Suppose if a variable ‘p’ is declared as integer data type (int) it means that ‘p’ can hold only an integer. Integer value data type must be declared to any variable which is used in program.

There data types in C Programming can be categorized as follows.

1. Primary Data Type

Primary data types are the fundamental data types in C programming that store numerical values and can further be classifies into integer (int), floating point (float), double and character (char) data types.

i. Integer data type (int)

Integers mean all whole number which doesn’t include any fractional value. When an identifier is declared as integer, it becomes an integer variable and can hold only integer value. C programming has three classes of integer storage namely short int, int and long int in both signed and unsigned form.

Type Storage size Value range
int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 8 bytes or (4bytes for 32 bit OS) -9223372036854775808 to 9223372036854775807
unsigned long 8 bytes 0 to 18446744073709551615

ii. Float data type (float)

Floating point number is stored in 32 bit with 6 decimal places. If an identifier is declared as float, it becomes a floating point variable and can hold floating point only where the value can be anywhere between 1.2E-38 to 3.4E+38.

iii. Double data type (double)

Double data type is treated as a distinct data type as it occupies twice the memory than float data type. It stores floating point number with much larger range i.e. 2.3E-308 to 1.7E+308 and precision i.e. 15 decimal places. 

Another form of double data type is long double  which occupies 80 bits memory and can range from 3.4E-4932 to 1.1E+4932 with 19 decimal places. 

iv. Char data type (char)

Char data type is also known as single character. It is defined as character type data. It is usually stored in 8 bits of internal storage. The value range for char data type can vary with its form.

Type Storage size Value range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127

v. Void data type (void)

Void data type is used for empty set of value and functions that do not return any value.

2. Derived data types

Derived data types are simply twisted form of primary data types where arrays, references and pointers are used.

Arrays - Arrays are sequences of data items having homogeneous values. They have adjacent memory locations to store values.

Pointers - Pointers are used to access the memory and deal with their addresses.

References -  Function pointers allow referencing functions with a particular signature.

3. User defined data types

C Programming also allows programmers to define the identifier that would represent an existing data type.

Structure - It is a collection of variables of different types under a single name used to handle data efficiently. 

Union - Union allow storing various data types in a single memory location. A union can be defined with different members but only a single member can contain a value at a given time.

Enumeration -  Enumeration is a special data type which can be used to define variables that can only assign certain discrete integer values throughout the program.

Using Data Types in C Programming

#include <stdio.h>
int main()
  {
    int w = 2, e = -1;
    float b = 5.23247, t = -2.51;
    long r = 12345, i = -10023; 
    short c = 123, k = -111;
    double s = 1.0123456789;

    printf("Sum = %f\n", (w+e+b+t+r+i+c+k+s));
}

typedef

C programming provides you a keyword called typedef, which can be used to give a data type a new name.

typedef int NUM;

The identifier can be used as an abbreviation for the data type in the program.

NUM  n1, n2;

Type Casting

Type casting is the process of converting one data type into another. For example, if you want to store a large value in a variable holding integer, you'll need to change the data type from int to long.

Syntax :

(newDataType) variable

Example :

#include <stdio.h>

main() {

   int length = 1734554, breadth = 543115434;
   double area;

   area = (double)length * breadth;
   printf("Area of the  field is : %f\n", area );
}

0 Like 0 Dislike 0 Comment Share

Leave a comment