Introduction to Basic Data Types in C Language

“From today on, study hard and make progress every day”

Repetition is the best method for memory; spend one minute each day to remember the basic knowledge of C language.

“C Language Beginner’s Essential Knowledge Notes Series 100”

7. Introduction to Basic Data Types in C Language

1. Basic Concept of Data Types

C is a strongly typed language, and all variables must be declared with a type before use. The data type determines:

  1. 1. The amount of memory occupied by the variable
  2. 2. The range of values that the variable can store
  3. 3. The operations that can be performed on the variable

2. Classification of Basic Data Types

The basic data types in C can be divided into the following categories:

  1. 1. Integer Types
  • • char
  • • short
  • • int
  • • long
  • • long long
  • 2. Floating Point Types
    • • float
    • • double
    • • long double
  • 3. Void Type
    • • void

    3. Detailed Explanation of Integer Types

    1. char Type

    • • Size: Typically 1 byte (8 bits)
    • • Range: -128 to 127 (signed) or 0 to 255 (unsigned)
    • • Purpose: To store ASCII characters or small integers
    • • Example:
      char grade = 'A';
      unsigned char count = 200;

    2. short Type

    • • Size: Typically 2 bytes (16 bits)
    • • Range: -32,768 to 32,767
    • • Example:
      short temperature = -10;

    3. int Type

    • • Size: Typically 4 bytes (32 bits)
    • • Range: -2,147,483,648 to 2,147,483,647
    • • Example:
      int population = 1400000000;

    4. long Type

    • • Size: Typically 4 or 8 bytes (32 or 64 bits)
    • • Range: Depends on the system
    • • Example:
      long bigNumber = 1234567890L;

    5. long long Type

    • • Size: Typically 8 bytes (64 bits)
    • • Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
    • • Example:
      long long hugeNumber = 1234567890123456789LL;

    4. Detailed Explanation of Floating Point Types

    1. float Type

    • • Size: 4 bytes
    • • Precision: About 6-7 significant digits
    • • Example:
      float pi = 3.14159f;

    2. double Type

    • • Size: 8 bytes
    • • Precision: About 15 significant digits
    • • Example:
      double precisePi = 3.141592653589793;

    3. long double Type

    • • Size: Typically 8 or 16 bytes
    • • Precision: Depends on the implementation
    • • Example:
      long double veryPrecise = 3.141592653589793238L;

    5. Type Modifiers

    Modifiers that can decorate basic data types:

    1. 1. signed: Signed (default)
      signed int a;  // Equivalent to int a
    2. 2. unsigned: Unsigned
      unsigned int b;  // Only stores non-negative numbers
    3. 3. const: Constant
      const int MAX = 100;

    6. Data Type Size Verification

    Use the sizeof operator to get the size of types:

    #include <stdio.h>
    
    int main() {
        printf("char: %zu bytes\n", sizeof(char));
        printf("int: %zu bytes\n", sizeof(int));
        printf("float: %zu bytes\n", sizeof(float));
        printf("double: %zu bytes\n", sizeof(double));
        return 0;
    }

    7. Type Selection Recommendations

    1. 1. Choose the smallest sufficient type based on the data range
    2. 2. Prefer using double over float when precise calculations are needed
    3. 3. Use long long for handling large integers
    4. 4. Use unsigned/signed when the need for signedness is clear

    8. Common Errors

    1. 1. Integer Overflow:
      int a = 2147483647 + 1;  // Overflow
    2. 2. Floating Point Precision Loss:
      float f = 0.1;  // Actual storage has an error
    3. 3. Type Mismatch:
      int a = 3.14;  // Decimal part is truncated

    9. Type Conversion

    1. 1. Implicit Conversion (automatic):
      int i = 3.14;  // 3.14 is converted to int
    2. 2. Explicit Conversion (forced):
      double d = (double)5 / 2;  // Result is 2.5

    ———- End ———-

    [Special Statement: All articles in this public account are original or authorized by the author, some content and images are sourced from the internet and AI, please feel free to use, opinions are for learning reference only~~]

    Introduction to Basic Data Types in C Language

    “If you like C, please like it”Introduction to Basic Data Types in C Language Click the bottom right to viewIntroduction to Basic Data Types in C Language

    Leave a Comment