8. C Datatypes

Table of Contents
< All Topics
Print

8. C Datatypes

In the C programming language, data types refer to a domain of allowed values & the operations that can be performed on those values. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted. There are 4 fundamental (primitive) C datatypes, which are- char, int, float &, double. Char is used to store any single character; int is used to store any integer value, float is used to store any single precision floating point number & double is used to store any double precision floating point number.
You can use two qualifiers to expand the range of these basic types.

  • a. Sign qualifier– signed & unsigned
  • b. Size qualifier- short & long

Why to Use of Data Types?

  • To identify the type of a variable when it declared.
  • To identify the type of the return value of a function.
  • To identify the type of a parameter expected by a function.

ANSI C provides three types of data types:

  • Primary(Built-in) Data Types:
    void, int, char, double and float.
  • Derived Data Types:
    Array, References, and Pointers.
  • User Defined Data Types:
    Structure, Union, and Enumeration.

Classification Chart

Image 5
All three types of data types

Primitive Datatypes

TypeStorage sizeFormatValue range
char1 byte%c-128 to 127
unsigned char1 byte%c0 to 255
int4 bytes%d-2,147,483,648 to 2,147,483,647
unsigned int4 bytes%u0 to 4,294,967,295
short2 bytes%d-32,768 to 32,767
unsigned short2 bytes%u0 to 65,535
long4 bytes%ld-2,147,483,648 to 2,147,483,647
unsigned long4 bytes%lu0 to 4,294,967,295
TypeStorage sizeFormatValue rangePrecision
float4 byte%f1.2E-38 to 3.4E+386 decimal places
double8 byte%lf2.3E-308 to 1.7E+30815 decimal places
long double10 byte%LF3.4E-4932 to 1.1E+493219 decimal places
void datatype As the name suggests it holds no value and is generally used for specifying the type of function or what it returns. If the function has a void type, it means that the function will not return any value.
A void is used in three kinds of situations −
TypeDescription
Function returns as void A function with no return value has the return type as void. For example, void exit (int status);
Function arguments as voidA function with no parameter can accept a void. For example, int rand(void);
Pointers to voidA 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 );
This returns a pointer to void which can be casted to any data type.

Three more data types have been added in C99:

  • _Bool
  • _Complex
  • _Imaginary

Declaration of Primary Data Types with Variable Names

After taking suitable variable names, they need to be assigned with a data type. This is how the data types are used along with variables:

Examples of Primitive datatypes

Example 1:

int    age;
char   letter;
float  height, width;
char answer, choice;

Example 2:

#include 
int main()
{
    int a = 4000; // positive integer data type
    float b = 5.2324; // float data type
    char c = 'Z'; // char data type
    long d = 41657; // long positive integer data type
    long e = -21556; // long -ve integer data type
    int f = -185; // -ve integer data type
    short g = 130; // short +ve integer data type
    short h = -130; // short -ve integer data type
    double i = 4.1234567890; // double float data type
    float j = -3.55; // float data type
}

Difference between Float and Double Data Types

float Datatypedouble Datatype
Float takes 4 bytes for storage.Double takes 8 bytes for storage.
A value having a range within 1.2E-38 to 3.4E+38 can be assigned to float variables.A value having range within 2.3E-308 to 1.7E+308 can be assigned to double type variables
Has a precision of 6 decimal places.Has a precision of 15 decimal places.
It has single precision.It has the double precision or you can say two times more precision than float.
This is generally used for graphic based libraries for making the processing power of your programs faster, as it is simpler to manage by compilers.This is the most commonly used data type in programming languages for assigning values having a real or decimal based number within, such as 3.14 for pi.

Derived Data Types

C language supports three derived datatypes

Data Types Description
ArraysArrays are sequences of data items having homogeneous values. They have adjacent memory locations to store values.
PointersThese are used to access the memory and deal with their addresses.
ReferencesFunction pointers allow referencing functions with a particular signature.
All these derived datatypes will be discussed later in the tutorial.

User Defined Data Types

C datatypes allows the feature called type definition which allows programmers to define their identifier that would represent an existing data type.

Data Types Description
StructureIt is a package of variables of different types under a single name. “struct” keyword is used to define a structure.
UnionThese allow storing various data types in the same memory location. Programmers can define a union with different members, but only a single member can contain a value at given time.
EnumEnumeration is a special data type that consists of integral constants, and each of them is assigned with a specific name. “enum” keyword is used to define the enumerated data type.
All these user defined datatypes will also be discussed later in the tutorial.

Write a program to check a datatype size.

The storage representation and machine instructions vary from one machine to another. The sizeof operator can be used to obtain the precise size of a type or variable on a specific platform. On some platforms, the size of a type or variable may be different due to factors such as the underlying architecture, compiler, and operating system. It is important to consider these differences when writing code that needs to be portable across different machines.

// Program to check the datatype or its variable size 
// use keyword sizeof to find a size.
#include 
#include 
int main() {
   printf("Storage size for void : %d \n", sizeof(void));
   printf("Storage size for char : %d \n", sizeof(char));
   printf("Storage size for float : %d \n", sizeof(float));
   printf("Storage size for double : %d \n", sizeof(double));
   printf("Storage size for int : %d \n", sizeof(int));
   printf("Storage size for long int : %d \n", sizeof(long int));
   printf("Storage size for unsigned int : %d \n", sizeof(unsigned int));
   printf("Storage size for unsigned long int : %d \n", sizeof(unsigned long int));
   printf("Storage size for long double : %d \n", sizeof(long double));
   printf("Minimum long double  negative value: %E\n", LDBL_MIN );
   printf("Maximum long double  positive value: %E\n", LDBL_MAX );
   printf("Precision value: %d\n", LDBL_DIG );
    
   return 0;
}

Output

Image 8

Leave a comment