Data Types in C

Last Updated : 4 Sep, 2026

Data types form the foundation of data representation in C. Every variable in C must be associated with a data type that defines the kind of value it can store and how the compiler handles it.

  • Data types define memory allocation, value range, and valid operations on a variable.
  • Understanding data types is essential for writing correct and efficient C programs.

Classification of Data Types in C

Data types in C are broadly classified into three categories:

data_type_in_c
Data Types in C

Primitive Data Types

Primitive data types are the basic built-in data types in C used to store simple values like integers, characters, and floating-point numbers.

Integer Data Type

  • Stores whole numbers (positive, negative, or zero).
  • We use int keyword to declare the integer variable:
  • Size: typically 4 bytes, Range: -2,147,483,648 to 2,147,483,647.
  • Format specifier: %d.

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

C++
#include <stdio.h>

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

Output
var = 22

Character Data Type

  • Stores a single character (like ‘A’, ‘b’, or ‘5’).
  • Size: typically 1 byte, Range: -128 to 127 (signed by default).
  • Format specifier: %c.
C++
#include <stdio.h>

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

Output
ch = A

Float Data Type

  • Stores decimal numbers (numbers with fractional part).
  • Size: typically 4 bytes, Approximate range: 3.4e-38 to 3.4e+38.
  • Format specifier: %f.
C++
#include <stdio.h>

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

Output
val = 12.450000

Double Data Type

  • Stores decimal numbers with more precision than float.
  • Size: typically 8 bytes, Approximate range: 1.7e-308 to 1.7e+308.
  • Format specifier: %lf.
C++
#include <stdio.h>

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

Output
val = 1.452100

Void Data Type

  • Represents no value or empty type.
  • Used in functions that do not return any value.
  • Can also be used for generic pointers (void *) in memory operations.
C++
#include <stdio.h>

// Function with void return type
void greet()
{
    printf("Hello, welcome!\n");
}

int main()
{
    greet();
    return 0;
}

Output
Hello, welcome!

Derived Data Types in C

Derived data types are created from primitive data types.

Arrays

An array stores multiple values of the same data type in contiguous memory locations. Each element is accessed using an index, starting from 0.

C++
#include <stdio.h>

int main() {
    int arr[5] = {10, 20, 30, 40, 50};

    printf("First element: %d\n", arr[0]);
    printf("Third element: %d\n", arr[2]);

    return 0;
}

Output
First element: 10
Third element: 30

Explanation:

  • int arr[5] declares an array that can store 5 integers.
  • {10, 20, 30, 40, 50} initializes the array elements.
  • arr[0] accesses the first element, while arr[2] accesses the third element.

Pointers

A pointer is a derived data type that stores the memory address of another variable. The & operator is used to get an address, while the * operator is used to declare and dereference a pointer.

C++
#include <stdio.h>

int main() {
    int x = 10;
    int *ptr = &x;

    printf("Value of x: %d\n", x);
    printf("Address of x: %p\n", (void *)&x);
    printf("Value using pointer: %d\n", *ptr);

    return 0;
}

Output
Value of x: 10
Address of x: 0x7fffffffe9c4
Value using pointer: 10

Explanation: int x = 10; declares an integer variable, while int *ptr = &x; declares a pointer that stores the address of x. The & operator gets the address of x, and the * operator accesses the value stored at that address.

Functions

Functions are derived types that define a block of code to perform a specific task and can have a return type and parameters.

C++
#include <iostream>
using namespace std;

int add(int a, int b) {
    return a + b;
}

int main() {
    cout << add(10, 20);
    return 0;
}

Output
30

Explanation: int add(int a, int b) defines a function that takes two integers and returns their sum and add(10, 20) calls the function, which returns 30.\

User Defined Data Types in C

User-defined data types allow programmers to create custom data structures.

Structure (struct)

A structure groups related variables of different data types under a single name.

C++
#include <iostream>
using namespace std;

struct Student {
    string name;
    int age;
};

int main() {
    Student s1 = {"Riya", 20};

    cout << "Name: " << s1.name << endl;
    cout << "Age: " << s1.age << endl;

    return 0;
}

Output
Name: Riya
Age: 20

Explanation: struct Student defines a structure containing name and age as its data members. Student s1 creates a structure variable, and s1.name and s1.age access its members.

Union

A union allows multiple members to share the same memory location, but only one member can hold a meaningful value at a time.

C++
#include <iostream>
using namespace std;

union Data {
    int i;
    float f;
};

int main() {
    Data data;

    data.i = 10;
    cout << "Integer: " << data.i << endl;

    data.f = 5.5f;
    cout << "Float: " << data.f << endl;

    return 0;
}

Output
Integer: 10
Float: 5.5

Explanation: union Data defines multiple members that share the same memory location. When data.f is assigned a value, it overwrites the value previously stored in data.i.

Enumeration (enum)

An enumeration (enum) is a user-defined data type that defines a set of named integral constants.

C++
#include <iostream>
using namespace std;

enum Day {
    MON, TUE, WED, THU, FRI, SAT, SUN
};

int main() {
    Day today = WED;

    cout << "Day: " << today << endl;

    return 0;
}

Explanation: enum Day defines named constants such as MON, TUE, and WED, which are assigned integer values starting from 0. Day today = WED creates an enum variable and assigns it the value WED.

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.

C
#include <stdio.h>

int main()
{

    // Use sizeof() to know size of 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

Explanation: sizeof() returns the amount of memory occupied by a data type or variable, measured in bytes. The exact size of some data types can vary across systems, while types such as char are guaranteed to occupy 1 byte.

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.

Related Article

Comment