Data Types in C

A data type specifies the type of data that a variable can store, such as integers, floating-point numbers, characters, etc.

C Data Types

C Data Types

The following are the data types in C language:

Basic Data Types

Basic data types in C are integer-based and floating-point-based. C language supports both signed and unsigned literals.

The memory size of these types may vary depending on the system architecture (32-bit or 64-bit).

Memory Sizes and Ranges

Data Type Memory Size Range
char 1 byte −128 to 127
unsigned char 1 byte 0 to 255
short 2 bytes −32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
int 4 bytes −2,147,483,648 to 2,147,483,647
unsigned int 4 bytes 0 to 4,294,967,295
long 4 bytes (32-bit), 8 bytes (64-bit) −2,147,483,648 to 2,147,483,647 (32-bit)
−9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (64-bit)
unsigned long 4 bytes (32-bit), 8 bytes (64-bit) 0 to 4,294,967,295 (32-bit)
0 to 18,446,744,073,709,551,615 (64-bit)
long long 8 bytes −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 4 bytes ~3.4 × 10−38 to ~3.4 × 1038
double 8 bytes ~2.2 × 10−308 to ~1.7 × 10308
long double 16 bytes ~3.4 × 10−4932 to ~1.1 × 104932

Example Code: Working with Integer Arrays

                        
#include <stdio.h>
int main() 
{
    int numbers[5] = {10, 20, 30, 40, 50};
    for (int i = 0; i < 5; i++) {
        printf("%d ", numbers[i]);
    }
    return 0;           
}
                        
                    

Output

10 20 30 40 50

Example Code: Working with Floating-Point Numbers

                        
#include <stdio.h>
int main() 
{
 float number = 12.34;
 printf("The floating-point number is: %.2f\n", number);
 return 0;
}
                        
                    

Output

The floating-point number is: 12.34

Example Code: Using char Data Type

                        
#include <stdio.h>
int main() 
{
 char letter = 'A';
 printf("The character is: %c\n", letter);
 return 0;
}
                        
                    

Output

The character is: A

Array

An array is a collection of elements of the same type stored in contiguous memory locations. Arrays allow you to store multiple values of the same data type under a single name.

                
#include <stdio.h>
int main() 
{
    int numbers[5] = {10, 20, 30, 40, 50};
    for (int i = 0; i < 5; i++) {
        printf("%d ", numbers[i]);
    }
    return 0;
}
                
            

Output

10 20 30 40 50

Pointer

A pointer is a variable that stores the memory address of another variable. Pointers are used to manipulate data indirectly through their addresses.

                
#include <stdio.h>
int main() 
{
    int num = 10;
    int *ptr = #
    printf("Value of num: %d\n", num);
    printf("Address of num: %p\n", ptr);
    return 0;
}
                
            

Output

Value of num: 10
Address of num: 0x7ffeeeb95a3c (example address)

Structure

A structure is a user-defined data type in C that allows the grouping of variables of different types into a single unit. Structures are used to represent real-world entities.

                
#include <stdio.h>

struct Person {
    char name[50];
    int age;
};

int main() 
{
    struct Person p1 = {"John Doe", 30};
    printf("Name: %s, Age: %d\n", p1.name, p1.age);
    return 0;
}
                
            

Output

Name: John Doe, Age: 30

Union

A union is similar to a structure in that it allows different data types to be stored in the same memory location. However, unlike structures, a union can store only one of its members at a time.

                
#include <stdio.h>
union Data 
{
    int i;
    float f;
    char c;
};

int main() 
{
    union Data data;
    data.i = 10;
    printf("Integer: %d\n", data.i);
    data.f = 220.5;
    printf("Float: %.2f\n", data.f);
    data.c = 'A';
    printf("Character: %c\n", data.c);
    return 0;
}
                
            

Output

Integer: 10
Float: 220.50
Character: A

Enumeration Data Type

The enum data type in C is used to define a set of named integer constants. This is helpful when you need to work with a limited set of values.

            
#include <stdio.h>

enum Week {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};

int main() 
{
 enum Week today;
 today = Wednesday;
 printf("Day: %d\n", today);
 return 0;
}
            
        

Output

Day: 3

Void Data Type

The void data type is used to specify that a function does not return any value or to declare pointers that point to an unknown data type.

            
#include <stdio.h>
void printMessage() 
{
  printf("Hello, World!\n");
}

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

Output

Hello, World!