Data Types

As the name suggests, data-type specifies the type of the data present in the variable. Variables must be declared with a data-type.

There are four different types of Data types in C.

TypesData-type
Basicint, char, float, double
Derivedarray, pointer, structure, union
Enumerationenum
Voidvoid

1. Basic data types

Basic data types are generally arithmetic types which are based on integer and float data types. They support both signed and unsigned values. Below are some of the oftenly used data types

Data typeDescriptionRangeMemory SizeFormat specifier
intused to store whole numbers-2,147,483,648 to 2,147,483,6474 bytes%d
short intused to store whole numbers-32,768 to 32,7672 bytes%hd
long intused to store whole numbers-2,147,483,648 to 2,147,483,6474 bytes%li
floatused to store fractional numbers6 to 7 decimal digits4 bytes%f
doubleused to store fractional numbers15 decimal digits8 bytes%lf
charused to store a single character-128 to 127 (signed) or 0 to 255 (unsigned)1 byte%c
unsigned charused to store a single character0 to 2551 byte%c
unsigned intused to store positive whole numbers0 to 4,294,967,2954 bytes%u
unsigned longused to store positive whole numbers0 to 4,294,967,295 (minimum)4 or 8 bytes%lu
long long intused to store very large whole numbers-9,223,372,036,854,775,808 to 9,223,372,036,854,775,8078 bytes%lld
unsigned long longused to store very large positive numbers0 to 18,446,744,073,709,551,6158 bytes%llu
_Bool or boolused to store boolean values0 or 11 byte%d

Additional Data Types

size_t and ptrdiff_t

  • size_t: An unsigned integer type used to represent the size of objects in bytes and is guaranteed to be able to store the maximum size of any object. Format specifier: %zu
  • ptrdiff_t: A signed integer type used to represent the difference between two pointers. Format specifier: %td

Fixed-width Integer Types (from stdint.h)

C99 introduced fixed-width integer types that guarantee exact sizes across different platforms:

Data typeDescriptionSizeFormat specifier
int8_tSigned 8-bit integer1 byte%d (after casting) or use PRId8
uint8_tUnsigned 8-bit integer1 byte%u (after casting) or use PRIu8
int16_tSigned 16-bit integer2 bytes%d or use PRId16
uint16_tUnsigned 16-bit integer2 bytes%u or use PRIu16
int32_tSigned 32-bit integer4 bytes%d or use PRId32
uint32_tUnsigned 32-bit integer4 bytes%u or use PRIu32
int64_tSigned 64-bit integer8 bytes%lld or use PRId64
uint64_tUnsigned 64-bit integer8 bytes%llu or use PRIu64

Examples

#include <stdio.h>
#include <float.h>
#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>

int main()
{
    // Basic types with proper sizeof usage (note: sizeof returns size_t)
    int x = 90;
    printf("size of int: %zu bytes\n", sizeof(int));
    printf("value of x: %d\n", x);
    
    // Unsigned types
    unsigned int ui = 4294967295U;
    printf("size of unsigned int: %zu bytes\n", sizeof(unsigned int));
    printf("max unsigned int: %u\n", ui);
    
    // Long long
    long long ll = 9223372036854775807LL;
    printf("size of long long: %zu bytes\n", sizeof(long long));
    printf("max long long: %lld\n", ll);
    
    // Float and double
    float f = 3.14f;
    printf("size of float: %zu bytes\n", sizeof(float));
    
    double d = 2.25507e-308;
    printf("size of double: %zu bytes\n", sizeof(double));
    
    // Character types
    char c = 'a';
    unsigned char uc = 255;
    printf("size of char: %zu byte\n", sizeof(char));
    printf("char value: %c, unsigned char value: %u\n", c, uc);
    
    // Boolean type
    bool flag = true;
    printf("size of bool: %zu byte\n", sizeof(bool));
    printf("bool value: %d\n", flag);
    
    // Fixed-width types
    int32_t fixed32 = 2147483647;
    uint64_t fixed64u = 18446744073709551615ULL;
    printf("int32_t value: %" PRId32 "\n", fixed32);
    printf("uint64_t value: %" PRIu64 "\n", fixed64u);
    
    // size_t and ptrdiff_t
    size_t size = sizeof(int);
    printf("size_t example: %zu\n", size);
    
    return 0;
}

Check result here

2. Derived Data types

Derived Data types are the ones which are derived from fundamental data types. Arrays, Pointers, Structures, etc. are examples of derived data types. Let's learn more about them in next chapters.

3. Enumeration Data types

Enumeration Data type is a user-defined data type in C. enum keyword is used to declare a new enumeration types in C.

Syntax

enum name{constant1, constant2, constant3, ....... };

Example

#include<stdio.h> 
  
enum month{January, February, March, April, May, June, July, August, September, October, November, December};
  
int main() 
{ 
    enum month name; 
    name = June; 
    printf("%d",name); 
    return 0; 
} 

Check result here

4. Void Data types

Void specifies that there is no return value. Generally void is used in the below situations.

  • If the funtion has return type mentioned as Void, then it specifies that the function returns no value.
  • A function with out any parameters can accept void. For example., char greetings(void)
  • A pointer with type specified as void represents the address of an object but not it's type. Let's learn more about pointers in next chapters.