C Data Types


Data types in C language specify the type of data that a variable can hold. They determine the size and type of information that can be stored in that variable, such as integers, characters, or floating-point numbers. Choosing the appropriate data type is crucial for effective memory usage and performance.

Categories of Data Types in C

Data types in C can be broadly categorized into four groups:

  1. Basic (Primary) Data Types
  2. Derived Data Types
  3. Enumeration Data Type
  4. Void Data Type

Let's go through each of them in detail.

1. Basic (Primary) Data Types

These are the fundamental data types used to define variables in C.

  • Integer (int): Used to store whole numbers.

    • Size: Typically 4 bytes (depends on the system and compiler).
    • Example:
      int age = 25;
  • Character (char): Used to store individual characters.

    • Size: 1 byte.
    • Example:
      char letter = 'A';
  • Floating-point (float): Used to store decimal numbers with single precision.

    • Size: Typically 4 bytes.
    • Example:
      float height = 5.9;
  • Double (double): Used to store decimal numbers with double precision.

    • Size: Typically 8 bytes.
    • Example:
      double distance = 12345.678;
  • Modifiers: The basic data types can be modified using keywords such as short, long, signed, and unsigned to change their size and range.

    • Examples:
      • short int: Uses less memory than a standard integer.
        short int smallNum = 100;
      • long int: Can hold larger values than a standard integer.
        long int largeNum = 100000L;
      • unsigned int: Can hold only positive values and zero.
        unsigned int positiveNum = 500;

2. Derived Data Types

Derived data types are created using primary data types and include arrays, pointers, structures, and unions.

  • Array: A collection of elements of the same type.

    • Example:
      int numbers[5] = {1, 2, 3, 4, 5};
  • Pointer: A variable that stores the address of another variable.

    • Example:
      int num = 10; int *ptr = # // ptr is a pointer to an integer
  • Structure (struct): A user-defined data type that groups different types of variables under a single name.

    • Example:
      struct Student { char name[50]; int age; float grade; };
  • Union (union): Similar to a structure, but all members share the same memory space, so only one member can be used at a time.

    • Example:
      union Data { int i; float f; char str[20]; };

3. Enumeration Data Type (enum)

Enumeration is used to define a variable that can be assigned one of a set of predefined values. It's useful for variables that have a limited number of possible values.

  • Example:
    enum day {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}; enum day today = Tuesday; // 'today' can only have values from the 'day' set

4. Void Data Type

The void data type represents the absence of a value.

  • Void Function: A function that does not return a value.

    • Example:
      void greet() { printf("Hello, World!\n"); }
  • Void Pointer: A generic pointer that can point to any data type.

    • Example:
      void *ptr;

Summary of Data Types

Data TypeDescriptionSize (Typical)
intInteger numbers4 bytes
charCharacter data1 byte
floatSingle precision floating-point4 bytes
doubleDouble precision floating-point8 bytes
shortShort integer2 bytes
longLong integer4 or 8 bytes
unsignedPositive integer values onlyDepends on type
enumEnumerated constantsDepends on implementation
structGrouping of different data typesVaries
unionMemory sharing for multiple membersSize of largest member
voidRepresents "no value"0 bytes

Example Program Using Different Data Types

#include <stdio.h> int main() { int age = 30; // Integer type char initial = 'A'; // Character type float height = 5.9; // Float type double distance = 1500.75; // Double type unsigned int score = 85; // Unsigned integer enum day today = Friday; // Enumeration type printf("Age: %d\n", age); printf("Initial: %c\n", initial); printf("Height: %.1f\n", height); printf("Distance: %.2f\n", distance); printf("Score: %u\n", score); printf("Today is day number: %d\n", today); // Days are represented as integers starting from 0 return 0; }

Summary

  • Primary Data Types: Include int, char, float, and double, used for basic values.
  • Derived Data Types: Include arrays, pointers, structures, and unions, which are created using primary data types.
  • Enumeration (enum): Allows for creating a variable that can only take a restricted set of values.
  • Void: Represents no value or a generic type.

Understanding and using the appropriate data types helps ensure efficient memory usage and better performance in C programming.