Open In App

Data Types in C

Last Updated : 13 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Each variable in C has an associated data type. It specifies the type of data that the variable can store like integer, character, floating, double, etc.

Example:

C++
int number;

The above statement declares a variable with name number that can store integer values.

C is a statically type language where each variable's type must be specified at the declaration and once specified, it cannot be changed. This is because each data type requires different amounts of memory and allows type specific operations.

The data types in C can be classified as follows:

Types

Description

Data Types

Primitive Data TypesPrimitive data types are the most basic data types that are used for representing simple values.

int, char, float, double, void

Derived TypesThe data types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types.

array, pointers, function

User Defined Data Types

The user-defined data types are defined by the user himself.

structure, union, enum

In this article, we will discuss the basic (primary) data types in C.

Integer Data Type

The integer datatype in C is used to store the integer numbers (any number including positive, negative and zero without decimal part). Octal values, hexadecimal values, and decimal values can also be stored in int data type in C.

  • Range:  -2,147,483,648 to 2,147,483,647
  • Size: 4 bytes
  • Format Specifier: %d

Format specifiers are the symbols that are used for printing and scanning values of given data types.

Example:

We use int keyword to declare the integer variable:

C++
int val;

We can store the integer values (literals) in this variable.

C++
#include <stdio.h>

int main() {
    int var = 22;
    
    printf("var = %d", var);
    return 0;
}

A variable of given data type can only contains the values of the same type. So, var can only store numbers, not text or anything else.

The integer data type can also be used as:

  1. unsigned int: It can store the data values from zero to positive numbers, but it can’t store negative values
  2. short int: It is lesser in size than the int by 2 bytes so can only store values from -32,768 to 32,767.
  3. long int: Larger version of the int datatype so can store values greater than int.
  4. unsigned short int: Similar in relationship with short int as unsigned int with int.

Note: The size of an integer data type is compiler dependent. We can use sizeof operator to check the actual size of any data type. In this article, we are discussing the sizes according to 64-bit compilers.

Character Data Type

Character data type allows its variable to store only a single character. The size of the character is 1 byte. It is the most basic data type in C. It stores a single character and requires a single byte of memory in almost all compilers.

  • Range: (-128 to 127) or (0 to 255)
  • Size: 1 byte
  • Format Specifier: %c

Example:

C++
#include <stdio.h>

int main() {
    char ch = 'A';
    
    printf("ch = %c", ch);
    return 0;
}

Float Data Type

In C programming, float data type is used to store single precision floating-point values. These values are decimal and exponential numbers.

  • Range: 1.2E-38 to 3.4E+38
  • Size: 4 bytes
  • Format Specifier: %f

Example:

C++
#include <stdio.h>

int main() {
    float val = 12.45;
    
    printf("val = %f", val);
    return 0;
}

Double Data Type

The double data type in C is used to store decimal numbers (numbers with floating point values) with double precision. It can easily accommodate about 16 to 17 digits after or before a decimal point.

  • Range: 1.7E-308 to 1.7E+308
  • Size: 8 bytes
  • Format Specifier: %lf

Example:

C++
#include <stdio.h>

int main() {
    double val = 1.4521;
    
    printf("val = %d", val);
    return 0;
}

Void Data Type

The void data type in C is used to indicate the absence of a value. Variables of void data type are not allowed. It can only be used for pointers and function return type and parameters.

Example:

C++
void fun(int a, int b){
    // function body
}

where function fun is a void type of function means it doesn't return any value.

Size of Data Types in C

The size of the data types in C is dependent on the size of the architecture, so we cannot define the universal size of the data types. For that, the C language provides the sizeof() operator to check the size of the data types.

Example

C
#include <stdio.h>

int main(){
    
    // Use sizeof() to know size
    // the data types
    printf("The size of int: %d\n",
        sizeof(int));
    printf("The size of char: %d\n",
        sizeof(char));
    printf("The size of float: %d\n",
        sizeof(float));
    printf("The size of double: %d",
        sizeof(double));

    return 0;
}

Output
The size of int: 4
The size of char: 1
The size of float: 4
The size of double: 8

Different data types also have different ranges up to which can vary from compiler to compiler. Below is a list of ranges along with the memory requirement and format specifiers on the 64-bit GCC compiler.

Data TypeSize (bytes) RangeFormat Specifier 
 
short int-32,768 to 32,767 %hd 
unsigned short int0 to 65,535 %hu 
unsigned int0 to 4,294,967,295 %u 
int-2,147,483,648 to 2,147,483,647 %d 
long int-2,147,483,648 to 2,147,483,647 %ld 
unsigned long int0 to 4,294,967,295 %lu 
long long int-(2^63) to (2^63)-1 %lld 
unsigned long long int0 to 18,446,744,073,709,551,615 %llu 
signed char-128 to 127 %c 
unsigned char0 to 255 %c 
float1.2E-38 to 3.4E+38%f 
double1.7E-308 to 1.7E+308%lf 
long double16 3.4E-4932 to 1.1E+4932%Lf 

Note: The long, short, signed and unsigned are datatype modifier that can be used with some primitive data types to change the size or length of the datatype.

Literals in C

In C, literals are constant values assigned to variables. They represent fixed values that cannot be changed. Literals occupy memory but do not have references like variables. Often, the terms constants and literals are used interchangeably.

Type Conversion

In C, type conversion is the process of changing one data type into another. This can happen automatically by the compiler or manually by the programmer. Type conversion is only performed between data types where such a conversion is possible.


Next Article

Similar Reads