In C language, basic data types (also known as primary data types) are the fundamental types provided by the language to represent various kinds of data. These types define the kind of data that can be stored in a variable and dictate the operations that can be performed on that data. Below is an overview of the basic data types in C:

1. Integer (int)

  • Description: Used to store whole numbers (both positive and negative).
  • Size: Typically 4 bytes (32 bits), but it can vary based on the system architecture.
  • Range: Depends on whether it's signed or unsigned.
    • Signed int: 2,147,483,648-2,147,483,648 to 2,147,483,6472,147,483,647
    • Unsigned int: 00 to 4,294,967,2954,294,967,295
  • Example:
    int age = 25;

2. Character (char)

  • Description: Used to store individual characters (letters, digits, punctuation, etc.).
  • Size: Always 1 byte (8 bits).
  • Range: 128-128 to 127127 for signed characters, or 00 to 255255 for unsigned characters.
  • Example:
    char initial = 'A';

3. Floating-Point (float)

  • Description: Used to store decimal numbers (floating-point numbers) with single precision.
  • Size: Typically 4 bytes (32 bits).
  • Range: Approximately 3.4×1038-3.4 \times 10^{38} to 3.4×10383.4 \times 10^{38} with about 7 decimal digits of precision.
  • Example:
    float height = 5.9;

4. Double (double)

  • Description: Used to store decimal numbers with double precision, allowing for more precise values than float.
  • Size: Typically 8 bytes (64 bits).
  • Range: Approximately 1.7×10308-1.7 \times 10^{308} to 1.7×103081.7 \times 10^{308} with about 15 decimal digits of precision.
  • Example:
    double distance = 12345.678;

Modifiers for Basic Data Types

C allows the use of modifiers to change the properties of the basic data types. These modifiers can adjust the size and range of the data types.

  1. Short: Reduces the size of the integer type.

    • Example:
      short int smallNum = 100; // Typically 2 bytes
  2. Long: Increases the size of the integer type.

    • Example:
      long int largeNum = 100000L; // Typically 4 or 8 bytes
  3. Unsigned: Indicates that the variable can only hold non-negative values.

    • Example:
      unsigned int positiveNum = 500; // Can only store positive integers


Example Program Using Basic Data Types

Here’s a simple program that demonstrates the use of various basic 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 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); return 0; }

Output

Age: 30 Initial: A Height: 5.9 Distance: 1500.75 Score: 85

Summary

  • Basic Data Types: Include int, char, float, and double, which represent fundamental types of data.
  • Modifiers: Include short, long, and unsigned, which can alter the size and range of basic types.
  • Memory Size: The size of data types can vary based on the system architecture.

Understanding basic data types is essential for effective programming in C, as they form the foundation for declaring and manipulating variables.